1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2008, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  
11  package ch.qos.logback.classic.multiJVM;
12  
13  import org.slf4j.Logger;
14  
15  import ch.qos.logback.classic.LoggerContext;
16  import ch.qos.logback.classic.PatternLayout;
17  import ch.qos.logback.classic.spi.LoggingEvent;
18  import ch.qos.logback.core.rolling.RollingFileAppender;
19  import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
20  import ch.qos.logback.core.util.StatusPrinter;
21  
22  /**
23   * An application to write to a file using a RollingFileAppender in safe mode.
24   * 
25   * @author Ceki Gulcu
26   * 
27   */
28  public class SafeModeRollingFileAppender {
29  
30    static long LEN;
31    static String FILENAME;
32    static String STAMP;
33    
34    static final String DATE_PATTERN = "yyyy-MM-dd_HH_mm_ss";
35  
36    static public void main(String[] argv) throws Exception {
37      if (argv.length != 3) {
38        usage("Wrong number of arguments.");
39      }
40  
41      STAMP = argv[0];
42      LEN = Integer.parseInt(argv[1]);
43      FILENAME = argv[2];
44      writeContinously(STAMP, FILENAME, true);
45    }
46  
47    static void usage(String msg) {
48      System.err.println(msg);
49      System.err.println("Usage: java " + SafeModeRollingFileAppender.class.getName()
50          + " stamp runLength filename\n" + " stamp JVM instance stamp\n"
51          + "   runLength (integer) the number of logs to generate perthread"
52          + "    filename (string) the filename where to write\n");
53      System.exit(1);
54    }
55  
56    static LoggerContext buildLoggerContext(String stamp, String filename,
57        boolean safetyMode) {
58      LoggerContext loggerContext = new LoggerContext();
59  
60      RollingFileAppender<LoggingEvent> rfa = new RollingFileAppender<LoggingEvent>();
61      PatternLayout patternLayout = new PatternLayout();
62      patternLayout.setPattern(stamp + " %5p - %-50m%n");
63      patternLayout.setContext(loggerContext);
64      patternLayout.start();
65  
66      rfa.setLayout(patternLayout);
67      
68      rfa.setAppend(true);
69      rfa.setImmediateFlush(true);
70      rfa.setBufferedIO(false);
71      rfa.setPrudent(safetyMode);
72      rfa.setContext(loggerContext);
73  
74      TimeBasedRollingPolicy tbrp = new TimeBasedRollingPolicy();
75      
76      tbrp.setContext(loggerContext);
77      tbrp.setFileNamePattern(filename+"-%d{"+DATE_PATTERN+"}.log");
78      tbrp.setParent(rfa);
79      tbrp.start();
80    
81      rfa.setRollingPolicy(tbrp);
82  
83      
84      rfa.start();
85  
86      ch.qos.logback.classic.Logger root = loggerContext
87          .getLogger(LoggerContext.ROOT_NAME);
88      root.addAppender(rfa);
89  
90      return loggerContext;
91    }
92  
93    static void writeContinously(String stamp, String filename, boolean safetyMode)
94        throws Exception {
95      LoggerContext lc = buildLoggerContext(stamp, filename, safetyMode);
96      Logger logger = lc.getLogger(SafeModeRollingFileAppender.class);
97  
98      long before = System.nanoTime();
99      for (int i = 0; i < LEN; i++) {
100       logger.debug(LoggingThread.msgLong + " " + i);
101     }
102     lc.stop();
103     StatusPrinter.print(lc);
104     double durationPerLog = (System.nanoTime() - before) / (LEN * 1000.0);
105 
106     System.out.println("Average duration of " + (durationPerLog)
107         + " microseconds per log. Safety mode " + safetyMode);
108     System.out.println("------------------------------------------------");
109   }
110 }