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 chapter10.implicit;
11  
12  import java.util.ArrayList;
13  import java.util.HashMap;
14  import java.util.List;
15  import java.util.Map;
16  
17  import ch.qos.logback.core.Context;
18  import ch.qos.logback.core.ContextBase;
19  import ch.qos.logback.core.joran.action.Action;
20  import ch.qos.logback.core.joran.action.ImplicitAction;
21  import ch.qos.logback.core.joran.spi.Pattern;
22  import ch.qos.logback.core.util.StatusPrinter;
23  import chapter10.SimpleConfigurator;
24  
25  /**
26   * This example illustrates the usage of implicit actions.
27   * 
28   * <p>Keep in mind that implicit actions are not associated with any specific
29   * pattern. Moreover, they are added directly to a Joran Interpreter instead of
30   * a rule store.
31   * 
32   * @author Ceki G&uuml;ulc&uuml;
33   */
34  public class PrintMe {
35  
36    public static void main(String[] args) throws Exception {
37      Context context = new ContextBase();
38  
39      Map<Pattern, Action> ruleMap = new HashMap<Pattern, Action>();
40  
41      // we start with the rule for the top-most (root) element
42      ruleMap.put(new Pattern("*/foo"), new NOPAction());
43  
44      // Add an implicit action. 
45      List<ImplicitAction> iaList = new ArrayList<ImplicitAction>();
46      iaList.add(new PrintMeImplicitAction());
47      SimpleConfigurator simpleConfigurator = new SimpleConfigurator(ruleMap,
48          iaList); 
49  
50      // link the configurator with its context
51      simpleConfigurator.setContext(context);
52  
53      simpleConfigurator.doConfigure(args[0]);
54      StatusPrinter.printInCaseOfErrorsOrWarnings(context);
55      
56    }
57  }