View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2024, 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  
15  package ch.qos.logback.classic.issue;
16  
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.filter.ThresholdFilter;
21  import ch.qos.logback.classic.tyler.TylerConfiguratorBase;
22  import ch.qos.logback.classic.util.LogbackMDCAdapter;
23  import ch.qos.logback.core.Appender;
24  import ch.qos.logback.core.rolling.RollingFileAppender;
25  import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy;
26  import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
27  import ch.qos.logback.core.util.StatusPrinter2;
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Disabled;
30  import org.junit.jupiter.api.Test;
31  
32  @Disabled
33  public class LOGBACK_1393_Test extends TylerConfiguratorBase {
34  
35      LoggerContext loggerContext = new LoggerContext();
36      LogbackMDCAdapter mdcAdapter = new LogbackMDCAdapter();
37      StatusPrinter2 statusPrinter2 = new StatusPrinter2();
38  
39  
40      @BeforeEach
41      public void setup() {
42          loggerContext.setMDCAdapter(mdcAdapter);
43      }
44      public void configure(LoggerContext loggerCoontext) {
45          setContext(loggerCoontext);
46          addOnConsoleStatusListener();
47          Appender appenderFILE = setupAppenderFILE();
48          Logger logger_ROOT = setupLogger("ROOT", "DEBUG", null);
49          logger_ROOT.addAppender(appenderFILE);
50      }
51  
52      Appender setupAppenderFILE() {
53          RollingFileAppender appenderFILE = new RollingFileAppender();
54          appenderFILE.setContext(loggerContext);
55          appenderFILE.setName("FILE");
56  
57          appenderFILE.setImmediateFlush(true);
58          // Configure component of type TimeBasedRollingPolicy
59          TimeBasedRollingPolicy timeBasedRollingPolicy = new TimeBasedRollingPolicy();
60          timeBasedRollingPolicy.setContext(loggerContext);
61          timeBasedRollingPolicy.setFileNamePattern(subst("/tmp/log/lb1393.%d{yyyy-MM-dd}.log"));
62          timeBasedRollingPolicy.setMaxHistory(6);
63          timeBasedRollingPolicy.setParent(appenderFILE);
64          timeBasedRollingPolicy.start();
65          // Inject component of type TimeBasedRollingPolicy into parent
66          appenderFILE.setRollingPolicy(timeBasedRollingPolicy);
67  
68          // Configure component of type PatternLayoutEncoder
69          PatternLayoutEncoder patternLayoutEncoder = new PatternLayoutEncoder();
70          patternLayoutEncoder.setContext(context);
71          patternLayoutEncoder.setPattern("%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n");
72  
73          patternLayoutEncoder.setParent(appenderFILE);
74          patternLayoutEncoder.start();
75          // Inject component of type PatternLayoutEncoder into parent
76          appenderFILE.setEncoder(patternLayoutEncoder);
77  
78          // Configure component of type SizeBasedTriggeringPolicy
79          SizeBasedTriggeringPolicy sizeBasedTriggeringPolicy = new SizeBasedTriggeringPolicy();
80          sizeBasedTriggeringPolicy.setContext(loggerContext);
81          sizeBasedTriggeringPolicy.setMaxFileSize(ch.qos.logback.core.util.FileSize.valueOf("1000"));
82          // ===========no parent setter
83          sizeBasedTriggeringPolicy.start();
84          // Inject component of type SizeBasedTriggeringPolicy into parent
85          appenderFILE.setTriggeringPolicy(sizeBasedTriggeringPolicy);
86  
87          // Configure component of type ThresholdFilter
88          ThresholdFilter thresholdFilter = new ThresholdFilter();
89          thresholdFilter.setContext(loggerContext);
90          thresholdFilter.setLevel("TRACE");
91          // ===========no parent setter
92          thresholdFilter.start();
93          // Inject component of type ThresholdFilter into parent
94          appenderFILE.addFilter(thresholdFilter);
95  
96          appenderFILE.start();
97          return appenderFILE;
98      }
99  
100     @Test
101     void smoke() {
102         configure(loggerContext);
103         Logger logger = loggerContext.getLogger(this.getClass());
104         for(int i = 0; i < 100; i++) {
105             logger.atInfo().addKeyValue("i", i).log("hello world xxasdaasfasf asdfasfdsfd");
106             delay(100);
107         }
108         //statusPrinter2.print(loggerContext);
109     }
110 
111     private void delay(int i) {
112         try {
113             Thread.sleep(i);
114         } catch (InterruptedException e) {
115             throw new RuntimeException(e);
116         }
117     }
118 
119 }
120 
121