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"
53                  + " stamp JVM instance stamp\n" + "   runLength (integer) the number of logs to generate perthread"
54                  + "    filename (string) the filename where to write\n");
55          System.exit(1);
56      }
57  
58      static LoggerContext buildLoggerContext(String stamp, String filename, boolean safetyMode) {
59          LoggerContext loggerContext = new LoggerContext();
60  
61          RollingFileAppender<ILoggingEvent> rfa = new RollingFileAppender<ILoggingEvent>();
62          PatternLayoutEncoder patternLayout = new PatternLayoutEncoder();
63          patternLayout.setPattern(stamp + " %5p - %-50m%n");
64          patternLayout.setContext(loggerContext);
65          patternLayout.start();
66  
67          rfa.setEncoder(patternLayout);
68  
69          rfa.setAppend(true);
70          rfa.setPrudent(safetyMode);
71          rfa.setContext(loggerContext);
72  
73          TimeBasedRollingPolicy<ILoggingEvent> tbrp = new TimeBasedRollingPolicy<>();
74  
75          tbrp.setContext(loggerContext);
76          tbrp.setFileNamePattern(filename + "-%d{" + DATE_PATTERN + "}.log");
77          tbrp.setParent(rfa);
78          tbrp.start();
79  
80          rfa.setRollingPolicy(tbrp);
81  
82          rfa.start();
83  
84          ch.qos.logback.classic.Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
85          root.addAppender(rfa);
86  
87          return loggerContext;
88      }
89  
90      static void writeContinously(String stamp, String filename, boolean safetyMode) throws Exception {
91          LoggerContext lc = buildLoggerContext(stamp, filename, safetyMode);
92          Logger logger = lc.getLogger(SafeModeRollingFileAppender.class);
93  
94          long before = System.nanoTime();
95          for (int i = 0; i < LEN; i++) {
96              logger.debug(LoggingThread.msgLong + " " + i);
97          }
98          lc.stop();
99          StatusPrinter.print(lc);
100         double durationPerLog = (System.nanoTime() - before) / (LEN * 1000.0);
101 
102         System.out.println(
103                 "Average duration of " + (durationPerLog) + " microseconds per log. Safety mode " + safetyMode);
104         System.out.println("------------------------------------------------");
105     }
106 }