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.PrintStream;
20 import java.text.DateFormat;
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23 import java.util.Map;
24
25 import org.apache.logging.log4j.Level;
26 import org.apache.logging.log4j.Marker;
27 import org.apache.logging.log4j.ThreadContext;
28 import org.apache.logging.log4j.message.Message;
29 import org.apache.logging.log4j.message.MessageFactory;
30 import org.apache.logging.log4j.spi.AbstractLogger;
31 import org.apache.logging.log4j.util.PropertiesUtil;
32 import org.apache.logging.log4j.util.Strings;
33
34
35
36
37 public class SimpleLogger extends AbstractLogger {
38
39 private static final long serialVersionUID = 1L;
40
41 private static final char SPACE = ' ';
42
43
44
45
46
47
48
49 private DateFormat dateFormatter;
50
51 private Level level;
52
53 private final boolean showDateTime;
54
55 private final boolean showContextMap;
56
57 private PrintStream stream;
58
59 private final String logName;
60
61 public SimpleLogger(final String name, final Level defaultLevel, final boolean showLogName,
62 final boolean showShortLogName, final boolean showDateTime, final boolean showContextMap,
63 final String dateTimeFormat, final MessageFactory messageFactory, final PropertiesUtil props,
64 final PrintStream stream) {
65 super(name, messageFactory);
66 final String lvl = props.getStringProperty(SimpleLoggerContext.SYSTEM_PREFIX + name + ".level");
67 this.level = Level.toLevel(lvl, defaultLevel);
68 if (showShortLogName) {
69 final int index = name.lastIndexOf(".");
70 if (index > 0 && index < name.length()) {
71 this.logName = name.substring(index + 1);
72 } else {
73 this.logName = name;
74 }
75 } else if (showLogName) {
76 this.logName = name;
77 } else {
78 this.logName = null;
79 }
80 this.showDateTime = showDateTime;
81 this.showContextMap = showContextMap;
82 this.stream = stream;
83
84 if (showDateTime) {
85 try {
86 this.dateFormatter = new SimpleDateFormat(dateTimeFormat);
87 } catch (final IllegalArgumentException e) {
88
89 this.dateFormatter = new SimpleDateFormat(SimpleLoggerContext.DEFAULT_DATE_TIME_FORMAT);
90 }
91 }
92 }
93
94 @Override
95 public Level getLevel() {
96 return level;
97 }
98
99 @Override
100 public boolean isEnabled(final Level testLevel, final Marker marker, final Message msg, final Throwable t) {
101 return this.level.intLevel() >= testLevel.intLevel();
102 }
103
104 @Override
105 public boolean isEnabled(final Level testLevel, final Marker marker, final Object msg, final Throwable t) {
106 return this.level.intLevel() >= testLevel.intLevel();
107 }
108
109 @Override
110 public boolean isEnabled(final Level testLevel, final Marker marker, final String msg) {
111 return this.level.intLevel() >= testLevel.intLevel();
112 }
113
114 @Override
115 public boolean isEnabled(final Level testLevel, final Marker marker, final String msg, final Object... p1) {
116 return this.level.intLevel() >= testLevel.intLevel();
117 }
118
119 @Override
120 public boolean isEnabled(final Level testLevel, final Marker marker, final String msg, final Throwable t) {
121 return this.level.intLevel() >= testLevel.intLevel();
122 }
123
124 @Override
125 public void logMessage(final String fqcn, final Level mgsLevel, final Marker marker, final Message msg,
126 final Throwable throwable) {
127 final StringBuilder sb = new StringBuilder();
128
129 if (showDateTime) {
130 final Date now = new Date();
131 String dateText;
132 synchronized (dateFormatter) {
133 dateText = dateFormatter.format(now);
134 }
135 sb.append(dateText);
136 sb.append(SPACE);
137 }
138
139 sb.append(mgsLevel.toString());
140 sb.append(SPACE);
141 if (Strings.isNotEmpty(logName)) {
142 sb.append(logName);
143 sb.append(SPACE);
144 }
145 sb.append(msg.getFormattedMessage());
146 if (showContextMap) {
147 final Map<String, String> mdc = ThreadContext.getImmutableContext();
148 if (mdc.size() > 0) {
149 sb.append(SPACE);
150 sb.append(mdc.toString());
151 sb.append(SPACE);
152 }
153 }
154 final Object[] params = msg.getParameters();
155 Throwable t;
156 if (throwable == null && params != null && params.length > 0
157 && params[params.length - 1] instanceof Throwable) {
158 t = (Throwable) params[params.length - 1];
159 } else {
160 t = throwable;
161 }
162 stream.println(sb.toString());
163 if (t != null) {
164 stream.print(SPACE);
165 t.printStackTrace(stream);
166 }
167 }
168
169 public void setLevel(final Level level) {
170 if (level != null) {
171 this.level = level;
172 }
173 }
174
175 public void setStream(final PrintStream stream) {
176 this.stream = stream;
177 }
178
179 }