1
2
3
4
5
6
7
8
9
10
11
12
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
27
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, "This is a serious an error requiring the admin's attention", 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 " + Marked_EMail.class.getName() + " configFile\n" + " configFile a logback configuration file in XML format.");
64 System.exit(1);
65 }
66 }