View Javadoc

1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2009, 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  package chapter6;
11  
12  import org.slf4j.Logger;
13  import org.slf4j.LoggerFactory;
14  import org.slf4j.MDC;
15  import org.slf4j.Marker;
16  import org.slf4j.MarkerFactory;
17  
18  import ch.qos.logback.classic.LoggerContext;
19  import ch.qos.logback.classic.joran.JoranConfigurator;
20  import ch.qos.logback.core.joran.spi.JoranException;
21  
22  public class FilterEvents {
23  
24    public static void main(String[] args) throws InterruptedException {
25      if (args.length == 0) {
26        System.out.println("A configuration file must be passed as a parameter.");
27        return;
28      }
29      
30      Logger logger = (Logger) LoggerFactory.getLogger(FilterEvents.class);
31      LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
32  
33      try {
34        JoranConfigurator configurator = new JoranConfigurator();
35        configurator.setContext(lc);
36        lc.reset();
37        configurator.doConfigure(args[0]);
38      } catch (JoranException je) {
39        je.printStackTrace();
40      }
41      
42      for (int i = 0; i < 10; i++) {
43        if (i == 3)  {
44          MDC.put("username", "sebastien");
45          logger.debug("logging statement {}", i);
46          MDC.remove("username");
47        } else if (i == 6) {
48          Marker billing = MarkerFactory.getMarker("billing");
49          logger.error(billing, "billing statement {}", i);
50        } else {
51          logger.info("logging statement {}", i);
52        }
53      }
54    }
55  }