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 private static final String SYSTEM_OUT = "system.out";
37
38 private static final String SYSTEM_ERR = "system.err";
39
40
41 protected static final String DEFAULT_DATE_TIME_FORMAT = "yyyy/MM/dd HH:mm:ss:SSS zzz";
42
43
44 protected static final String SYSTEM_PREFIX = "org.apache.logging.log4j.simplelog.";
45
46 private final PropertiesUtil props;
47
48
49 private final boolean showLogName;
50
51
52
53
54
55 private final boolean showShortName;
56
57 private final boolean showDateTime;
58
59 private final boolean showContextMap;
60
61 private final String dateTimeFormat;
62
63 private final Level defaultLevel;
64
65 private final PrintStream stream;
66
67 private final LoggerRegistry<ExtendedLogger> loggerRegistry = new LoggerRegistry<>();
68
69 public SimpleLoggerContext() {
70 props = new PropertiesUtil("log4j2.simplelog.properties");
71
72 showContextMap = props.getBooleanProperty(SYSTEM_PREFIX + "showContextMap", false);
73 showLogName = props.getBooleanProperty(SYSTEM_PREFIX + "showlogname", false);
74 showShortName = props.getBooleanProperty(SYSTEM_PREFIX + "showShortLogname", true);
75 showDateTime = props.getBooleanProperty(SYSTEM_PREFIX + "showdatetime", false);
76 final String lvl = props.getStringProperty(SYSTEM_PREFIX + "level");
77 defaultLevel = Level.toLevel(lvl, Level.ERROR);
78
79 dateTimeFormat = showDateTime ? props.getStringProperty(SimpleLoggerContext.SYSTEM_PREFIX + "dateTimeFormat",
80 DEFAULT_DATE_TIME_FORMAT) : null;
81
82 final String fileName = props.getStringProperty(SYSTEM_PREFIX + "logFile", SYSTEM_ERR);
83 PrintStream ps;
84 if (SYSTEM_ERR.equalsIgnoreCase(fileName)) {
85 ps = System.err;
86 } else if (SYSTEM_OUT.equalsIgnoreCase(fileName)) {
87 ps = System.out;
88 } else {
89 try {
90 final FileOutputStream os = new FileOutputStream(fileName);
91 ps = new PrintStream(os);
92 } catch (final FileNotFoundException fnfe) {
93 ps = System.err;
94 }
95 }
96 this.stream = ps;
97 }
98
99 @Override
100 public ExtendedLogger getLogger(final String name) {
101 return getLogger(name, null);
102 }
103
104 @Override
105 public ExtendedLogger getLogger(final String name, final MessageFactory messageFactory) {
106
107 final ExtendedLogger extendedLogger = loggerRegistry.getLogger(name, messageFactory);
108 if (extendedLogger != null) {
109 AbstractLogger.checkMessageFactory(extendedLogger, messageFactory);
110 return extendedLogger;
111 }
112 final SimpleLogger simpleLogger = new SimpleLogger(name, defaultLevel, showLogName, showShortName, showDateTime,
113 showContextMap, dateTimeFormat, messageFactory, props, stream);
114 loggerRegistry.putIfAbsent(name, messageFactory, simpleLogger);
115 return loggerRegistry.getLogger(name, messageFactory);
116 }
117
118 @Override
119 public boolean hasLogger(final String name) {
120 return false;
121 }
122
123 @Override
124 public boolean hasLogger(final String name, final MessageFactory messageFactory) {
125 return false;
126 }
127
128 @Override
129 public boolean hasLogger(final String name, final Class<? extends MessageFactory> messageFactoryClass) {
130 return false;
131 }
132
133 @Override
134 public Object getExternalContext() {
135 return null;
136 }
137
138 }