1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.log4j.customLogger;
19  
20  
21  import org.apache.log4j.*;
22  import org.apache.log4j.spi.OptionHandler;
23  import org.apache.log4j.spi.LoggerFactory;
24  import org.apache.log4j.xml.XLevel;
25  
26  /**
27     A simple example showing Logger sub-classing. It shows the
28     minimum steps necessary to implement one's {@link LoggerFactory}.
29     Note that sub-classes follow the hierarchy even if its loggers
30     belong to different classes.
31   */
32  public class XLogger extends Logger implements OptionHandler {
33    
34    // It's usually a good idea to add a dot suffix to the fully
35    // qualified class name. This makes caller localization to work
36    // properly even from classes that have almost the same fully
37    // qualified class name as XLogger, such as XLogegoryTest.
38    private static String FQCN = XLogger.class.getName() + ".";
39  
40    // It's enough to instantiate a factory once and for all.
41    private static XFactory factory = new XFactory();
42    
43    String suffix = "";
44  
45    /**
46       Just calls the parent constuctor.
47     */
48    protected XLogger(String name) {
49      super(name);
50    }
51  
52    /** 
53       Nothing to activate.
54     */
55    public
56    void activateOptions() {
57    }
58  
59    /**
60       Overrides the standard debug method by appending the value of
61       suffix variable to each message.  
62    */
63    public 
64    void debug(String message) {
65      super.log(FQCN, Level.DEBUG, message + " " + suffix, null);
66    }
67  
68    /**
69       We introduce a new printing method in order to support {@link
70       XLevel#LETHAL}.  */
71    public
72    void lethal(String message, Throwable t) { 
73      if(repository.isDisabled(XLevel.LETHAL_INT)) 
74        return;
75      if(XLevel.LETHAL.isGreaterOrEqual(this.getEffectiveLevel()))
76        forcedLog(FQCN, XLevel.LETHAL, message, t);
77    }
78  
79    /**
80       We introduce a new printing method in order to support {@link
81       XLevel#LETHAL}.  */
82    public
83    void lethal(String message) { 
84      if(repository.isDisabled(XLevel.LETHAL_INT)) 
85        return;
86      if(XLevel.LETHAL.isGreaterOrEqual(this.getEffectiveLevel()))
87        forcedLog(FQCN, XLevel.LETHAL, message, null);
88    }
89  
90    static
91    public
92    Logger getLogger(String name) {
93      return LogManager.getLogger(name, factory);
94    }
95  
96    static
97    public
98    Logger getLogger(Class clazz) {
99      return XLogger.getLogger(clazz.getName());
100   }
101 
102 
103   public
104   String getSuffix() {
105     return suffix;
106   }
107 
108   public
109   void setSuffix(String suffix) {
110     this.suffix = suffix;
111   }
112 
113   /**
114      We introduce a new printing method that takes the TRACE level.
115   */
116   public
117   void trace(String message, Throwable t) { 
118     if(repository.isDisabled(XLevel.TRACE_INT))
119       return;   
120     if(XLevel.TRACE.isGreaterOrEqual(this.getEffectiveLevel()))
121       forcedLog(FQCN, XLevel.TRACE, message, t);
122   }
123 
124   /**
125      We introduce a new printing method that takes the TRACE level.
126   */
127   public
128   void trace(String message) { 
129     if(repository.isDisabled(XLevel.TRACE_INT))
130       return;   
131     if(XLevel.TRACE.isGreaterOrEqual(this.getEffectiveLevel()))
132       forcedLog(FQCN, XLevel.TRACE, message, null);
133   }
134 
135 
136 
137   // Any sub-class of Logger must also have its own implementation of 
138   // CategoryFactory.
139   public static class XFactory implements LoggerFactory {
140     
141     public XFactory() {
142     }
143 
144     public
145     Logger  makeNewLoggerInstance(String name) {
146       return new XLogger(name);
147     }
148   }
149 }
150 
151