Clover icon

Coverage Report

  1. Project Clover database Mon Nov 18 2024 09:38:20 GMT
  2. Package jalview.util

File Log4j.java

 

Coverage histogram

../../img/srcFileCovDistChart6.png
27% 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.609090960.9%
 

Contributing tests

This file is covered by 23 tests. .

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  12 toggle public static boolean isInit()
60    {
61  12 return init;
62    }
63   
 
64  118 toggle public static Level log4jLevel(JLogger.LogLevel loglevel)
65    {
66  118 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  12 toggle public static void init(JLogger.LogLevel myLevel, File logfile)
75    {
76  12 init(log4jLevel(myLevel), logfile);
77    }
78   
 
79  0 toggle public static void init(Level myLevel)
80    {
81  0 init(myLevel, null);
82    }
83   
 
84  12 toggle public static void init(Level myLevel, File logfile)
85    {
86  12 if (init)
87    {
88  6 return;
89    }
90  6 try
91    {
92    // configure the root logger to stderr
93  6 ConfigurationBuilder<BuiltConfiguration> configBuilder = Log4j
94    .getConfigurationBuilder();
95  6 configBuilder.setStatusLevel(Level.INFO);
96   
97    // Console
98  6 String consoleLoggerName = "STDERR";
99  6 AppenderComponentBuilder appenderBuilder = configBuilder
100    .newAppender(consoleLoggerName, "Console");
101  6 appenderBuilder.addAttribute("target",
102    ConsoleAppender.Target.SYSTEM_ERR);
103  6 appenderBuilder.add(Log4j.getSimpleLayoutBuilder());
104  6 appenderBuilder.add(Log4j.getThresholdFilterBuilder());
105  6 configBuilder.add(appenderBuilder);
106   
107  6 RootLoggerComponentBuilder root = configBuilder
108    .newRootLogger(myLevel);
109  6 root.add(configBuilder.newAppenderRef(consoleLoggerName));
110   
111  6 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  6 configBuilder.add(root);
124   
125  6 Configurator.initialize(configBuilder.build());
126   
127  6 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  12 toggle public static Logger getLogger(String name, JLogger.LogLevel loglevel)
142    {
143  12 return getLogger(name, log4jLevel(loglevel));
144    }
145   
 
146  12 toggle public static Logger getLogger(String name, Level level)
147    {
148  12 Logger logger = LogManager.getLogger(name);
149  12 Log4j.setLevel(logger, level);
150  12 return logger;
151    }
152   
 
153  18 toggle public static ConfigurationBuilder<BuiltConfiguration> getConfigurationBuilder()
154    {
155  18 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  47 toggle public static Layout getSimpleLayout()
164    {
165  47 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  6 toggle public static LayoutComponentBuilder getSimpleLayoutBuilder()
175    {
176  6 return getConfigurationBuilder().newLayout("PatternLayout")
177    .addAttribute("pattern", Log4j.SIMPLE_PATTERN);
178    }
179   
 
180  47 toggle public static Filter getThresholdFilter(Level level)
181    {
182  47 return ThresholdFilter.createFilter(level, Filter.Result.ACCEPT,
183    Filter.Result.NEUTRAL);
184    }
185   
 
186  6 toggle public static FilterComponentBuilder getThresholdFilterBuilder()
187    {
188  6 return getConfigurationBuilder().newFilter("ThresholdFilter",
189    Filter.Result.ACCEPT, Filter.Result.NEUTRAL);
190    }
191   
 
192  47 toggle public static void setLevel(Logger logger, JLogger.LogLevel loglevel)
193    {
194  47 setLevel(logger, log4jLevel(loglevel));
195    }
196   
 
197  59 toggle public static void setLevel(Logger logger, Level level)
198    {
199  59 if (!Platform.isJS())
200    {
201  59 LoggerContext context = (LoggerContext) LogManager.getContext(false);
202  59 Configuration config = context.getConfiguration();
203  59 LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
204  59 loggerConfig.setLevel(level);
205   
206  59 Map<String, Appender> appenders = config.getAppenders();
207   
208  59 Appender jappender = config.getAppender(JalviewAppender.NAME);
209   
210  59 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  47 toggle public static void addAppender(Logger logger, Appender appender)
246    {
247  47 if (appender == null)
248  0 return;
249  47 LoggerContext context = (LoggerContext) LogManager.getContext(false);
250  47 Configuration config = context.getConfiguration();
251  47 LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
252  47 if (loggerConfig == null)
253  0 return;
254   
255  47 Level level = loggerConfig.getLevel();
256   
257  47 config.addAppender(appender);
258  47 loggerConfig.addAppender(appender, null, null);
259   
260  47 context.updateLoggers();
261    }
262   
 
263  0 toggle public static void addAppenderToRootLogger(Appender appender)
264    {
265  0 Log4j.addAppender(LogManager.getRootLogger(), appender);
266    }
267    }