13
13
*/
14
14
package ch .qos .logback .classic .pattern ;
15
15
16
- import ch .qos .logback .classic .ClassicConstants ;
17
- import ch .qos .logback .classic .ClassicTestConstants ;
18
- import ch .qos .logback .classic .Level ;
19
- import ch .qos .logback .classic .Logger ;
20
- import ch .qos .logback .classic .LoggerContext ;
16
+ import ch .qos .logback .classic .*;
21
17
import ch .qos .logback .classic .spi .ILoggingEvent ;
22
18
import ch .qos .logback .classic .spi .LoggingEvent ;
23
19
import ch .qos .logback .classic .util .LogbackMDCAdapter ;
31
27
import org .junit .jupiter .api .Test ;
32
28
import org .slf4j .MarkerFactory ;
33
29
30
+ import java .time .Instant ;
34
31
import java .util .ArrayList ;
35
32
import java .util .List ;
36
33
import java .util .regex .Pattern ;
37
34
38
- import static org .junit .jupiter .api .Assertions .assertEquals ;
39
- import static org .junit .jupiter .api .Assertions .assertTrue ;
40
- import static org .junit .jupiter .api .Assertions .fail ;
35
+ import static org .junit .jupiter .api .Assertions .*;
41
36
42
37
public class ConverterTest {
43
38
44
39
LoggerContext loggerContext = new LoggerContext ();
45
40
LogbackMDCAdapter logbackMDCAdapter = new LogbackMDCAdapter ();
46
41
Logger logger = loggerContext .getLogger (ConverterTest .class );
47
42
LoggingEvent le ;
48
- List <String > optionList = new ArrayList <String >();
43
+ // List<String> optionList = new ArrayList<String>();
49
44
50
45
// The LoggingEvent is massaged with an FCQN of FormattingConverter. This
51
46
// forces the returned caller information to match the caller stack for
@@ -77,7 +72,7 @@ public void testLineOfCaller() {
77
72
StringBuilder buf = new StringBuilder ();
78
73
converter .write (buf , le );
79
74
// the number below should be the line number of the previous line
80
- assertEquals ("78 " , buf .toString ());
75
+ assertEquals ("73 " , buf .toString ());
81
76
}
82
77
}
83
78
@@ -134,8 +129,7 @@ public void testException() {
134
129
135
130
{
136
131
DynamicConverter <ILoggingEvent > converter = new ThrowableProxyConverter ();
137
- this .optionList .add ("3" );
138
- converter .setOptionList (this .optionList );
132
+ converter .setOptionList (List .of ("3" ));
139
133
StringBuilder buf = new StringBuilder ();
140
134
converter .write (buf , le );
141
135
}
@@ -152,8 +146,7 @@ public void testLogger() {
152
146
153
147
{
154
148
ClassicConverter converter = new LoggerConverter ();
155
- this .optionList .add ("20" );
156
- converter .setOptionList (this .optionList );
149
+ converter .setOptionList (List .of ("20" ));
157
150
converter .start ();
158
151
StringBuilder buf = new StringBuilder ();
159
152
converter .write (buf , le );
@@ -162,9 +155,7 @@ public void testLogger() {
162
155
163
156
{
164
157
DynamicConverter <ILoggingEvent > converter = new LoggerConverter ();
165
- this .optionList .clear ();
166
- this .optionList .add ("0" );
167
- converter .setOptionList (this .optionList );
158
+ converter .setOptionList (List .of ("0" ));
168
159
converter .start ();
169
160
StringBuilder buf = new StringBuilder ();
170
161
converter .write (buf , le );
@@ -175,8 +166,7 @@ public void testLogger() {
175
166
@ Test
176
167
public void testVeryLongLoggerName () {
177
168
ClassicConverter converter = new LoggerConverter ();
178
- this .optionList .add ("5" );
179
- converter .setOptionList (this .optionList );
169
+ converter .setOptionList (List .of ("5" ));
180
170
converter .start ();
181
171
StringBuilder buf = new StringBuilder ();
182
172
@@ -239,9 +229,7 @@ public void testCallerData() {
239
229
240
230
{
241
231
DynamicConverter <ILoggingEvent > converter = new CallerDataConverter ();
242
- this .optionList .add ("2" );
243
- this .optionList .add ("XXX" );
244
- converter .setOptionList (this .optionList );
232
+ converter .setOptionList (List .of ("2" , "XXX" ));
245
233
converter .start ();
246
234
247
235
StringBuilder buf = new StringBuilder ();
@@ -255,11 +243,7 @@ public void testCallerData() {
255
243
256
244
{
257
245
DynamicConverter <ILoggingEvent > converter = new CallerDataConverter ();
258
- this .optionList .clear ();
259
- this .optionList .add ("2" );
260
- this .optionList .add ("XXX" );
261
- this .optionList .add ("*" );
262
- converter .setOptionList (this .optionList );
246
+ converter .setOptionList (List .of ("2" , "XXX" , "*" ));
263
247
converter .start ();
264
248
265
249
StringBuilder buf = new StringBuilder ();
@@ -272,11 +256,7 @@ public void testCallerData() {
272
256
}
273
257
{
274
258
DynamicConverter <ILoggingEvent > converter = new CallerDataConverter ();
275
- this .optionList .clear ();
276
- this .optionList .add ("2" );
277
- this .optionList .add ("XXX" );
278
- this .optionList .add ("+" );
279
- converter .setOptionList (this .optionList );
259
+ converter .setOptionList (List .of ("2" , "XXX" , "*" ));
280
260
converter .start ();
281
261
282
262
StringBuilder buf = new StringBuilder ();
@@ -290,11 +270,7 @@ public void testCallerData() {
290
270
291
271
{
292
272
DynamicConverter <ILoggingEvent > converter = new CallerDataConverter ();
293
- this .optionList .clear ();
294
- this .optionList .add ("2" );
295
- this .optionList .add ("XXX" );
296
- this .optionList .add ("*" );
297
- converter .setOptionList (this .optionList );
273
+ converter .setOptionList (List .of ("2" , "XXX" , "*" ));
298
274
converter .start ();
299
275
300
276
StringBuilder buf = new StringBuilder ();
@@ -307,16 +283,10 @@ public void testCallerData() {
307
283
308
284
{
309
285
DynamicConverter <ILoggingEvent > converter = new CallerDataConverter ();
310
- this .optionList .clear ();
311
-
286
+
312
287
boolean jdk18 = EnvUtil .isJDK18OrHigher ();
313
288
// jdk 18EA creates a different stack trace
314
- if (jdk18 ) {
315
- this .optionList .add ("2..3" );
316
- } else {
317
- this .optionList .add ("4..5" );
318
- }
319
- converter .setOptionList (this .optionList );
289
+ converter .setOptionList (jdk18 ? List .of ("2..3" ) : List .of ("4..5" ));
320
290
converter .start ();
321
291
322
292
StringBuilder buf = new StringBuilder ();
@@ -352,9 +322,7 @@ public void testRelativeTime() throws Exception {
352
322
@ Test
353
323
public void testSyslogStart () throws Exception {
354
324
DynamicConverter <ILoggingEvent > converter = new SyslogStartConverter ();
355
- this .optionList .clear ();
356
- this .optionList .add ("MAIL" );
357
- converter .setOptionList (this .optionList );
325
+ converter .setOptionList (List .of ("MAIL" ));
358
326
converter .start ();
359
327
360
328
ILoggingEvent event = makeLoggingEvent (null );
@@ -371,9 +339,7 @@ public void testMDCConverter() throws Exception {
371
339
logbackMDCAdapter .clear ();
372
340
logbackMDCAdapter .put ("someKey" , "someValue" );
373
341
MDCConverter converter = new MDCConverter ();
374
- this .optionList .clear ();
375
- this .optionList .add ("someKey" );
376
- converter .setOptionList (optionList );
342
+ converter .setOptionList (List .of ("someKey" ));
377
343
converter .start ();
378
344
379
345
ILoggingEvent event = makeLoggingEvent (null );
@@ -401,9 +367,7 @@ public void contextNameConverter() {
401
367
public void contextProperty () {
402
368
PropertyConverter converter = new PropertyConverter ();
403
369
converter .setContext (loggerContext );
404
- List <String > ol = new ArrayList <String >();
405
- ol .add ("k" );
406
- converter .setOptionList (ol );
370
+ converter .setOptionList (List .of ("k" ));
407
371
converter .start ();
408
372
loggerContext .setName ("aValue" );
409
373
loggerContext .putProperty ("k" , "v" );
@@ -427,4 +391,30 @@ public void testSequenceNumber() {
427
391
assertEquals ("123" , converter .convert (event ));
428
392
StatusPrinter .print (loggerContext );
429
393
}
394
+
395
+ @ Test
396
+ void dateConverterTest () {
397
+ dateConverterChecker (List .of ("STRICT" , "GMT" ), "2024-08-14T15:29:25,956" );
398
+ dateConverterChecker (List .of ("ISO8601" , "GMT" ), "2024-08-14 15:29:25,956" );
399
+ dateConverterChecker (List .of ("ISO8601" , "UTC" ), "2024-08-14 15:29:25,956" );
400
+ dateConverterChecker (List .of ("yyyy-MM-EE" , "UTC" , "fr-CH" ), "2024-08-mer." );
401
+
402
+ }
403
+
404
+ void dateConverterChecker (List <String > options , String expected ) {
405
+ DateConverter dateConverter = new DateConverter ();
406
+ dateConverter .setOptionList (options ) ;
407
+ dateConverter .setContext (loggerContext );
408
+ dateConverter .start ();
409
+
410
+ assertTrue (dateConverter .isStarted ());
411
+ LoggingEvent event = makeLoggingEvent (null );
412
+
413
+ // 2024-08-14T1Z:29:25,956 GMT
414
+ long millis = 1_723_649_365_956L ; //System.currentTimeMillis();
415
+ Instant now = Instant .ofEpochMilli (millis );
416
+ event .setInstant (now );
417
+ String result = dateConverter .convert (event );
418
+ assertEquals (expected , result );
419
+ }
430
420
}
0 commit comments