1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.simple;
18
19 import java.io.FileNotFoundException;
20 import java.io.FileOutputStream;
21 import java.io.PrintStream;
22
23 import org.apache.logging.log4j.Level;
24 import org.apache.logging.log4j.message.MessageFactory;
25 import org.apache.logging.log4j.spi.AbstractLogger;
26 import org.apache.logging.log4j.spi.ExtendedLogger;
27 import org.apache.logging.log4j.spi.LoggerContext;
28 import org.apache.logging.log4j.spi.LoggerRegistry;
29 import org.apache.logging.log4j.util.PropertiesUtil;
30
31
32
33
34 public class SimpleLoggerContext implements LoggerContext {
35
36
37 protected static final String DEFAULT_DATE_TIME_FORMAT = "yyyy/MM/dd HH:mm:ss:SSS zzz";
38
39
40 protected static final String SYSTEM_PREFIX = "org.apache.logging.log4j.simplelog.";
41
42 private final PropertiesUtil props;
43
44
45 private final boolean showLogName;
46
47
48
49
50
51 private final boolean showShortName;
52
53 private final boolean showDateTime;
54
55 private final boolean showContextMap;
56
57 private final String dateTimeFormat;
58
59 private final Level defaultLevel;
60
61 private final PrintStream stream;
62
63 private final LoggerRegistry<ExtendedLogger> loggerRegistry = new LoggerRegistry<>();
64
65 public SimpleLoggerContext() {
66 props = new PropertiesUtil("log4j2.simplelog.properties");
67
68 showContextMap = props.getBooleanProperty(SYSTEM_PREFIX + "showContextMap", false);
69 showLogName = props.getBooleanProperty(SYSTEM_PREFIX + "showlogname", false);
70 showShortName = props.getBooleanProperty(SYSTEM_PREFIX + "showShortLogname", true);
71 showDateTime = props.getBooleanProperty(SYSTEM_PREFIX + "showdatetime", false);
72 final String lvl = props.getStringProperty(SYSTEM_PREFIX + "level");
73 defaultLevel = Level.toLevel(lvl, Level.ERROR);
74
75 dateTimeFormat = showDateTime ? props.getStringProperty(SimpleLoggerContext.SYSTEM_PREFIX + "dateTimeFormat",
76 DEFAULT_DATE_TIME_FORMAT) : null;
77
78 final String fileName = props.getStringProperty(SYSTEM_PREFIX + "logFile", "system.err");
79 PrintStream ps;
80 if ("system.err".equalsIgnoreCase(fileName)) {
81 ps = System.err;
82 } else if ("system.out".equalsIgnoreCase(fileName)) {
83 ps = System.out;
84 } else {
85 try {
86 final FileOutputStream os = new FileOutputStream(fileName);
87 ps = new PrintStream(os);
88 } catch (final FileNotFoundException fnfe) {
89 ps = System.err;
90 }
91 }
92 this.stream = ps;
93 }
94
95 @Override
96 public ExtendedLogger getLogger(final String name) {
97 return getLogger(name, null);
98 }
99
100 @Override
101 public ExtendedLogger getLogger(final String name, final MessageFactory messageFactory) {
102
103 final ExtendedLogger extendedLogger = loggerRegistry.getLogger(name, messageFactory);
104 if (extendedLogger != null) {
105 AbstractLogger.checkMessageFactory(extendedLogger, messageFactory);
106 return extendedLogger;
107 }
108 final SimpleLogger simpleLogger = new SimpleLogger(name, defaultLevel, showLogName, showShortName, showDateTime,
109 showContextMap, dateTimeFormat, messageFactory, props, stream);
110 loggerRegistry.putIfAbsent(name, messageFactory, simpleLogger);
111 return loggerRegistry.getLogger(name, messageFactory);
112 }
113
114 @Override
115 public boolean hasLogger(final String name) {
116 return false;
117 }
118
119 @Override
120 public boolean hasLogger(final String name, final MessageFactory messageFactory) {
121 return false;
122 }
123
124 @Override
125 public boolean hasLogger(final String name, final Class<? extends MessageFactory> messageFactoryClass) {
126 return false;
127 }
128
129 @Override
130 public Object getExternalContext() {
131 return null;
132 }
133
134 }