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 chapters.appenders.mail;
15  
16  import org.slf4j.Logger;
17  import org.slf4j.LoggerFactory;
18  
19  import ch.qos.logback.classic.LoggerContext;
20  import ch.qos.logback.classic.joran.JoranConfigurator;
21  import ch.qos.logback.core.util.StatusPrinter;
22  
23  /**
24   * This application generates log messages in numbers specified by the
25   * user. 
26   * */
27  public class EMail {
28      static public void main(String[] args) throws Exception {
29          if (args.length != 2) {
30              usage("Wrong number of arguments.");
31          }
32  
33          int runLength = Integer.parseInt(args[0]);
34          String configFile = args[1];
35  
36          LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
37          JoranConfigurator configurator = new JoranConfigurator();
38          lc.reset();
39          configurator.setContext(lc);
40          configurator.doConfigure(configFile);
41  
42          Logger logger = LoggerFactory.getLogger(EMail.class);
43  
44          for (int i = 1; i <= runLength; i++) {
45              if ((i % 10) < 9) {
46                  logger.debug("This is a debug message. Message number: " + i);
47              } else {
48                  logger.warn("This is a warning message. Message number: " + i);
49              }
50          }
51  
52          logger.error("At last an error.", new Exception("Just testing"));
53          
54          lc.stop();
55          
56          StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
57      }
58  
59      static void usage(String msg) {
60          System.err.println(msg);
61          System.err.println("Usage: java " + EMail.class.getName() + " runLength configFile\n" + "   runLength (integer) the number of logs to generate\n"
62                          + "   configFile a logback configuration file in XML format." + " XML files must have a '.xml' extension.");
63          System.exit(1);
64      }
65  }