1
2
3
4
5
6
7
8
9
10
11
12
13
14 package chapter4.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
25
26
27
28
29
30 public class EMail {
31 static public void main(String[] args) throws Exception {
32 if (args.length != 2) {
33 usage("Wrong number of arguments.");
34 }
35
36 int runLength = Integer.parseInt(args[0]);
37 String configFile = args[1];
38
39 LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
40 JoranConfigurator configurator = new JoranConfigurator();
41 lc.reset();
42 configurator.setContext(lc);
43 configurator.doConfigure(configFile);
44 StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
45
46 Logger logger = LoggerFactory.getLogger(EMail.class);
47
48 for (int i = 1; i <= runLength; i++) {
49 if ((i % 10) < 9) {
50 logger.debug("This is a debug message. Message number: " + i);
51 } else {
52 logger.warn("This is a warning message. Message number: " + i);
53 }
54 }
55
56 logger.error("At last an error.", new Exception("Just testing"));
57
58 StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
59 }
60
61 static void usage(String msg) {
62 System.err.println(msg);
63 System.err.println("Usage: java " + EMail.class.getName() +
64 " runLength configFile\n" +
65 " runLength (integer) the number of logs to generate\n" +
66 " configFile a logback configuration file in XML format." +
67 " XML files must have a '.xml' extension.");
68 System.exit(1);
69 }
70 }