/
Log4JLogger.java
518 lines (419 loc) · 16.1 KB
/
Log4JLogger.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.logger.log4j;
import org.apache.ignite.*;
import org.apache.ignite.internal.util.*;
import org.apache.ignite.internal.util.tostring.*;
import org.apache.ignite.internal.util.typedef.*;
import org.apache.ignite.internal.util.typedef.internal.*;
import org.apache.ignite.lang.*;
import org.apache.ignite.logger.*;
import org.apache.log4j.*;
import org.apache.log4j.varia.*;
import org.apache.log4j.xml.*;
import org.jetbrains.annotations.*;
import java.io.*;
import java.net.*;
import java.util.*;
import static org.apache.ignite.IgniteSystemProperties.*;
/**
* Log4j-based implementation for logging. This logger should be used
* by loaders that have prefer <a target=_new href="http://logging.apache.org/log4j/docs/">log4j</a>-based logging.
* <p>
* Here is a typical example of configuring log4j logger in Ignite configuration file:
* <pre name="code" class="xml">
* <property name="gridLogger">
* <bean class="org.apache.ignite.logger.log4j.Log4JLogger">
* <constructor-arg type="java.lang.String" value="config/ignite-log4j.xml"/>
* </bean>
* </property>
* </pre>
* and from your code:
* <pre name="code" class="java">
* IgniteConfiguration cfg = new IgniteConfiguration();
* ...
* URL xml = U.resolveIgniteUrl("config/custom-log4j.xml");
* IgniteLogger log = new Log4JLogger(xml);
* ...
* cfg.setGridLogger(log);
* </pre>
*
* Please take a look at <a target=_new href="http://logging.apache.org/log4j/1.2/index.html">Apache Log4j 1.2</a>
* for additional information.
* <p>
* It's recommended to use Ignite logger injection instead of using/instantiating
* logger in your task/job code. See {@link org.apache.ignite.resources.LoggerResource} annotation about logger
* injection.
*/
public class Log4JLogger implements IgniteLogger, LoggerNodeIdAware, Log4jFileAware {
/** Appenders. */
private static Collection<FileAppender> fileAppenders = new GridConcurrentHashSet<>();
/** */
private static volatile boolean inited;
/** */
private static volatile boolean quiet0;
/** */
private static final Object mux = new Object();
/** Logger implementation. */
@GridToStringExclude
@SuppressWarnings("FieldAccessedSynchronizedAndUnsynchronized")
private Logger impl;
/** Path to configuration file. */
private final String path;
/** Quiet flag. */
private final boolean quiet;
/** Node ID. */
private UUID nodeId;
/**
* Creates new logger and automatically detects if root logger already
* has appenders configured. If it does not, the root logger will be
* configured with default appender (analogous to calling
* {@link #Log4JLogger(boolean) Log4JLogger(boolean)}
* with parameter {@code true}, otherwise, existing appenders will be used (analogous
* to calling {@link #Log4JLogger(boolean) Log4JLogger(boolean)}
* with parameter {@code false}).
*/
public Log4JLogger() {
this(!isConfigured());
}
/**
* Creates new logger. If initialize parameter is {@code true} the Log4j
* logger will be initialized with default console appender and {@code INFO}
* log level.
*
* @param init If {@code true}, then a default console appender with
* following pattern layout will be created: {@code %d{ABSOLUTE} %-5p [%c{1}] %m%n}.
* If {@code false}, then no implicit initialization will take place,
* and {@code Log4j} should be configured prior to calling this
* constructor.
*/
public Log4JLogger(boolean init) {
impl = Logger.getRootLogger();
if (init) {
// Implementation has already been inited, passing NULL.
addConsoleAppenderIfNeeded(Level.INFO, null);
quiet = quiet0;
}
else
quiet = true;
path = null;
}
/**
* Creates new logger with given implementation.
*
* @param impl Log4j implementation to use.
*/
public Log4JLogger(final Logger impl) {
assert impl != null;
path = null;
addConsoleAppenderIfNeeded(null, new C1<Boolean, Logger>() {
@Override public Logger apply(Boolean init) {
return impl;
}
});
quiet = quiet0;
}
/**
* Creates new logger with given configuration {@code path}.
*
* @param path Path to log4j configuration XML file.
* @throws IgniteCheckedException Thrown in case logger can't be created.
*/
public Log4JLogger(String path) throws IgniteCheckedException {
if (path == null)
throw new IgniteCheckedException("Configuration XML file for Log4j must be specified.");
this.path = path;
final URL cfgUrl = U.resolveIgniteUrl(path);
if (cfgUrl == null)
throw new IgniteCheckedException("Log4j configuration path was not found: " + path);
addConsoleAppenderIfNeeded(null, new C1<Boolean, Logger>() {
@Override public Logger apply(Boolean init) {
if (init)
DOMConfigurator.configure(cfgUrl);
return Logger.getRootLogger();
}
});
quiet = quiet0;
}
/**
* Creates new logger with given configuration {@code cfgFile}.
*
* @param cfgFile Log4j configuration XML file.
* @throws IgniteCheckedException Thrown in case logger can't be created.
*/
public Log4JLogger(File cfgFile) throws IgniteCheckedException {
if (cfgFile == null)
throw new IgniteCheckedException("Configuration XML file for Log4j must be specified.");
if (!cfgFile.exists() || cfgFile.isDirectory())
throw new IgniteCheckedException("Log4j configuration path was not found or is a directory: " + cfgFile);
path = cfgFile.getAbsolutePath();
addConsoleAppenderIfNeeded(null, new C1<Boolean, Logger>() {
@Override public Logger apply(Boolean init) {
if (init)
DOMConfigurator.configure(path);
return Logger.getRootLogger();
}
});
quiet = quiet0;
}
/**
* Creates new logger with given configuration {@code cfgUrl}.
*
* @param cfgUrl URL for Log4j configuration XML file.
* @throws IgniteCheckedException Thrown in case logger can't be created.
*/
public Log4JLogger(final URL cfgUrl) throws IgniteCheckedException {
if (cfgUrl == null)
throw new IgniteCheckedException("Configuration XML file for Log4j must be specified.");
path = null;
addConsoleAppenderIfNeeded(null, new C1<Boolean, Logger>() {
@Override public Logger apply(Boolean init) {
if (init)
DOMConfigurator.configure(cfgUrl);
return Logger.getRootLogger();
}
});
quiet = quiet0;
}
/**
* Checks if Log4j is already configured within this VM or not.
*
* @return {@code True} if log4j was already configured, {@code false} otherwise.
*/
public static boolean isConfigured() {
return Logger.getRootLogger().getAllAppenders().hasMoreElements();
}
/**
* Sets level for internal log4j implementation.
*
* @param level Log level to set.
*/
public void setLevel(Level level) {
impl.setLevel(level);
}
/** {@inheritDoc} */
@Nullable @Override public String fileName() {
FileAppender fapp = F.first(fileAppenders);
return fapp != null ? fapp.getFile() : null;
}
/**
* Adds console appender when needed with some default logging settings.
*
* @param logLevel Optional log level.
* @param implInitC Optional log implementation init closure.
*/
private void addConsoleAppenderIfNeeded(@Nullable Level logLevel,
@Nullable IgniteClosure<Boolean, Logger> implInitC) {
if (inited) {
if (implInitC != null)
// Do not init.
impl = implInitC.apply(false);
return;
}
synchronized (mux) {
if (inited) {
if (implInitC != null)
// Do not init.
impl = implInitC.apply(false);
return;
}
if (implInitC != null)
// Init logger impl.
impl = implInitC.apply(true);
boolean quiet = Boolean.valueOf(System.getProperty(IGNITE_QUIET, "true"));
boolean consoleAppenderFound = false;
Category rootCategory = null;
ConsoleAppender errAppender = null;
for (Category l = impl; l != null; ) {
if (!consoleAppenderFound) {
for (Enumeration appenders = l.getAllAppenders(); appenders.hasMoreElements(); ) {
Appender appender = (Appender)appenders.nextElement();
if (appender instanceof ConsoleAppender) {
if ("CONSOLE_ERR".equals(appender.getName())) {
// Treat CONSOLE_ERR appender as a system one and don't count it.
errAppender = (ConsoleAppender)appender;
continue;
}
consoleAppenderFound = true;
break;
}
}
}
if (l.getParent() == null) {
rootCategory = l;
break;
}
else
l = l.getParent();
}
if (consoleAppenderFound && quiet)
// User configured console appender, but log is quiet.
quiet = false;
if (!consoleAppenderFound && !quiet && Boolean.valueOf(System.getProperty(IGNITE_CONSOLE_APPENDER, "true"))) {
// Console appender not found => we've looked through all categories up to root.
assert rootCategory != null;
// User launched ignite in verbose mode and did not add console appender with INFO level
// to configuration and did not set IGNITE_CONSOLE_APPENDER to false.
if (errAppender != null) {
rootCategory.addAppender(createConsoleAppender(Level.INFO));
if (errAppender.getThreshold() == Level.ERROR)
errAppender.setThreshold(Level.WARN);
}
else
// No error console appender => create console appender with no level limit.
rootCategory.addAppender(createConsoleAppender(Level.OFF));
if (logLevel != null)
impl.setLevel(logLevel);
}
quiet0 = quiet;
inited = true;
}
}
/**
* Creates console appender with some reasonable default logging settings.
*
* @param maxLevel Max logging level.
* @return New console appender.
*/
private Appender createConsoleAppender(Level maxLevel) {
String fmt = "[%d{ABSOLUTE}][%-5p][%t][%c{1}] %m%n";
// Configure output that should go to System.out
Appender app = new ConsoleAppender(new PatternLayout(fmt), ConsoleAppender.SYSTEM_OUT);
LevelRangeFilter lvlFilter = new LevelRangeFilter();
lvlFilter.setLevelMin(Level.TRACE);
lvlFilter.setLevelMax(maxLevel);
app.addFilter(lvlFilter);
return app;
}
/**
* Adds file appender.
*
* @param a Appender.
*/
public static void addAppender(FileAppender a) {
A.notNull(a, "a");
fileAppenders.add(a);
}
/**
* Removes file appender.
*
* @param a Appender.
*/
public static void removeAppender(FileAppender a) {
A.notNull(a, "a");
fileAppenders.remove(a);
}
/** {@inheritDoc} */
@Override public void setNodeId(UUID nodeId) {
A.notNull(nodeId, "nodeId");
this.nodeId = nodeId;
updateFilePath(new Log4jNodeIdFilePath(nodeId));
}
/** {@inheritDoc} */
@Override public UUID getNodeId() {
return nodeId;
}
/**
* Gets files for all registered file appenders.
*
* @return List of files.
*/
public static Collection<String> logFiles() {
Collection<String> res = new ArrayList<>(fileAppenders.size());
for (FileAppender a : fileAppenders)
res.add(a.getFile());
return res;
}
/**
* Gets {@link org.apache.ignite.IgniteLogger} wrapper around log4j logger for the given
* category. If category is {@code null}, then root logger is returned. If
* category is an instance of {@link Class} then {@code (Class)ctgr).getName()}
* is used as category name.
*
* @param ctgr {@inheritDoc}
* @return {@link org.apache.ignite.IgniteLogger} wrapper around log4j logger.
*/
@Override public Log4JLogger getLogger(Object ctgr) {
return new Log4JLogger(ctgr == null ? Logger.getRootLogger() :
ctgr instanceof Class ? Logger.getLogger(((Class<?>)ctgr).getName()) :
Logger.getLogger(ctgr.toString()));
}
/** {@inheritDoc} */
@Override public void trace(String msg) {
if (!impl.isTraceEnabled())
warning("Logging at TRACE level without checking if TRACE level is enabled: " + msg);
impl.trace(msg);
}
/** {@inheritDoc} */
@Override public void debug(String msg) {
if (!impl.isDebugEnabled())
warning("Logging at DEBUG level without checking if DEBUG level is enabled: " + msg);
impl.debug(msg);
}
/** {@inheritDoc} */
@Override public void info(String msg) {
if (!impl.isInfoEnabled())
warning("Logging at INFO level without checking if INFO level is enabled: " + msg);
impl.info(msg);
}
/** {@inheritDoc} */
@Override public void warning(String msg) {
impl.warn(msg);
}
/** {@inheritDoc} */
@Override public void warning(String msg, @Nullable Throwable e) {
impl.warn(msg, e);
}
/** {@inheritDoc} */
@Override public void error(String msg) {
impl.error(msg);
}
/** {@inheritDoc} */
@Override public void error(String msg, @Nullable Throwable e) {
impl.error(msg, e);
}
/** {@inheritDoc} */
@Override public boolean isTraceEnabled() {
return impl.isTraceEnabled();
}
/** {@inheritDoc} */
@Override public boolean isDebugEnabled() {
return impl.isDebugEnabled();
}
/** {@inheritDoc} */
@Override public boolean isInfoEnabled() {
return impl.isInfoEnabled();
}
/** {@inheritDoc} */
@Override public boolean isQuiet() {
return quiet;
}
/** {@inheritDoc} */
@Override public String toString() {
return S.toString(Log4JLogger.class, this);
}
/** {@inheritDoc} */
@Override public void updateFilePath(IgniteClosure<String, String> filePathClos) {
A.notNull(filePathClos, "filePathClos");
for (FileAppender a : fileAppenders) {
if (a instanceof Log4jFileAware) {
((Log4jFileAware)a).updateFilePath(filePathClos);
a.activateOptions();
}
}
}
}