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 chapters.appenders.mail;
015
016import org.slf4j.Logger;
017import org.slf4j.LoggerFactory;
018
019import ch.qos.logback.classic.LoggerContext;
020import ch.qos.logback.classic.joran.JoranConfigurator;
021import ch.qos.logback.core.util.StatusPrinter;
022
023/**
024 * This application generates log messages in numbers specified by the
025 * user. 
026 * */
027public class EMail {
028    static public void main(String[] args) throws Exception {
029        if (args.length != 2) {
030            usage("Wrong number of arguments.");
031        }
032
033        int runLength = Integer.parseInt(args[0]);
034        String configFile = args[1];
035
036        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
037        JoranConfigurator configurator = new JoranConfigurator();
038        lc.reset();
039        configurator.setContext(lc);
040        configurator.doConfigure(configFile);
041
042        Logger logger = LoggerFactory.getLogger(EMail.class);
043
044        for (int i = 1; i <= runLength; i++) {
045            if ((i % 10) < 9) {
046                logger.debug("This is a debug message. Message number: " + i);
047            } else {
048                logger.warn("This is a warning message. Message number: " + i);
049            }
050        }
051
052        logger.error("At last an error.", new Exception("Just testing"));
053        
054        lc.stop();
055        
056        StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
057    }
058
059    static void usage(String msg) {
060        System.err.println(msg);
061        System.err.println("Usage: java " + EMail.class.getName() + " runLength configFile\n" + "   runLength (integer) the number of logs to generate\n"
062                        + "   configFile a logback configuration file in XML format." + " XML files must have a '.xml' extension.");
063        System.exit(1);
064    }
065}