KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > subclass > MyLogger


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>See <b><a HREF="doc-files/MyLogger.java">source code</a></b>
30    for more details.
31
32    <p>See {@link MyLoggerTest} for a usage example.
33    
34  */

35 public class MyLogger extends Logger {
36
37   // It's usually a good idea to add a dot suffix to the fully
38
// qualified class name. This makes caller localization to work
39
// properly even from classes that have almost the same fully
40
// qualified class name as MyLogger, e.g. MyLoggerTest.
41
static String JavaDoc FQCN = MyLogger.class.getName() + ".";
42
43   // It's enough to instantiate a factory once and for all.
44
private static MyLoggerFactory myFactory = new MyLoggerFactory();
45
46   /**
47      Just calls the parent constuctor.
48    */

49   public MyLogger(String JavaDoc name) {
50     super(name);
51   }
52
53   /**
54      Overrides the standard debug method by appending " world" at the
55      end of each message. */

56   public
57   void debug(Object JavaDoc message) {
58     super.log(FQCN, Level.DEBUG, message + " world.", null);
59   }
60
61   /**
62      This method overrides {@link Logger#getLogger} by supplying
63      its own factory type as a parameter.
64   */

65   public
66   static
67   Logger getLogger(String JavaDoc name) {
68     return Logger.getLogger(name, myFactory);
69   }
70
71   public
72   void trace(Object JavaDoc message) {
73     super.log(FQCN, XLevel.TRACE, message, null);
74   }
75 }
76
77
78
Popular Tags