001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.classic.multiJVM; 015 016import org.slf4j.Logger; 017 018import ch.qos.logback.classic.LoggerContext; 019import ch.qos.logback.classic.encoder.PatternLayoutEncoder; 020import ch.qos.logback.classic.spi.ILoggingEvent; 021import ch.qos.logback.core.rolling.RollingFileAppender; 022import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; 023import ch.qos.logback.core.util.StatusPrinter; 024 025/** 026 * An application to write to a file using a RollingFileAppender in safe mode. 027 * 028 * @author Ceki Gulcu 029 * 030 */ 031public class SafeModeRollingFileAppender { 032 033 static long LEN; 034 static String FILENAME; 035 static String STAMP; 036 037 static final String DATE_PATTERN = "yyyy-MM-dd_HH_mm_ss"; 038 039 static public void main(String[] argv) throws Exception { 040 if (argv.length != 3) { 041 usage("Wrong number of arguments."); 042 } 043 044 STAMP = argv[0]; 045 LEN = Integer.parseInt(argv[1]); 046 FILENAME = argv[2]; 047 writeContinously(STAMP, FILENAME, true); 048 } 049 050 static void usage(String msg) { 051 System.err.println(msg); 052 System.err.println("Usage: java " + SafeModeRollingFileAppender.class.getName() + " stamp runLength filename\n" + " stamp JVM instance stamp\n" 053 + " runLength (integer) the number of logs to generate perthread" + " filename (string) the filename where to write\n"); 054 System.exit(1); 055 } 056 057 static LoggerContext buildLoggerContext(String stamp, String filename, boolean safetyMode) { 058 LoggerContext loggerContext = new LoggerContext(); 059 060 RollingFileAppender<ILoggingEvent> rfa = new RollingFileAppender<ILoggingEvent>(); 061 PatternLayoutEncoder patternLayout = new PatternLayoutEncoder(); 062 patternLayout.setPattern(stamp + " %5p - %-50m%n"); 063 patternLayout.setContext(loggerContext); 064 patternLayout.start(); 065 066 rfa.setEncoder(patternLayout); 067 068 rfa.setAppend(true); 069 rfa.setPrudent(safetyMode); 070 rfa.setContext(loggerContext); 071 072 TimeBasedRollingPolicy<ILoggingEvent> tbrp = new TimeBasedRollingPolicy<>(); 073 074 tbrp.setContext(loggerContext); 075 tbrp.setFileNamePattern(filename + "-%d{" + DATE_PATTERN + "}.log"); 076 tbrp.setParent(rfa); 077 tbrp.start(); 078 079 rfa.setRollingPolicy(tbrp); 080 081 rfa.start(); 082 083 ch.qos.logback.classic.Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME); 084 root.addAppender(rfa); 085 086 return loggerContext; 087 } 088 089 static void writeContinously(String stamp, String filename, boolean safetyMode) throws Exception { 090 LoggerContext lc = buildLoggerContext(stamp, filename, safetyMode); 091 Logger logger = lc.getLogger(SafeModeRollingFileAppender.class); 092 093 long before = System.nanoTime(); 094 for (int i = 0; i < LEN; i++) { 095 logger.debug(LoggingThread.msgLong + " " + i); 096 } 097 lc.stop(); 098 StatusPrinter.print(lc); 099 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}