1 package net.avcompris.commons3.utils;
2
3 import static com.google.common.base.Preconditions.checkNotNull;
4 import static com.google.common.base.Preconditions.checkState;
5
6 import javax.annotation.Nullable;
7
8 import org.apache.commons.logging.Log;
9
10 public abstract class LogFactory {
11
12 private static final ThreadLocal<String> CORRELATION_IDS = new ThreadLocal<>();
13
14 private static Level level = Level.INFO;
15
16 public static Log getLog(final Class<?> clazz) {
17
18 return new LogImpl(clazz);
19 }
20
21 public static void resetCorrelationId() {
22
23 CORRELATION_IDS.remove();
24 }
25
26 public static void setCorrelationId(final String correlationId) {
27
28 checkNotNull(correlationId, "correlationId");
29
30 final String oldCorrelationId = CORRELATION_IDS.get();
31
32 checkState(oldCorrelationId == null || correlationId.contentEquals(oldCorrelationId),
33 "correlationId: Expected: %s, but was: %s", oldCorrelationId, correlationId);
34
35 CORRELATION_IDS.set(correlationId);
36 }
37
38 private static String getCorrelationId() {
39
40 final String correlationId = CORRELATION_IDS.get();
41
42 if (correlationId == null) {
43
44 final RuntimeException e = new IllegalStateException("correlationId should have been set");
45
46 e.printStackTrace();
47
48
49
50 return "????????????????????????????????????";
51 }
52
53 return correlationId;
54 }
55
56 private static class LogImpl implements Log {
57
58 private final String prefix;
59
60 LogImpl(final Class<?> clazz) {
61
62 checkNotNull(clazz, "class");
63
64 prefix = clazz.getSimpleName() + ": ";
65 }
66
67 @Override
68 public void trace(@Nullable final Object message) {
69
70 if (!isTraceEnabled()) {
71 return;
72 }
73
74 System.out.println(LogUtils.format(getCorrelationId(), Level.TRACE, prefix + message));
75 }
76
77 @Override
78 public void trace(@Nullable final Object message, @Nullable final Throwable t) {
79
80 if (!isTraceEnabled()) {
81 return;
82 }
83
84 System.out.println(LogUtils.format(getCorrelationId(), Level.TRACE, prefix + message));
85
86 if (t != null) {
87
88 t.printStackTrace(System.out);
89 }
90 }
91
92 @Override
93 public void debug(@Nullable final Object message) {
94
95 if (!isDebugEnabled()) {
96 return;
97 }
98
99 System.out.println(LogUtils.format(getCorrelationId(), Level.DEBUG, prefix + message));
100 }
101
102 @Override
103 public void debug(@Nullable final Object message, @Nullable final Throwable t) {
104
105 if (!isDebugEnabled()) {
106 return;
107 }
108
109 System.out.println(LogUtils.format(getCorrelationId(), Level.DEBUG, prefix + message));
110
111 if (t != null) {
112
113 t.printStackTrace(System.out);
114 }
115 }
116
117 @Override
118 public void info(@Nullable final Object message) {
119
120 if (!isInfoEnabled()) {
121 return;
122 }
123
124 System.out.println(LogUtils.format(getCorrelationId(), Level.INFO, prefix + message));
125 }
126
127 @Override
128 public void info(@Nullable final Object message, @Nullable final Throwable t) {
129
130 if (!isInfoEnabled()) {
131 return;
132 }
133
134 System.out.println(LogUtils.format(getCorrelationId(), Level.INFO, prefix + message));
135
136 if (t != null) {
137
138 t.printStackTrace(System.out);
139 }
140 }
141
142 @Override
143 public void warn(@Nullable final Object message) {
144
145 if (!isWarnEnabled()) {
146 return;
147 }
148
149 System.out.println(LogUtils.format(getCorrelationId(), Level.WARN, prefix + message));
150 }
151
152 @Override
153 public void warn(@Nullable final Object message, @Nullable final Throwable t) {
154
155 if (!isWarnEnabled()) {
156 return;
157 }
158
159 System.out.println(LogUtils.format(getCorrelationId(), Level.WARN, prefix + message));
160
161 if (t != null) {
162
163 t.printStackTrace(System.out);
164 }
165 }
166
167 @Override
168 public void error(@Nullable final Object message) {
169
170 if (!isErrorEnabled()) {
171 return;
172 }
173
174 System.err.println(LogUtils.format(getCorrelationId(), Level.ERROR, prefix + message));
175 }
176
177 @Override
178 public void error(@Nullable final Object message, @Nullable final Throwable t) {
179
180 if (!isErrorEnabled()) {
181 return;
182 }
183
184 System.err.println(LogUtils.format(getCorrelationId(), Level.ERROR, prefix + message + ": " + t));
185
186 if (t != null) {
187
188 t.printStackTrace(System.err);
189 }
190 }
191
192 @Override
193 public void fatal(@Nullable final Object message) {
194
195 if (!isFatalEnabled()) {
196 return;
197 }
198
199 System.err.println(LogUtils.format(getCorrelationId(), Level.FATAL, prefix + message));
200 }
201
202 @Override
203 public void fatal(@Nullable final Object message, @Nullable final Throwable t) {
204
205 if (!isFatalEnabled()) {
206 return;
207 }
208
209 System.err.println(LogUtils.format(getCorrelationId(), Level.FATAL, prefix + message + ": " + t));
210
211 if (t != null) {
212
213 t.printStackTrace(System.err);
214 }
215 }
216
217 @Override
218 public boolean isDebugEnabled() {
219
220 return !getLevel().isGreaterThan(Level.DEBUG);
221 }
222
223 @Override
224 public boolean isErrorEnabled() {
225
226 return !getLevel().isGreaterThan(Level.ERROR);
227 }
228
229 @Override
230 public boolean isFatalEnabled() {
231
232 return !getLevel().isGreaterThan(Level.FATAL);
233 }
234
235 @Override
236 public boolean isInfoEnabled() {
237
238 return !getLevel().isGreaterThan(Level.INFO);
239 }
240
241 @Override
242 public boolean isTraceEnabled() {
243
244 return !getLevel().isGreaterThan(Level.TRACE);
245 }
246
247 @Override
248 public boolean isWarnEnabled() {
249
250 return !getLevel().isGreaterThan(Level.WARN);
251 }
252
253 private Level getLevel() {
254
255 return LogFactory.getLevel();
256 }
257 }
258
259 public static void setLevel(final Level level) {
260
261 LogFactory.level = checkNotNull(level, "level");
262 }
263
264 private static Level getLevel() {
265
266 return level;
267 }
268 }