View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2023, 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.logback_1754;
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  import java.util.concurrent.CountDownLatch;
20  
21  import ch.qos.logback.classic.ClassicConstants;
22  import ch.qos.logback.classic.ClassicTestConstants;
23  import ch.qos.logback.core.testUtil.RandomUtil;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  public class SafeModeTest {
28  
29      private static final int THREADS = 3;
30  
31      private void runTest() {
32  
33          CountDownLatch latch = new CountDownLatch(THREADS);
34          List<Thread> threads = new ArrayList<>(THREADS);
35          for (int i = 0; i < THREADS; i++) {
36              LoggerThread thread = new LoggerThread(latch, "message from thread " + i);
37              thread.start();
38              threads.add(thread);
39          }
40          int i = 0;
41          for (Thread thread : threads) {
42              try {
43                  thread.join();
44                  System.out.println("joined thread "+thread.getName());
45              } catch (InterruptedException e) {
46                  e.printStackTrace();
47                  //Thread.currentThread().interrupt();
48                  //throw new RuntimeException(e);
49              }
50          }
51      }
52  
53      public static void main(String... args) {
54          int diff = RandomUtil.getPositiveInt();
55          //System.setProperty("logback.statusListenerClass", "sysout");
56          System.setProperty(ClassicConstants.CONFIG_FILE_PROPERTY, ClassicTestConstants.INPUT_PREFIX+"issue/logback-1754.xml");
57          System.setProperty("logback_1754_targetDirectory", ClassicTestConstants.OUTPUT_DIR_PREFIX+"safeWrite_"+diff);
58  
59  
60          new SafeModeTest().runTest();
61      }
62  
63      private static final class LoggerThread extends Thread {
64          private static final Logger LOG = LoggerFactory.getLogger(LoggerThread.class);
65          private final CountDownLatch latch;
66          private final String message;
67  
68          LoggerThread(CountDownLatch latch, String message) {
69              setDaemon(false);
70              this.latch = latch;
71              this.message = message;
72          }
73  
74          @Override
75          public void run() {
76              latch.countDown();
77              for(int i = 0; i < 100; i++) {
78                  if(i % 10 == 0) {
79                      delay(1);
80                  }
81                  LOG.info(message + " i=" + i);
82              }
83          }
84  
85          static void delay(long millis) {
86              try {
87                  Thread.sleep(millis);
88              } catch (InterruptedException e) {
89                  throw new RuntimeException(e);
90              }
91          }
92      }
93  }