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