1
2
3
4
5
6
7
8
9
10
11
12
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
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
66 appenderFILE.setRollingPolicy(timeBasedRollingPolicy);
67
68
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
76 appenderFILE.setEncoder(patternLayoutEncoder);
77
78
79 SizeBasedTriggeringPolicy sizeBasedTriggeringPolicy = new SizeBasedTriggeringPolicy();
80 sizeBasedTriggeringPolicy.setContext(loggerContext);
81 sizeBasedTriggeringPolicy.setMaxFileSize(ch.qos.logback.core.util.FileSize.valueOf("1000"));
82
83 sizeBasedTriggeringPolicy.start();
84
85 appenderFILE.setTriggeringPolicy(sizeBasedTriggeringPolicy);
86
87
88 ThresholdFilter thresholdFilter = new ThresholdFilter();
89 thresholdFilter.setContext(loggerContext);
90 thresholdFilter.setLevel("TRACE");
91
92 thresholdFilter.start();
93
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
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