View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
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   * Propagate level changes made to a logback logger into the equivalent logger
31   * in j.u.l.
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          // prevent garbage collection of jul loggers whose level we set
64          // see also http://jira.qos.ch/browse//LOGBACK-404
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 }