1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.jul;
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.spi.LoggerContextListener;
20 import ch.qos.logback.core.spi.ContextAwareBase;
21 import ch.qos.logback.core.spi.LifeCycle;
22
23 import java.util.Enumeration;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
27 import java.util.logging.LogManager;
28
29
30
31
32
33 public class LevelChangePropagator extends ContextAwareBase implements LoggerContextListener, LifeCycle {
34
35 private Set<java.util.logging.Logger> julLoggerSet = new HashSet<java.util.logging.Logger>();
36 boolean isStarted = false;
37 boolean resetJUL = false;
38
39 public void setResetJUL(boolean resetJUL) {
40 this.resetJUL = resetJUL;
41 }
42
43 public boolean isResetResistant() {
44 return false;
45 }
46
47 public void onStart(LoggerContext context) {
48 }
49
50 public void onReset(LoggerContext context) {
51 }
52
53 public void onStop(LoggerContext context) {
54 }
55
56 public void onLevelChange(Logger logger, Level level) {
57 propagate(logger, level);
58 }
59
60 private void propagate(Logger logger, Level level) {
61 addInfo("Propagating " + level + " level on " + logger + " onto the JUL framework");
62 java.util.logging.Logger julLogger = JULHelper.asJULLogger(logger);
63
64
65 julLoggerSet.add(julLogger);
66 java.util.logging.Level julLevel = JULHelper.asJULLevel(level);
67 julLogger.setLevel(julLevel);
68 }
69
70 public void resetJULLevels() {
71 LogManager lm = LogManager.getLogManager();
72
73 Enumeration<String> e = lm.getLoggerNames();
74 while (e.hasMoreElements()) {
75 String loggerName = e.nextElement();
76 java.util.logging.Logger julLogger = lm.getLogger(loggerName);
77 if (JULHelper.isRegularNonRootLogger(julLogger) && julLogger.getLevel() != null) {
78 addInfo("Setting level of jul logger [" + loggerName + "] to null");
79 julLogger.setLevel(null);
80 }
81 }
82 }
83
84 private void propagateExistingLoggerLevels() {
85 LoggerContext loggerContext = (LoggerContext) context;
86 List<Logger> loggerList = loggerContext.getLoggerList();
87 for (Logger l : loggerList) {
88 if (l.getLevel() != null) {
89 propagate(l, l.getLevel());
90 }
91 }
92 }
93
94 public void start() {
95 if (resetJUL) {
96 resetJULLevels();
97 }
98 propagateExistingLoggerLevels();
99
100 isStarted = true;
101 }
102
103 public void stop() {
104 isStarted = false;
105 }
106
107 public boolean isStarted() {
108 return isStarted;
109 }
110 }