View Javadoc

1   /**
2    * Logback: the reliable, fast and flexible logging library for Java.
3    * 
4    * Copyright (C) 1999-2006, 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  
11  package chapter3.helloWorld;
12  
13  import java.util.HashMap;
14  import java.util.Map;
15  
16  import chapter3.SimpleConfigurator;
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.spi.JoranException;
21  import ch.qos.logback.core.joran.spi.Pattern;
22  import ch.qos.logback.core.util.StatusPrinter;
23  
24  
25  /**
26   *
27   * A hello world example using Joran.
28   *
29   * The first and only argument of this application must be the path to
30   * the XML file to interpret.
31   *
32   * For example,
33   *
34  <pre>
35      java joran.helloWorld.HelloWorld examples/src/joran/helloWorld/hello.xml
36  </pre>
37   *
38   * @author Ceki Gulcu
39   */
40  public class HelloWorld {
41    public static void main(String[] args) throws Exception {
42      Map<Pattern, Action> ruleMap = new HashMap<Pattern, Action>();
43  
44      // Associate "hello-world" pattern with  HelloWorldAction
45      ruleMap.put(new Pattern("hello-world"), new HelloWorldAction());
46  
47      // Joran needs to work within a context.
48      Context context = new ContextBase();
49      SimpleConfigurator simpleConfigurator = new SimpleConfigurator(ruleMap);
50      // link the configurator with its context
51      simpleConfigurator.setContext(context);
52  
53      try {
54        simpleConfigurator.doConfigure(args[0]);
55      } catch (JoranException e) {
56        // Print any errors that might have occured.
57        StatusPrinter.print(context);
58      }
59        }
60  }