1
2
3
4
5
6
7
8
9
10 package chapter4.sift;
11
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14 import org.slf4j.MDC;
15
16 import ch.qos.logback.classic.LoggerContext;
17 import ch.qos.logback.classic.joran.JoranConfigurator;
18 import ch.qos.logback.core.joran.spi.JoranException;
19
20 public class SiftExample {
21
22 public static void main(String[] args) throws JoranException {
23 if (args.length != 1) {
24 usage("Wrong number of arguments.");
25 }
26
27 String configFile = args[0];
28
29 LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
30 JoranConfigurator configurator = new JoranConfigurator();
31 lc.reset();
32 configurator.setContext(lc);
33 configurator.doConfigure(configFile);
34
35
36 Logger logger = LoggerFactory.getLogger(SiftExample.class);
37 logger.debug("Application started");
38
39 MDC.put("userid", "Alice");
40 logger.debug("Alice says hello");
41
42
43 }
44
45 static void usage(String msg) {
46 System.err.println(msg);
47 System.err.println("Usage: java " + SiftExample.class.getName()
48 + " configFile\n" + " configFile a logback configuration file");
49 System.exit(1);
50 }
51 }