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 |
|
|
|
|
| 0% |
Uncovered Elements: 110 (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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
59 |
0 |
public static boolean isInit()... |
60 |
|
{ |
61 |
0 |
return init; |
62 |
|
} |
63 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
64 |
0 |
public static Level log4jLevel(JLogger.LogLevel loglevel)... |
65 |
|
{ |
66 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
74 |
0 |
public static void init(JLogger.LogLevel myLevel, File logfile)... |
75 |
|
{ |
76 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 29 (29) |
Complexity: 4 |
Complexity Density: 0.16 |
|
84 |
0 |
public static void init(Level myLevel, File logfile)... |
85 |
|
{ |
86 |
0 |
if (init) |
87 |
|
{ |
88 |
0 |
return; |
89 |
|
} |
90 |
0 |
try |
91 |
|
{ |
92 |
|
|
93 |
0 |
ConfigurationBuilder<BuiltConfiguration> configBuilder = Log4j |
94 |
|
.getConfigurationBuilder(); |
95 |
0 |
configBuilder.setStatusLevel(Level.INFO); |
96 |
|
|
97 |
|
|
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 |
|
|
|
|
| 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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
141 |
0 |
public static Logger getLogger(String name, JLogger.LogLevel loglevel)... |
142 |
|
{ |
143 |
0 |
return getLogger(name, log4jLevel(loglevel)); |
144 |
|
} |
145 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
146 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
153 |
0 |
public static ConfigurationBuilder<BuiltConfiguration> getConfigurationBuilder()... |
154 |
|
{ |
155 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
163 |
0 |
public static Layout getSimpleLayout()... |
164 |
|
{ |
165 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
174 |
0 |
public static LayoutComponentBuilder getSimpleLayoutBuilder()... |
175 |
|
{ |
176 |
0 |
return getConfigurationBuilder().newLayout("PatternLayout") |
177 |
|
.addAttribute("pattern", Log4j.SIMPLE_PATTERN); |
178 |
|
} |
179 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
180 |
0 |
public static Filter getThresholdFilter(Level level)... |
181 |
|
{ |
182 |
0 |
return ThresholdFilter.createFilter(level, Filter.Result.ACCEPT, |
183 |
|
Filter.Result.NEUTRAL); |
184 |
|
} |
185 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
186 |
0 |
public static FilterComponentBuilder getThresholdFilterBuilder()... |
187 |
|
{ |
188 |
0 |
return getConfigurationBuilder().newFilter("ThresholdFilter", |
189 |
|
Filter.Result.ACCEPT, Filter.Result.NEUTRAL); |
190 |
|
} |
191 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
192 |
0 |
public static void setLevel(Logger logger, JLogger.LogLevel loglevel)... |
193 |
|
{ |
194 |
0 |
setLevel(logger, log4jLevel(loglevel)); |
195 |
|
} |
196 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
197 |
0 |
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 |
|
|
|
|
| 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 |
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 3 |
Complexity Density: 0.27 |
|
245 |
0 |
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 |
|
|
|
|
| 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 |
|
} |