View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package chapters.onJoran.implicit;
15  
16  import java.util.ArrayList;
17  import java.util.HashMap;
18  import java.util.List;
19  import java.util.Map;
20  
21  import ch.qos.logback.core.Context;
22  import ch.qos.logback.core.ContextBase;
23  import ch.qos.logback.core.joran.action.Action;
24  import ch.qos.logback.core.joran.action.ImplicitAction;
25  import ch.qos.logback.core.joran.spi.Pattern;
26  import ch.qos.logback.core.util.StatusPrinter;
27  import chapters.onJoran.SimpleConfigurator;
28  
29  /**
30   * This example illustrates the usage of implicit actions.
31   * 
32   * <p>Keep in mind that implicit actions are not associated with any specific
33   * pattern. Moreover, they are added directly to a Joran Interpreter instead of
34   * a rule store.
35   * 
36   * @author Ceki G&uuml;ulc&uuml;
37   */
38  public class PrintMe {
39  
40    public static void main(String[] args) throws Exception {
41      Context context = new ContextBase();
42  
43      Map<Pattern, Action> ruleMap = new HashMap<Pattern, Action>();
44  
45      // we start with the rule for the top-most (root) element
46      ruleMap.put(new Pattern("*/foo"), new NOPAction());
47  
48      // Add an implicit action. 
49      List<ImplicitAction> iaList = new ArrayList<ImplicitAction>();
50      iaList.add(new PrintMeImplicitAction());
51      SimpleConfigurator simpleConfigurator = new SimpleConfigurator(ruleMap,
52          iaList); 
53  
54      // link the configurator with its context
55      simpleConfigurator.setContext(context);
56  
57      simpleConfigurator.doConfigure(args[0]);
58      StatusPrinter.printInCaseOfErrorsOrWarnings(context);
59      
60    }
61  }