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.FileAppender;
022
023/**
024 * An application to write to a file using a FileAppender in safe mode.
025 * 
026 * @author Ceki Gulcu
027 * 
028 */
029public class SafeModeFileAppender {
030
031    static long LEN;
032    static String FILENAME;
033    static String STAMP;
034
035    static public void main(String[] argv) throws Exception {
036        if (argv.length != 3) {
037            usage("Wrong number of arguments.");
038        }
039
040        STAMP = argv[0];
041        LEN = Integer.parseInt(argv[1]);
042        FILENAME = argv[2];
043        writeContinously(STAMP, FILENAME, true);
044    }
045
046    static void usage(String msg) {
047        System.err.println(msg);
048        System.err.println("Usage: java " + SafeModeFileAppender.class.getName() + " stamp runLength filename\n" + " stamp JVM instance stamp\n"
049                        + "   runLength (integer) the number of logs to generate perthread" + "    filename (string) the filename where to write\n");
050        System.exit(1);
051    }
052
053    static LoggerContext buildLoggerContext(String stamp, String filename, boolean safetyMode) {
054        LoggerContext loggerContext = new LoggerContext();
055
056        FileAppender<ILoggingEvent> fa = new FileAppender<ILoggingEvent>();
057
058        PatternLayoutEncoder patternLayout = new PatternLayoutEncoder();
059        patternLayout.setPattern(stamp + " %5p - %m%n");
060        patternLayout.setContext(loggerContext);
061        patternLayout.start();
062
063        fa.setEncoder(patternLayout);
064        fa.setFile(filename);
065        fa.setAppend(true);
066        fa.setPrudent(safetyMode);
067        fa.setContext(loggerContext);
068        fa.start();
069
070        ch.qos.logback.classic.Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
071        root.addAppender(fa);
072
073        return loggerContext;
074    }
075
076    static void writeContinously(String stamp, String filename, boolean safetyMode) throws Exception {
077        LoggerContext lc = buildLoggerContext(stamp, filename, safetyMode);
078        Logger logger = lc.getLogger(SafeModeFileAppender.class);
079
080        long before = System.nanoTime();
081        for (int i = 0; i < LEN; i++) {
082            logger.debug(LoggingThread.msgLong + " " + i);
083        }
084        lc.stop();
085        double durationPerLog = (System.nanoTime() - before) / (LEN * 1000.0);
086
087        System.out.println("Average duration of " + (durationPerLog) + " microseconds per log. Safety mode " + safetyMode);
088        System.out.println("------------------------------------------------");
089    }
090}