1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
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 |
|
|
|
|
| 60.9% |
Uncovered Elements: 43 (110) |
Complexity: 30 |
Complexity Density: 0.39 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
59 |
110 |
public static boolean isInit()... |
60 |
|
{ |
61 |
110 |
return init; |
62 |
|
} |
63 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
64 |
370 |
public static Level log4jLevel(JLogger.LogLevel loglevel)... |
65 |
|
{ |
66 |
370 |
return Level.toLevel(loglevel.toString()); |
67 |
|
} |
68 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
69 |
0 |
public static void init(JLogger.LogLevel myLevel)... |
70 |
|
{ |
71 |
0 |
init(log4jLevel(myLevel)); |
72 |
|
} |
73 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
74 |
110 |
public static void init(JLogger.LogLevel myLevel, File logfile)... |
75 |
|
{ |
76 |
110 |
init(log4jLevel(myLevel), logfile); |
77 |
|
} |
78 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
79 |
0 |
public static void init(Level myLevel)... |
80 |
|
{ |
81 |
0 |
init(myLevel, null); |
82 |
|
} |
83 |
|
|
|
|
| 69% |
Uncovered Elements: 9 (29) |
Complexity: 4 |
Complexity Density: 0.16 |
|
84 |
110 |
public static void init(Level myLevel, File logfile)... |
85 |
|
{ |
86 |
110 |
if (init) |
87 |
|
{ |
88 |
55 |
return; |
89 |
|
} |
90 |
55 |
try |
91 |
|
{ |
92 |
|
|
93 |
55 |
ConfigurationBuilder<BuiltConfiguration> configBuilder = Log4j |
94 |
|
.getConfigurationBuilder(); |
95 |
55 |
configBuilder.setStatusLevel(Level.INFO); |
96 |
|
|
97 |
|
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
136 |
0 |
public static Logger getLogger(String name)... |
137 |
|
{ |
138 |
0 |
return getLogger(name, Level.INFO); |
139 |
|
} |
140 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
141 |
110 |
public static Logger getLogger(String name, JLogger.LogLevel loglevel)... |
142 |
|
{ |
143 |
110 |
return getLogger(name, log4jLevel(loglevel)); |
144 |
|
} |
145 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
146 |
110 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
153 |
165 |
public static ConfigurationBuilder<BuiltConfiguration> getConfigurationBuilder()... |
154 |
|
{ |
155 |
165 |
return ConfigurationFactory.newConfigurationBuilder(); |
156 |
|
} |
157 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
158 |
0 |
public static Layout getTimeLayout()... |
159 |
|
{ |
160 |
0 |
return PatternLayout.newBuilder().withPattern(TIME_PATTERN).build(); |
161 |
|
} |
162 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
163 |
75 |
public static Layout getSimpleLayout()... |
164 |
|
{ |
165 |
75 |
return PatternLayout.newBuilder().withPattern(SIMPLE_PATTERN).build(); |
166 |
|
} |
167 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
168 |
0 |
public static LayoutComponentBuilder getTimeLayoutBuilder()... |
169 |
|
{ |
170 |
0 |
return getConfigurationBuilder().newLayout("PatternLayout") |
171 |
|
.addAttribute("pattern", Log4j.TIME_PATTERN); |
172 |
|
} |
173 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
174 |
55 |
public static LayoutComponentBuilder getSimpleLayoutBuilder()... |
175 |
|
{ |
176 |
55 |
return getConfigurationBuilder().newLayout("PatternLayout") |
177 |
|
.addAttribute("pattern", Log4j.SIMPLE_PATTERN); |
178 |
|
} |
179 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
180 |
75 |
public static Filter getThresholdFilter(Level level)... |
181 |
|
{ |
182 |
75 |
return ThresholdFilter.createFilter(level, Filter.Result.ACCEPT, |
183 |
|
Filter.Result.NEUTRAL); |
184 |
|
} |
185 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
186 |
55 |
public static FilterComponentBuilder getThresholdFilterBuilder()... |
187 |
|
{ |
188 |
55 |
return getConfigurationBuilder().newFilter("ThresholdFilter", |
189 |
|
Filter.Result.ACCEPT, Filter.Result.NEUTRAL); |
190 |
|
} |
191 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
192 |
75 |
public static void setLevel(Logger logger, JLogger.LogLevel loglevel)... |
193 |
|
{ |
194 |
75 |
setLevel(logger, log4jLevel(loglevel)); |
195 |
|
} |
196 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
197 |
185 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
214 |
0 |
public static void setRootLevel(JLogger.LogLevel loglevel)... |
215 |
|
{ |
216 |
0 |
setRootLevel(log4jLevel(loglevel)); |
217 |
|
} |
218 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
219 |
0 |
public static void setRootLevel(Level level)... |
220 |
|
{ |
221 |
0 |
setLevel(LogManager.getRootLogger(), level); |
222 |
|
} |
223 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
224 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
232 |
0 |
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 |
|
|
|
|
| 73.3% |
Uncovered Elements: 4 (15) |
Complexity: 3 |
Complexity Density: 0.27 |
|
245 |
75 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
263 |
0 |
public static void addAppenderToRootLogger(Appender appender)... |
264 |
|
{ |
265 |
0 |
Log4j.addAppender(LogManager.getRootLogger(), appender); |
266 |
|
} |
267 |
|
} |