View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * 
4    * Copyright (C) 1999-2006, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  
11  package chapter4.mail;
12  
13  import org.slf4j.Logger;
14  import org.slf4j.LoggerFactory;
15  
16  import ch.qos.logback.classic.LoggerContext;
17  import ch.qos.logback.classic.joran.JoranConfigurator;
18  import ch.qos.logback.core.util.StatusPrinter;
19  
20  
21  
22  /**
23   * This application generates log messages in numbers specified by the
24   * user. See
25   * also configuration scripts rolling.properties and rolling.xml.
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      StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
42      
43      Logger logger = LoggerFactory.getLogger(EMail.class);
44  
45      for (int i = 1; i <= runLength; i++) {
46        if ((i % 10) < 9) {
47          logger.debug("This is a debug message. Message number: " + i);
48        } else {
49          logger.warn("This is a warning message. Message number: " + i);
50        }
51      }
52  
53      logger.error("At last an error.", new Exception("Just testing"));
54      
55      StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
56    }
57  
58    static void usage(String msg) {
59      System.err.println(msg);
60      System.err.println("Usage: java " + EMail.class.getName() +
61        " runLength configFile\n" +
62        "   runLength (integer) the number of logs to generate\n" +
63        "   configFile a logback configuration file in XML format." +
64        " XML files must have a '.xml' extension.");
65      System.exit(1);
66    }
67  }