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