View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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  import org.slf4j.Marker;
19  import org.slf4j.MarkerFactory;
20  
21  import ch.qos.logback.classic.LoggerContext;
22  import ch.qos.logback.classic.joran.JoranConfigurator;
23  import ch.qos.logback.core.util.StatusPrinter;
24  
25  /**
26   * This application generates a number of message many of which are of LEVEL.
27   * However, only one message bears the  "NOTIFY_ADMIN" marker.
28   * */
29  public class Marked_EMail {
30    static public void main(String[] args) throws Exception {
31      if (args.length != 1) {
32        usage("Wrong number of arguments.");
33      }
34  
35      String configFile = args[0];
36  
37      LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
38      JoranConfigurator configurator = new JoranConfigurator();
39      lc.reset();
40      configurator.setContext(lc);
41      configurator.doConfigure(configFile);
42      StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
43  
44      Logger logger = LoggerFactory.getLogger(Marked_EMail.class);
45  
46      int runLength = 100;
47      for (int i = 1; i <= runLength; i++) {
48        if ((i % 10) < 9) {
49          logger.debug("This is a debug message. Message number: " + i);
50        } else {
51          logger.error("This is an error message. Message number: " + i);
52        }
53      }
54  
55      Marker notifyAdmin = MarkerFactory.getMarker("NOTIFY_ADMIN");
56      logger.error(notifyAdmin,
57          "This is a serious an error requiring the admin's attention",
58          new Exception("Just testing"));
59  
60      StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
61    }
62  
63    static void usage(String msg) {
64      System.err.println(msg);
65      System.err.println("Usage: java " + Marked_EMail.class.getName()
66          + " configFile\n"
67          + "   configFile a logback configuration file in XML format.");
68      System.exit(1);
69    }
70  }