1 package org.djutils.logger;
2
3 import java.util.Collection;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.LinkedHashMap;
7 import java.util.Map;
8 import java.util.Objects;
9 import java.util.concurrent.ConcurrentHashMap;
10 import java.util.function.BooleanSupplier;
11
12 import org.slf4j.LoggerFactory;
13 import org.slf4j.spi.CallerBoundaryAware;
14 import org.slf4j.spi.LoggingEventBuilder;
15
16 import ch.qos.logback.classic.Level;
17 import ch.qos.logback.classic.Logger;
18 import ch.qos.logback.classic.LoggerContext;
19 import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
20 import ch.qos.logback.classic.spi.ILoggingEvent;
21 import ch.qos.logback.core.Appender;
22 import ch.qos.logback.core.rolling.RollingFileAppender;
23 import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 @SuppressWarnings("checkstyle:needbraces")
49 public final class CategoryLogger
50 {
51
52 private static volatile boolean initialized = false;
53
54
55 private static final LoggerContext CTX = (LoggerContext) LoggerFactory.getILoggerFactory();
56
57
58 public static final String DEFAULT_PATTERN = "%date{HH:mm:ss} %-5level %-6logger{0} %class.%method:%line - %msg%n";
59
60
61 private static String defaultPattern = DEFAULT_PATTERN;
62
63
64 private static Level defaultLevel = Level.INFO;
65
66
67 private static final Map<LogCategory, CategoryConfig> CATEGORY_CFG = new LinkedHashMap<>();
68
69
70 private static final Map<LogCategory, CategoryState> CATEGORY_STATE = new LinkedHashMap<>();
71
72
73 private static final Map<String, CategoryAppenderFactory> APPENDER_FACTORIES = new LinkedHashMap<>();
74
75
76 private static final Map<LogCategory, DelegateLogger> DELEGATES = new ConcurrentHashMap<>();
77
78
79 public static final LogCategory CAT_ALWAYS = new LogCategory("ALWAYS");
80
81
82 private static final DelegateLogger BASE_DELEGATE = new DelegateLogger(LoggerFactory.getLogger(CAT_ALWAYS.toString()));
83
84
85 private static final DelegateLogger NO_LOGGER = new DelegateLogger(null, CategoryLogger.DelegateLogger.class, false);
86
87
88 private CategoryLogger()
89 {
90
91 }
92
93
94
95
96
97
98
99
100 private static void ensureInit()
101 {
102 if (initialized)
103 return;
104 synchronized (CategoryLogger.class)
105 {
106 if (initialized)
107 return;
108
109 initialized = true;
110 addLogCategory(CAT_ALWAYS);
111 setLogLevel(CAT_ALWAYS, Level.TRACE);
112 addLogCategory(LogCategory.ALL);
113 setLogLevel(LogCategory.ALL, defaultLevel);
114 }
115 }
116
117
118
119
120
121
122 private static void wireCategoryLogger(final LogCategory category, final CategoryConfig cfg)
123 {
124 Logger logger = getOrCreateLogger(category);
125 logger.setAdditive(false);
126 logger.setLevel(cfg.level);
127 CategoryState st = new CategoryState(logger);
128 CATEGORY_STATE.put(category, st);
129
130
131 for (CategoryAppenderFactory f : APPENDER_FACTORIES.values())
132 {
133 Appender<ILoggingEvent> app = f.create(f.id(), category, cfg.pattern, CTX);
134 app.start();
135 logger.addAppender(app);
136 st.appendersByFactoryId.put(f.id(), app);
137 }
138
139
140 if (APPENDER_FACTORIES.isEmpty())
141 {
142 CategoryAppenderFactory fallback = new ConsoleAppenderFactory("CONSOLE");
143 APPENDER_FACTORIES.putIfAbsent("CONSOLE", fallback);
144 Appender<ILoggingEvent> app = fallback.create("CONSOLE", category, cfg.pattern, CTX);
145 app.start();
146 logger.addAppender(app);
147 st.appendersByFactoryId.put("CONSOLE", app);
148 }
149 }
150
151
152
153
154
155 private static void rebuildCategoryAppenders(final LogCategory category)
156 {
157 CategoryState st = CATEGORY_STATE.get(category);
158 CategoryConfig cfg = CATEGORY_CFG.get(category);
159 if (st == null || cfg == null)
160 return;
161
162
163 st.appendersByFactoryId.forEach((id, app) ->
164 { st.logger.detachAppender(app); safeStop(app); });
165 st.appendersByFactoryId.clear();
166
167
168 for (CategoryAppenderFactory f : APPENDER_FACTORIES.values())
169 {
170 Appender<ILoggingEvent> app = f.create(f.id(), category, cfg.pattern, CTX);
171 app.start();
172 st.logger.addAppender(app);
173 st.appendersByFactoryId.put(f.id(), app);
174 }
175 }
176
177
178
179
180
181
182 private static Logger getOrCreateLogger(final LogCategory category)
183 {
184 Logger logger = CTX.getLogger(category.toString());
185 return logger;
186 }
187
188
189
190
191
192 private static void safeStop(final Appender<ILoggingEvent> appender)
193 {
194 try
195 {
196 appender.stop();
197 }
198 catch (RuntimeException ignore)
199 {
200 }
201 }
202
203
204
205
206
207
208
209
210
211 public static DelegateLogger always()
212 {
213 ensureInit();
214 return BASE_DELEGATE;
215 }
216
217
218
219
220
221
222 public static DelegateLogger when(final boolean condition)
223 {
224 ensureInit();
225 return condition ? BASE_DELEGATE : NO_LOGGER;
226 }
227
228
229
230
231
232
233 public static DelegateLogger when(final BooleanSupplier booleanSupplier)
234 {
235 return when(booleanSupplier.getAsBoolean());
236 }
237
238
239
240
241
242
243 public static DelegateLogger filter(final LogCategory category)
244 {
245 ensureInit();
246 return DELEGATES.getOrDefault(category, NO_LOGGER);
247 }
248
249
250
251
252
253
254 public static synchronized void addLogCategory(final LogCategory category)
255 {
256 ensureInit();
257 if (CATEGORY_CFG.containsKey(category))
258 return;
259 CategoryConfig cfg = new CategoryConfig(defaultLevel, defaultPattern);
260 CATEGORY_CFG.put(category, cfg);
261 org.slf4j.Logger slf = LoggerFactory.getLogger(category.toString());
262 var delegate = new DelegateLogger(slf);
263 DELEGATES.put(category, delegate);
264 wireCategoryLogger(category, cfg);
265 }
266
267
268
269
270
271
272 public static synchronized void removeLogCategory(final LogCategory category)
273 {
274 ensureInit();
275 CategoryState st = CATEGORY_STATE.remove(category);
276 CATEGORY_CFG.remove(category);
277 if (st != null)
278 {
279
280 Logger logger = st.logger;
281 st.appendersByFactoryId.values().forEach(app ->
282 { logger.detachAppender(app); safeStop(app); });
283
284 logger.setLevel(Level.OFF);
285 logger.setAdditive(false);
286 }
287 DELEGATES.remove(category);
288 }
289
290
291
292
293
294
295 public static Collection<Appender<ILoggingEvent>> getAppenders(final LogCategory category)
296 {
297 ensureInit();
298 var st = CATEGORY_STATE.get(category);
299 return st == null ? new HashSet<>() : st.appendersByFactoryId.values();
300 }
301
302
303
304
305
306
307 public static synchronized void setLogLevel(final LogCategory category, final Level level)
308 {
309 ensureInit();
310 addLogCategory(category);
311 CATEGORY_CFG.get(category).level = level;
312 Logger logger = CATEGORY_STATE.get(category).logger;
313 logger.setLevel(level);
314 }
315
316
317
318
319
320 public static synchronized void setLogLevelAll(final Level level)
321 {
322 ensureInit();
323 defaultLevel = level;
324 for (var cat : CATEGORY_CFG.keySet())
325 {
326 if (cat.equals(CAT_ALWAYS))
327 continue;
328 CATEGORY_CFG.get(cat).level = level;
329 CATEGORY_STATE.get(cat).logger.setLevel(level);
330 }
331 }
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364 public static synchronized void setPattern(final LogCategory category, final String pattern)
365 {
366 ensureInit();
367 addLogCategory(category);
368 CATEGORY_CFG.get(category).pattern = Objects.requireNonNull(pattern);
369
370 rebuildCategoryAppenders(category);
371 }
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403 public static synchronized void setPatternAll(final String pattern)
404 {
405 ensureInit();
406 defaultPattern = Objects.requireNonNull(pattern);
407 CATEGORY_CFG.replaceAll((c, cfg) ->
408 { cfg.pattern = pattern; return cfg; });
409 CATEGORY_CFG.keySet().forEach(CategoryLogger::rebuildCategoryAppenders);
410 }
411
412
413
414
415
416
417 public static synchronized void addAppender(final String id, final CategoryAppenderFactory factory)
418 {
419 ensureInit();
420 if (APPENDER_FACTORIES.containsKey(id))
421 throw new IllegalArgumentException("factory id exists: " + id);
422 APPENDER_FACTORIES.put(id, factory);
423
424 for (var e : CATEGORY_CFG.entrySet())
425 {
426 LogCategory cat = e.getKey();
427 CategoryConfig cfg = e.getValue();
428 CategoryState st = CATEGORY_STATE.get(cat);
429 Appender<ILoggingEvent> app = factory.create(id, cat, cfg.pattern, CTX);
430 app.start();
431 st.logger.addAppender(app);
432 st.appendersByFactoryId.put(id, app);
433 }
434 }
435
436
437
438
439
440 public static synchronized void removeAppender(final String id)
441 {
442 ensureInit();
443 if (APPENDER_FACTORIES.remove(id) == null)
444 return;
445 for (CategoryState st : CATEGORY_STATE.values())
446 {
447 Appender<ILoggingEvent> app = st.appendersByFactoryId.remove(id);
448 if (app != null)
449 {
450 st.logger.detachAppender(app);
451 safeStop(app);
452 }
453 }
454 }
455
456
457
458
459
460
461
462
463 private static final class CategoryConfig
464 {
465
466 private Level level;
467
468
469 private String pattern;
470
471
472
473
474
475
476 private CategoryConfig(final Level level, final String pattern)
477 {
478 this.level = Objects.requireNonNull(level);
479 this.pattern = Objects.requireNonNull(pattern);
480 }
481 }
482
483
484
485
486 private static final class CategoryState
487 {
488
489 private final Logger logger;
490
491
492 private final Map<String, Appender<ILoggingEvent>> appendersByFactoryId = new HashMap<>();
493
494
495
496
497
498 CategoryState(final Logger logger)
499 {
500 this.logger = logger;
501 }
502 }
503
504
505
506
507
508
509
510
511 public interface CategoryAppenderFactory
512 {
513
514
515
516
517 String id();
518
519
520
521
522
523
524
525
526
527 Appender<ILoggingEvent> create(String id, LogCategory category, String messageFormat, LoggerContext ctx);
528 }
529
530
531 public static final class ConsoleAppenderFactory implements CategoryAppenderFactory
532 {
533
534 private final String id;
535
536
537
538
539
540 public ConsoleAppenderFactory(final String id)
541 {
542 this.id = id;
543 }
544
545 @Override
546 public String id()
547 {
548 return this.id;
549 }
550
551 @Override
552 @SuppressWarnings("checkstyle:hiddenfield")
553 public Appender<ILoggingEvent> create(final String id, final LogCategory category, final String messageFormat,
554 final LoggerContext ctx)
555 {
556 PatternLayoutEncoder enc = new PatternLayoutEncoder();
557 enc.setContext(ctx);
558 enc.setPattern(messageFormat);
559 enc.start();
560
561 ch.qos.logback.core.ConsoleAppender<ILoggingEvent> app = new ch.qos.logback.core.ConsoleAppender<>();
562 app.setName(id + "@" + category.toString());
563 app.setContext(ctx);
564 app.setEncoder(enc);
565 return app;
566 }
567 }
568
569
570 public static final class RollingFileAppenderFactory implements CategoryAppenderFactory
571 {
572
573 private final String id;
574
575
576 private final String fileNamePattern;
577
578
579
580
581
582
583 public RollingFileAppenderFactory(final String id, final String fileNamePattern)
584 {
585 this.id = id;
586 this.fileNamePattern = fileNamePattern;
587 }
588
589 @Override
590 public String id()
591 {
592 return this.id;
593 }
594
595 @Override
596 @SuppressWarnings("checkstyle:hiddenfield")
597 public Appender<ILoggingEvent> create(final String id, final LogCategory category, final String messageFormat,
598 final LoggerContext ctx)
599 {
600 PatternLayoutEncoder enc = new PatternLayoutEncoder();
601 enc.setContext(ctx);
602 enc.setPattern(messageFormat);
603 enc.start();
604
605 RollingFileAppender<ILoggingEvent> file = new RollingFileAppender<>();
606 file.setName(id + "@" + category.toString());
607 file.setContext(ctx);
608 file.setEncoder(enc);
609
610 final TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<>();
611 policy.setContext(ctx);
612 policy.setParent(file);
613
614
615 final String effectivePattern = this.fileNamePattern.replace("%s", category.toString());
616 policy.setFileNamePattern(effectivePattern);
617
618 policy.start();
619 file.setRollingPolicy(policy);
620
621 return file;
622 }
623 }
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638 public static final class DelegateLogger
639 {
640
641 private final org.slf4j.Logger logger;
642
643
644 private final String boundaryFqcn;
645
646
647 private final boolean log;
648
649
650
651
652
653
654
655 private DelegateLogger(final org.slf4j.Logger slf4jLogger, final Class<?> callerBoundary, final boolean log)
656 {
657 this.logger = slf4jLogger;
658 this.boundaryFqcn = Objects.requireNonNull(callerBoundary).getName();
659 this.log = log;
660 }
661
662
663
664
665
666 private DelegateLogger(final org.slf4j.Logger slf4jLogger)
667 {
668 this(slf4jLogger, CategoryLogger.DelegateLogger.class, true);
669 }
670
671
672
673
674
675
676 public DelegateLogger when(final boolean condition)
677 {
678 if (condition)
679 return this;
680 return CategoryLogger.NO_LOGGER;
681 }
682
683
684
685
686
687
688 public DelegateLogger when(final BooleanSupplier supplier)
689 {
690 if (supplier.getAsBoolean())
691 return this;
692 return CategoryLogger.NO_LOGGER;
693 }
694
695
696
697
698
699
700 private LoggingEventBuilder withBoundary(final LoggingEventBuilder leb)
701 {
702 if (leb instanceof CallerBoundaryAware cba)
703 cba.setCallerBoundary(this.boundaryFqcn);
704 return leb;
705 }
706
707
708
709
710
711
712
713
714
715 public void trace(final Object object)
716 {
717 if (!this.log || !this.logger.isTraceEnabled())
718 return;
719 withBoundary(this.logger.atTrace()).log(object.toString());
720 }
721
722
723
724
725
726 public void trace(final String message)
727 {
728 if (!this.log || !this.logger.isTraceEnabled())
729 return;
730 withBoundary(this.logger.atTrace()).log(message);
731 }
732
733
734
735
736
737
738 public void trace(final String message, final Object... arguments)
739 {
740 if (!this.log || !this.logger.isTraceEnabled())
741 return;
742 withBoundary(this.logger.atTrace()).log(message, arguments);
743 }
744
745
746
747
748
749 public void trace(final Throwable throwable)
750 {
751 if (!this.log || !this.logger.isTraceEnabled())
752 return;
753 withBoundary(this.logger.atTrace()).setCause(throwable)
754 .log((() -> throwable.getClass().getSimpleName() + "(" + Objects.requireNonNullElse(throwable.getMessage(), "")
755 + ")"));
756 }
757
758
759
760
761
762
763 public void trace(final Throwable throwable, final String message)
764 {
765 if (!this.log || !this.logger.isTraceEnabled())
766 return;
767 withBoundary(this.logger.atTrace()).setCause(throwable).log(message);
768 }
769
770
771
772
773
774
775
776 public void trace(final Throwable throwable, final String message, final Object... arguments)
777 {
778 if (!this.log || !this.logger.isTraceEnabled())
779 return;
780 withBoundary(this.logger.atTrace()).setCause(throwable).log(message, arguments);
781 }
782
783
784
785
786
787
788
789
790
791 public void debug(final Object object)
792 {
793 if (!this.log || !this.logger.isDebugEnabled())
794 return;
795 withBoundary(this.logger.atDebug()).log(object.toString());
796 }
797
798
799
800
801
802 public void debug(final String message)
803 {
804 if (!this.log || !this.logger.isDebugEnabled())
805 return;
806 withBoundary(this.logger.atDebug()).log(message);
807 }
808
809
810
811
812
813
814 public void debug(final String message, final Object... arguments)
815 {
816 if (!this.log || !this.logger.isDebugEnabled())
817 return;
818 withBoundary(this.logger.atDebug()).log(message, arguments);
819 }
820
821
822
823
824
825 public void debug(final Throwable throwable)
826 {
827 if (!this.log || !this.logger.isDebugEnabled())
828 return;
829 withBoundary(this.logger.atDebug()).setCause(throwable)
830 .log((() -> throwable.getClass().getSimpleName() + "(" + Objects.requireNonNullElse(throwable.getMessage(), "")
831 + ")"));
832 }
833
834
835
836
837
838
839 public void debug(final Throwable throwable, final String message)
840 {
841 if (!this.log || !this.logger.isDebugEnabled())
842 return;
843 withBoundary(this.logger.atDebug()).setCause(throwable).log(message);
844 }
845
846
847
848
849
850
851
852 public void debug(final Throwable throwable, final String message, final Object... arguments)
853 {
854 if (!this.log || !this.logger.isDebugEnabled())
855 return;
856 withBoundary(this.logger.atDebug()).setCause(throwable).log(message, arguments);
857 }
858
859
860
861
862
863
864
865
866
867 public void info(final Object object)
868 {
869 if (!this.log || !this.logger.isInfoEnabled())
870 return;
871 withBoundary(this.logger.atInfo()).log(object.toString());
872 }
873
874
875
876
877
878 public void info(final String message)
879 {
880 if (!this.log || !this.logger.isInfoEnabled())
881 return;
882 withBoundary(this.logger.atInfo()).log(message);
883 }
884
885
886
887
888
889
890 public void info(final String message, final Object... arguments)
891 {
892 if (!this.log || !this.logger.isInfoEnabled())
893 return;
894 withBoundary(this.logger.atInfo()).log(message, arguments);
895 }
896
897
898
899
900
901 public void info(final Throwable throwable)
902 {
903 if (!this.log || !this.logger.isInfoEnabled())
904 return;
905 withBoundary(this.logger.atInfo()).setCause(throwable)
906 .log((() -> throwable.getClass().getSimpleName() + "(" + Objects.requireNonNullElse(throwable.getMessage(), "")
907 + ")"));
908 }
909
910
911
912
913
914
915 public void info(final Throwable throwable, final String message)
916 {
917 if (!this.log || !this.logger.isInfoEnabled())
918 return;
919 withBoundary(this.logger.atInfo()).setCause(throwable).log(message);
920 }
921
922
923
924
925
926
927
928 public void info(final Throwable throwable, final String message, final Object... arguments)
929 {
930 if (!this.log || !this.logger.isInfoEnabled())
931 return;
932 withBoundary(this.logger.atInfo()).setCause(throwable).log(message, arguments);
933 }
934
935
936
937
938
939
940
941
942
943 public void warn(final Object object)
944 {
945 if (!this.log || !this.logger.isWarnEnabled())
946 return;
947 withBoundary(this.logger.atWarn()).log(object.toString());
948 }
949
950
951
952
953
954 public void warn(final String message)
955 {
956 if (!this.log || !this.logger.isWarnEnabled())
957 return;
958 withBoundary(this.logger.atWarn()).log(message);
959 }
960
961
962
963
964
965
966 public void warn(final String message, final Object... arguments)
967 {
968 if (!this.log || !this.logger.isWarnEnabled())
969 return;
970 withBoundary(this.logger.atWarn()).log(message, arguments);
971 }
972
973
974
975
976
977 public void warn(final Throwable throwable)
978 {
979 if (!this.log || !this.logger.isWarnEnabled())
980 return;
981 withBoundary(this.logger.atWarn()).setCause(throwable)
982 .log((() -> throwable.getClass().getSimpleName() + "(" + Objects.requireNonNullElse(throwable.getMessage(), "")
983 + ")"));
984 }
985
986
987
988
989
990
991 public void warn(final Throwable throwable, final String message)
992 {
993 if (!this.log || !this.logger.isWarnEnabled())
994 return;
995 withBoundary(this.logger.atWarn()).setCause(throwable).log(message);
996 }
997
998
999
1000
1001
1002
1003
1004 public void warn(final Throwable throwable, final String message, final Object... arguments)
1005 {
1006 if (!this.log || !this.logger.isWarnEnabled())
1007 return;
1008 withBoundary(this.logger.atWarn()).setCause(throwable).log(message, arguments);
1009 }
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019 public void error(final Object object)
1020 {
1021 if (!this.log || !this.logger.isErrorEnabled())
1022 return;
1023 withBoundary(this.logger.atError()).log(object.toString());
1024 }
1025
1026
1027
1028
1029
1030 public void error(final String message)
1031 {
1032 if (!this.log || !this.logger.isErrorEnabled())
1033 return;
1034 withBoundary(this.logger.atError()).log(message);
1035 }
1036
1037
1038
1039
1040
1041
1042 public void error(final String message, final Object... arguments)
1043 {
1044 if (!this.log || !this.logger.isErrorEnabled())
1045 return;
1046 withBoundary(this.logger.atError()).log(message, arguments);
1047 }
1048
1049
1050
1051
1052
1053 public void error(final Throwable throwable)
1054 {
1055 if (!this.log || !this.logger.isErrorEnabled())
1056 return;
1057 withBoundary(this.logger.atError()).setCause(throwable)
1058 .log((() -> throwable.getClass().getSimpleName() + "(" + Objects.requireNonNullElse(throwable.getMessage(), "")
1059 + ")"));
1060 }
1061
1062
1063
1064
1065
1066
1067 public void error(final Throwable throwable, final String message)
1068 {
1069 if (!this.log || !this.logger.isErrorEnabled())
1070 return;
1071 withBoundary(this.logger.atError()).setCause(throwable).log(message);
1072 }
1073
1074
1075
1076
1077
1078
1079
1080 public void error(final Throwable throwable, final String message, final Object... arguments)
1081 {
1082 if (!this.log || !this.logger.isErrorEnabled())
1083 return;
1084 withBoundary(this.logger.atError()).setCause(throwable).log(message, arguments);
1085 }
1086 }
1087
1088 }