1
1
/*
2
- * Copyright 2012-2017 the original author or authors.
2
+ * Copyright 2012-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
31
31
*/
32
32
public class DeferredLog implements Log {
33
33
34
- private List <Line > lines = new ArrayList <>();
34
+ private volatile Log destination ;
35
+
36
+ private final List <Line > lines = new ArrayList <>();
35
37
36
38
@ Override
37
39
public boolean isTraceEnabled () {
38
- return true ;
40
+ synchronized (this .lines ) {
41
+ return (this .destination != null ) ? this .destination .isTraceEnabled () : true ;
42
+ }
39
43
}
40
44
41
45
@ Override
42
46
public boolean isDebugEnabled () {
43
- return true ;
47
+ synchronized (this .lines ) {
48
+ return (this .destination != null ) ? this .destination .isDebugEnabled () : true ;
49
+ }
44
50
}
45
51
46
52
@ Override
47
53
public boolean isInfoEnabled () {
48
- return true ;
54
+ synchronized (this .lines ) {
55
+ return (this .destination != null ) ? this .destination .isInfoEnabled () : true ;
56
+ }
49
57
}
50
58
51
59
@ Override
52
60
public boolean isWarnEnabled () {
53
- return true ;
61
+ synchronized (this .lines ) {
62
+ return (this .destination != null ) ? this .destination .isWarnEnabled () : true ;
63
+ }
54
64
}
55
65
56
66
@ Override
57
67
public boolean isErrorEnabled () {
58
- return true ;
68
+ synchronized (this .lines ) {
69
+ return (this .destination != null ) ? this .destination .isErrorEnabled () : true ;
70
+ }
59
71
}
60
72
61
73
@ Override
62
74
public boolean isFatalEnabled () {
63
- return true ;
75
+ synchronized (this .lines ) {
76
+ return (this .destination != null ) ? this .destination .isFatalEnabled () : true ;
77
+ }
64
78
}
65
79
66
80
@ Override
@@ -124,31 +138,106 @@ public void fatal(Object message, Throwable t) {
124
138
}
125
139
126
140
private void log (LogLevel level , Object message , Throwable t ) {
127
- this .lines .add (new Line (level , message , t ));
141
+ synchronized (this .lines ) {
142
+ if (this .destination != null ) {
143
+ logTo (this .destination , level , message , t );
144
+ }
145
+ this .lines .add (new Line (level , message , t ));
146
+ }
128
147
}
129
148
149
+ /**
150
+ * Switch from deferred logging to immediate logging to the specified destination.
151
+ * @param destination the new log destination
152
+ */
153
+ public void switchTo (Class <?> destination ) {
154
+ switchTo (LogFactory .getLog (destination ));
155
+ }
156
+
157
+ /**
158
+ * Switch from deferred logging to immediate logging to the specified destination.
159
+ * @param destination the new log destination
160
+ */
161
+ public void switchTo (Log destination ) {
162
+ synchronized (this .lines ) {
163
+ replayTo (destination );
164
+ this .destination = destination ;
165
+ }
166
+ }
167
+
168
+ /**
169
+ * Replay deferred logging to the specified destination.
170
+ * @param destination the destination for the deferred log messages
171
+ */
130
172
public void replayTo (Class <?> destination ) {
131
173
replayTo (LogFactory .getLog (destination ));
132
174
}
133
175
176
+ /**
177
+ * Replay deferred logging to the specified destination.
178
+ * @param destination the destination for the deferred log messages
179
+ */
134
180
public void replayTo (Log destination ) {
135
- for (Line line : this .lines ) {
136
- line .replayTo (destination );
181
+ synchronized (this .lines ) {
182
+ for (Line line : this .lines ) {
183
+ logTo (destination , line .getLevel (), line .getMessage (),
184
+ line .getThrowable ());
185
+ }
186
+ this .lines .clear ();
137
187
}
138
- this .lines .clear ();
139
188
}
140
189
190
+ /**
191
+ * Replay from a source log to a destination log when the source is deferred.
192
+ * @param source the source logger
193
+ * @param destination the destination logger class
194
+ * @return the destination
195
+ * @deprecated since 2.1.0 in favor of {@link #switchTo(Class)}
196
+ */
197
+ @ Deprecated
141
198
public static Log replay (Log source , Class <?> destination ) {
142
199
return replay (source , LogFactory .getLog (destination ));
143
200
}
144
201
202
+ /**
203
+ * Replay from a source log to a destination log when the source is deferred.
204
+ * @param source the source logger
205
+ * @param destination the destination logger
206
+ * @return the destination
207
+ * @deprecated since 2.1.0 in favor of {@link #switchTo(Log)}
208
+ */
209
+ @ Deprecated
145
210
public static Log replay (Log source , Log destination ) {
146
211
if (source instanceof DeferredLog ) {
147
212
((DeferredLog ) source ).replayTo (destination );
148
213
}
149
214
return destination ;
150
215
}
151
216
217
+ private static void logTo (Log log , LogLevel level , Object message ,
218
+ Throwable throwable ) {
219
+ switch (level ) {
220
+ case TRACE :
221
+ log .trace (message , throwable );
222
+ return ;
223
+ case DEBUG :
224
+ log .debug (message , throwable );
225
+ return ;
226
+ case INFO :
227
+ log .info (message , throwable );
228
+ return ;
229
+ case WARN :
230
+ log .warn (message , throwable );
231
+ return ;
232
+ case ERROR :
233
+ log .error (message , throwable );
234
+ return ;
235
+ case FATAL :
236
+ log .fatal (message , throwable );
237
+ return ;
238
+ }
239
+ }
240
+
152
241
private static class Line {
153
242
154
243
private final LogLevel level ;
@@ -163,27 +252,16 @@ private static class Line {
163
252
this .throwable = throwable ;
164
253
}
165
254
166
- public void replayTo (Log log ) {
167
- switch (this .level ) {
168
- case TRACE :
169
- log .trace (this .message , this .throwable );
170
- return ;
171
- case DEBUG :
172
- log .debug (this .message , this .throwable );
173
- return ;
174
- case INFO :
175
- log .info (this .message , this .throwable );
176
- return ;
177
- case WARN :
178
- log .warn (this .message , this .throwable );
179
- return ;
180
- case ERROR :
181
- log .error (this .message , this .throwable );
182
- return ;
183
- case FATAL :
184
- log .fatal (this .message , this .throwable );
185
- return ;
186
- }
255
+ public LogLevel getLevel () {
256
+ return this .level ;
257
+ }
258
+
259
+ public Object getMessage () {
260
+ return this .message ;
261
+ }
262
+
263
+ public Throwable getThrowable () {
264
+ return this .throwable ;
187
265
}
188
266
189
267
}
0 commit comments