Clover icon

Coverage Report

  1. Project Clover database Fri Dec 6 2024 13:47:14 GMT
  2. Package jalview.util

File Log4j.java

 

Coverage histogram

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