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.core.testUtil.RandomUtil;
20  
21  import org.junit.jupiter.api.BeforeEach;
22  import org.junit.jupiter.api.Test;
23  
24  import static org.junit.jupiter.api.Assertions.assertEquals;
25  import static org.junit.jupiter.api.Assertions.assertFalse;
26  import static org.junit.jupiter.api.Assertions.assertThrows;
27  
28  public class LevelChangePropagatorTest {
29      int rand = RandomUtil.getPositiveInt();
30      LoggerContext loggerContext = new LoggerContext();
31      LevelChangePropagator levelChangePropagator = new LevelChangePropagator();
32  
33      @BeforeEach
34      public void setUp() {
35          levelChangePropagator.setContext(loggerContext);
36          loggerContext.addListener(levelChangePropagator);
37      }
38  
39      void checkLevelChange(String loggerName, Level level) {
40          Logger logger = loggerContext.getLogger(loggerName);
41          logger.setLevel(level);
42          java.util.logging.Logger julLogger = JULHelper.asJULLogger(logger);
43          java.util.logging.Level julLevel = JULHelper.asJULLevel(level);
44  
45          assertEquals(julLevel, julLogger.getLevel());
46      }
47  
48  
49  
50      @Test
51      public void smoke() {
52          checkLevelChange("a", Level.INFO);
53          checkLevelChange("a.b", Level.DEBUG);
54      }
55  
56      @Test
57      public void root() {
58          checkLevelChange(Logger.ROOT_LOGGER_NAME, Level.TRACE);
59      }
60  
61      // see http://jira.qos.ch/browse/LBCLASSIC-256
62      @Test
63      public void gc() {
64          Logger logger = loggerContext.getLogger("gc" + rand);
65          logger.setLevel(Level.INFO);
66          // invoke GC so that the relevant julLogger can be garbage collected.
67          System.gc();
68          java.util.logging.Logger julLogger = JULHelper.asJULLogger(logger);
69          java.util.logging.Level julLevel = JULHelper.asJULLevel(Level.INFO);
70  
71          assertEquals(julLevel, julLogger.getLevel());
72      }
73  
74      // https://jira.qos.ch/browse/LOGBACK-1612
75      @Test
76      public void jonathan() {
77          Level level = Level.INFO;
78          Logger logger = loggerContext.getLogger("aaa");
79          logger.setLevel(level);
80  
81          java.util.logging.Logger julLogger = JULHelper.asJULLogger(logger);
82          java.util.logging.Level julLevel = JULHelper.asJULLevel(level);
83  
84          assertFalse(julLogger.isLoggable(java.util.logging.Level.CONFIG));
85      }
86  
87      @Test
88      public void julHelperAsJulLevelRejectsNull() {
89          Exception e = assertThrows(IllegalArgumentException.class, () -> {
90              JULHelper.asJULLevel(null);
91          });
92          assertEquals("Unexpected level [null]", e.getMessage());
93      }
94  
95      @Test
96      public void settingLevelToNullGetsParentLevel() {
97          // first set level of "a" (the parent) to DEBUG
98          Logger parent = loggerContext.getLogger("a");
99          parent.setLevel(Level.DEBUG);
100 
101         // then set level of "a.b" (child logger of a) to null
102         // for b to inherit its parent's level
103         Logger child = loggerContext.getLogger("a.b");
104         child.setLevel(Level.INFO);
105         child.setLevel(null);
106 
107         assertEquals(parent.getEffectiveLevel(), child.getEffectiveLevel());
108         assertEquals(Level.DEBUG, child.getEffectiveLevel());
109     }
110 }