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.multiJVM;
15  
16  import org.slf4j.Logger;
17  
18  import ch.qos.logback.classic.LoggerContext;
19  import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
20  import ch.qos.logback.classic.spi.ILoggingEvent;
21  import ch.qos.logback.core.rolling.RollingFileAppender;
22  import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
23  import ch.qos.logback.core.util.StatusPrinter;
24  
25  /**
26   * An application to write to a file using a RollingFileAppender in safe mode.
27   * 
28   * @author Ceki Gulcu
29   * 
30   */
31  public class SafeModeRollingFileAppender {
32  
33      static long LEN;
34      static String FILENAME;
35      static String STAMP;
36  
37      static final String DATE_PATTERN = "yyyy-MM-dd_HH_mm_ss";
38  
39      static public void main(String[] argv) throws Exception {
40          if (argv.length != 3) {
41              usage("Wrong number of arguments.");
42          }
43  
44          STAMP = argv[0];
45          LEN = Integer.parseInt(argv[1]);
46          FILENAME = argv[2];
47          writeContinously(STAMP, FILENAME, true);
48      }
49  
50      static void usage(String msg) {
51          System.err.println(msg);
52          System.err.println("Usage: java " + SafeModeRollingFileAppender.class.getName() + " stamp runLength filename\n" + " stamp JVM instance stamp\n"
53                          + "   runLength (integer) the number of logs to generate perthread" + "    filename (string) the filename where to write\n");
54          System.exit(1);
55      }
56  
57      static LoggerContext buildLoggerContext(String stamp, String filename, boolean safetyMode) {
58          LoggerContext loggerContext = new LoggerContext();
59  
60          RollingFileAppender<ILoggingEvent> rfa = new RollingFileAppender<ILoggingEvent>();
61          PatternLayoutEncoder patternLayout = new PatternLayoutEncoder();
62          patternLayout.setPattern(stamp + " %5p - %-50m%n");
63          patternLayout.setContext(loggerContext);
64          patternLayout.start();
65  
66          rfa.setEncoder(patternLayout);
67  
68          rfa.setAppend(true);
69          rfa.setPrudent(safetyMode);
70          rfa.setContext(loggerContext);
71  
72          TimeBasedRollingPolicy<ILoggingEvent> tbrp = new TimeBasedRollingPolicy<>();
73  
74          tbrp.setContext(loggerContext);
75          tbrp.setFileNamePattern(filename + "-%d{" + DATE_PATTERN + "}.log");
76          tbrp.setParent(rfa);
77          tbrp.start();
78  
79          rfa.setRollingPolicy(tbrp);
80  
81          rfa.start();
82  
83          ch.qos.logback.classic.Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
84          root.addAppender(rfa);
85  
86          return loggerContext;
87      }
88  
89      static void writeContinously(String stamp, String filename, boolean safetyMode) throws Exception {
90          LoggerContext lc = buildLoggerContext(stamp, filename, safetyMode);
91          Logger logger = lc.getLogger(SafeModeRollingFileAppender.class);
92  
93          long before = System.nanoTime();
94          for (int i = 0; i < LEN; i++) {
95              logger.debug(LoggingThread.msgLong + " " + i);
96          }
97          lc.stop();
98          StatusPrinter.print(lc);
99          double durationPerLog = (System.nanoTime() - before) / (LEN * 1000.0);
100 
101         System.out.println("Average duration of " + (durationPerLog) + " microseconds per log. Safety mode " + safetyMode);
102         System.out.println("------------------------------------------------");
103     }
104 }