KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > subclass > MyLoggerTest


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package examples.subclass;
18
19 import org.apache.log4j.*;
20 import org.apache.log4j.spi.LoggerFactory;
21 import org.apache.log4j.xml.DOMConfigurator;
22 import examples.customLevel.XLevel;
23 import org.apache.log4j.PropertyConfigurator;
24 import org.apache.log4j.helpers.LogLog;
25
26 /**
27    A simple example showing logger subclassing.
28
29    <p>The example should make it clear that subclasses follow the
30    hierarchy. You should also try running this example with a <a
31    href="doc-files/mycat.bad">bad</a> and <a
32    href="doc-files/mycat.good">good</a> configuration file samples.
33
34    <p>See <b><a
35    href="doc-files/MyLogger.java">source code</a></b> for more details.
36 */

37 public class MyLoggerTest {
38
39   /**
40      When called wihtout arguments, this program will just print
41      <pre>
42        DEBUG [main] some.cat - Hello world.
43      </pre>
44      and exit.
45      
46      <b>However, it can be called with a configuration file in XML or
47      properties format.
48
49    */

50   static public void main(String JavaDoc[] args) {
51     
52     if(args.length == 0) {
53       // Note that the appender is added to root but that the log
54
// request is made to an instance of MyLogger. The output still
55
// goes to System.out.
56
Logger root = Logger.getRootLogger();
57       Layout layout = new PatternLayout("%p [%t] %c (%F:%L) - %m%n");
58       root.addAppender(new ConsoleAppender(layout, ConsoleAppender.SYSTEM_OUT));
59     }
60     else if(args.length == 1) {
61       if(args[0].endsWith("xml")) {
62     DOMConfigurator.configure(args[0]);
63       } else {
64     PropertyConfigurator.configure(args[0]);
65       }
66     } else {
67       usage("Incorrect number of parameters.");
68     }
69     try {
70       MyLogger c = (MyLogger) MyLogger.getLogger("some.cat");
71       c.trace("Hello");
72       c.debug("Hello");
73     } catch(ClassCastException JavaDoc e) {
74       LogLog.error("Did you forget to set the factory in the config file?", e);
75     }
76   }
77
78   static
79   void usage(String JavaDoc errMsg) {
80     System.err.println(errMsg);
81     System.err.println("\nUsage: "+MyLogger.class.getName() + "[configFile]\n"
82                 + " where *configFile* is an optional configuration file, "+
83                "either in properties or XML format.");
84     System.exit(1);
85   }
86 }
87
Popular Tags