Clover icon

Coverage Report

  1. Project Clover database Thu Nov 7 2024 17:01:39 GMT
  2. Package jalview.util

File Log4j.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
0% of files have more coverage

Code metrics

10
76
24
1
267
201
30
0.39
3.17
24
1.25

Classes

Class Line # Actions
Log4j 50 76 30
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.util;
22   
23    import java.io.File;
24    import java.util.Map;
25   
26    import org.apache.logging.log4j.Level;
27    import org.apache.logging.log4j.LogManager;
28    import org.apache.logging.log4j.Logger;
29    import org.apache.logging.log4j.core.Appender;
30    import org.apache.logging.log4j.core.Filter;
31    import org.apache.logging.log4j.core.Layout;
32    import org.apache.logging.log4j.core.LoggerContext;
33    import org.apache.logging.log4j.core.appender.ConsoleAppender;
34    import org.apache.logging.log4j.core.config.Configuration;
35    import org.apache.logging.log4j.core.config.ConfigurationFactory;
36    import org.apache.logging.log4j.core.config.Configurator;
37    import org.apache.logging.log4j.core.config.LoggerConfig;
38    import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder;
39    import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
40    import org.apache.logging.log4j.core.config.builder.api.FilterComponentBuilder;
41    import org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder;
42    import org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder;
43    import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
44    import org.apache.logging.log4j.core.filter.ThresholdFilter;
45    import org.apache.logging.log4j.core.layout.PatternLayout;
46   
47    import jalview.log.JLogger;
48    import jalview.log.JalviewAppender;
49   
 
50    public class Log4j
51    {
52   
53    public final static String SIMPLE_PATTERN = "%level - %m%n";
54   
55    public final static String TIME_PATTERN = "%d{yyyy-MM-dd HH:mm:ss} %level - %m%n";
56   
57    private static boolean init = false;
58   
 
59  0 toggle public static boolean isInit()
60    {
61  0 return init;
62    }
63   
 
64  0 toggle public static Level log4jLevel(JLogger.LogLevel loglevel)
65    {
66  0 return Level.toLevel(loglevel.toString());
67    }
68   
 
69  0 toggle public static void init(JLogger.LogLevel myLevel)
70    {
71  0 init(log4jLevel(myLevel));
72    }
73   
 
74  0 toggle public static void init(JLogger.LogLevel myLevel, File logfile)
75    {
76  0 init(log4jLevel(myLevel), logfile);
77    }
78   
 
79  0 toggle public static void init(Level myLevel)
80    {
81  0 init(myLevel, null);
82    }
83   
 
84  0 toggle public static void init(Level myLevel, File logfile)
85    {
86  0 if (init)
87    {
88  0 return;
89    }
90  0 try
91    {
92    // configure the root logger to stderr
93  0 ConfigurationBuilder<BuiltConfiguration> configBuilder = Log4j
94    .getConfigurationBuilder();
95  0 configBuilder.setStatusLevel(Level.INFO);
96   
97    // Console
98  0 String consoleLoggerName = "STDERR";
99  0 AppenderComponentBuilder appenderBuilder = configBuilder
100    .newAppender(consoleLoggerName, "Console");
101  0 appenderBuilder.addAttribute("target",
102    ConsoleAppender.Target.SYSTEM_ERR);
103  0 appenderBuilder.add(Log4j.getSimpleLayoutBuilder());
104  0 appenderBuilder.add(Log4j.getThresholdFilterBuilder());
105  0 configBuilder.add(appenderBuilder);
106   
107  0 RootLoggerComponentBuilder root = configBuilder
108    .newRootLogger(myLevel);
109  0 root.add(configBuilder.newAppenderRef(consoleLoggerName));
110   
111  0 if (logfile != null)
112    {
113  0 String fileLoggerName = "LOGFILE";
114  0 appenderBuilder = configBuilder.newAppender(fileLoggerName, "File")
115    .addAttribute("fileName", logfile.getAbsolutePath());
116  0 appenderBuilder.add(Log4j.getTimeLayoutBuilder());
117  0 appenderBuilder.add(Log4j.getThresholdFilterBuilder());
118   
119  0 configBuilder.add(appenderBuilder);
120   
121  0 root.add(configBuilder.newAppenderRef(fileLoggerName));
122    }
123  0 configBuilder.add(root);
124   
125  0 Configurator.initialize(configBuilder.build());
126   
127  0 init = true;
128    } catch (Exception e)
129    {
130  0 jalview.bin.Console
131    .errPrintln("Problems initializing the log4j system\n");
132  0 e.printStackTrace(System.err);
133    }
134    }
135   
 
136  0 toggle public static Logger getLogger(String name)
137    {
138  0 return getLogger(name, Level.INFO);
139    }
140   
 
141  0 toggle public static Logger getLogger(String name, JLogger.LogLevel loglevel)
142    {
143  0 return getLogger(name, log4jLevel(loglevel));
144    }
145   
 
146  0 toggle public static Logger getLogger(String name, Level level)
147    {
148  0 Logger logger = LogManager.getLogger(name);
149  0 Log4j.setLevel(logger, level);
150  0 return logger;
151    }
152   
 
153  0 toggle public static ConfigurationBuilder<BuiltConfiguration> getConfigurationBuilder()
154    {
155  0 return ConfigurationFactory.newConfigurationBuilder();
156    }
157   
 
158  0 toggle public static Layout getTimeLayout()
159    {
160  0 return PatternLayout.newBuilder().withPattern(TIME_PATTERN).build();
161    }
162   
 
163  0 toggle public static Layout getSimpleLayout()
164    {
165  0 return PatternLayout.newBuilder().withPattern(SIMPLE_PATTERN).build();
166    }
167   
 
168  0 toggle public static LayoutComponentBuilder getTimeLayoutBuilder()
169    {
170  0 return getConfigurationBuilder().newLayout("PatternLayout")
171    .addAttribute("pattern", Log4j.TIME_PATTERN);
172    }
173   
 
174  0 toggle public static LayoutComponentBuilder getSimpleLayoutBuilder()
175    {
176  0 return getConfigurationBuilder().newLayout("PatternLayout")
177    .addAttribute("pattern", Log4j.SIMPLE_PATTERN);
178    }
179   
 
180  0 toggle public static Filter getThresholdFilter(Level level)
181    {
182  0 return ThresholdFilter.createFilter(level, Filter.Result.ACCEPT,
183    Filter.Result.NEUTRAL);
184    }
185   
 
186  0 toggle public static FilterComponentBuilder getThresholdFilterBuilder()
187    {
188  0 return getConfigurationBuilder().newFilter("ThresholdFilter",
189    Filter.Result.ACCEPT, Filter.Result.NEUTRAL);
190    }
191   
 
192  0 toggle public static void setLevel(Logger logger, JLogger.LogLevel loglevel)
193    {
194  0 setLevel(logger, log4jLevel(loglevel));
195    }
196   
 
197  0 toggle public static void setLevel(Logger logger, Level level)
198    {
199  0 if (!Platform.isJS())
200    {
201  0 LoggerContext context = (LoggerContext) LogManager.getContext(false);
202  0 Configuration config = context.getConfiguration();
203  0 LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
204  0 loggerConfig.setLevel(level);
205   
206  0 Map<String, Appender> appenders = config.getAppenders();
207   
208  0 Appender jappender = config.getAppender(JalviewAppender.NAME);
209   
210  0 context.updateLoggers();
211    }
212    }
213   
 
214  0 toggle public static void setRootLevel(JLogger.LogLevel loglevel)
215    {
216  0 setRootLevel(log4jLevel(loglevel));
217    }
218   
 
219  0 toggle public static void setRootLevel(Level level)
220    {
221  0 setLevel(LogManager.getRootLogger(), level);
222    }
223   
 
224  0 toggle public static Appender getAppender(String name)
225    {
226  0 LoggerContext context = (LoggerContext) LogManager.getContext(false);
227  0 Configuration config = context.getConfiguration();
228  0 Map<String, Appender> appenders = config.getAppenders();
229  0 return appenders.get(name);
230    }
231   
 
232  0 toggle public static void addAppender(Logger logger, Logger logger2,
233    String name2)
234    {
235  0 LoggerContext context = (LoggerContext) LogManager.getContext(false);
236  0 Configuration config = context.getConfiguration();
237  0 LoggerConfig logger2Config = config.getLoggerConfig(logger2.getName());
238  0 Map<String, Appender> logger2AppendersMap = logger2Config
239    .getAppenders();
240  0 Appender appender = logger2AppendersMap.get(name2);
241  0 addAppender(logger, appender);
242  0 context.updateLoggers();
243    }
244   
 
245  0 toggle public static void addAppender(Logger logger, Appender appender)
246    {
247  0 if (appender == null)
248  0 return;
249  0 LoggerContext context = (LoggerContext) LogManager.getContext(false);
250  0 Configuration config = context.getConfiguration();
251  0 LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
252  0 if (loggerConfig == null)
253  0 return;
254   
255  0 Level level = loggerConfig.getLevel();
256   
257  0 config.addAppender(appender);
258  0 loggerConfig.addAppender(appender, null, null);
259   
260  0 context.updateLoggers();
261    }
262   
 
263  0 toggle public static void addAppenderToRootLogger(Appender appender)
264    {
265  0 Log4j.addAppender(LogManager.getRootLogger(), appender);
266    }
267    }