View Javadoc

    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.LoggingEvent;
    24  import org.apache.log4j.spi.LoggerFactory;
    25  import org.apache.log4j.helpers.LogLog;
    26  import org.apache.log4j.xml.XLevel;
    27
    28  /***
    29     A simple example showing Logger sub-classing. It shows the
    30     minimum steps necessary to implement one's {@link LoggerFactory}.
    31     Note that sub-classes follow the hierarchy even if its loggers
    32     belong to different classes.
    33   */
    34  public class XLogger extends Logger implements OptionHandler {
    35
    36    // It's usually a good idea to add a dot suffix to the fully
    37    // qualified class name. This makes caller localization to work
    38    // properly even from classes that have almost the same fully
    39    // qualified class name as XLogger, such as XLogegoryTest.
    40    private static String FQCN = XLogger.class.getName() + ".";
    41
    42    // It's enough to instantiate a factory once and for all.
    43    private static XFactory factory = new XFactory();
    44
    45    String suffix = "";
    46
    47    /***
    48       Just calls the parent constuctor.
    49     */
    50    protected XLogger(String name) {
    51      super(name);
    52    }
    53
    54    /***
    55       Nothing to activate.
    56     */
    57    public
    58    void activateOptions() {
    59    }
    60
    61    /***
    62       Overrides the standard debug method by appending the value of
    63       suffix variable to each message.
    64    */
    65    public
    66    void debug(String message) {
    67      super.log(FQCN, Level.DEBUG, message + " " + suffix, null);
    68    }
    69
    70    /***
    71       We introduce a new printing method in order to support {@link
    72       XLevel#LETHAL}.  */
    73    public
    74    void lethal(String message, Throwable t) {
    75      if(repository.isDisabled(XLevel.LETHAL_INT))
    76        return;
    77      if(XLevel.LETHAL.isGreaterOrEqual(this.getEffectiveLevel()))
    78        forcedLog(FQCN, XLevel.LETHAL, message, t);
    79    }
    80
    81    /***
    82       We introduce a new printing method in order to support {@link
    83       XLevel#LETHAL}.  */
    84    public
    85    void lethal(String message) {
    86      if(repository.isDisabled(XLevel.LETHAL_INT))
    87        return;
    88      if(XLevel.LETHAL.isGreaterOrEqual(this.getEffectiveLevel()))
    89        forcedLog(FQCN, XLevel.LETHAL, message, null);
    90    }
    91
    92    static
    93    public
    94    Logger getLogger(String name) {
    95      return LogManager.getLogger(name, factory);
    96    }
    97
    98    static
    99    public
    100   Logger getLogger(Class clazz) {
    101     return XLogger.getLogger(clazz.getName());
    102   }
    103
    104
    105   public
    106   String getSuffix() {
    107     return suffix;
    108   }
    109
    110   public
    111   void setSuffix(String suffix) {
    112     this.suffix = suffix;
    113   }
    114
    115   /***
    116      We introduce a new printing method that takes the TRACE level.
    117   */
    118   public
    119   void trace(String message, Throwable t) {
    120     if(repository.isDisabled(XLevel.TRACE_INT))
    121       return;
    122     if(XLevel.TRACE.isGreaterOrEqual(this.getEffectiveLevel()))
    123       forcedLog(FQCN, XLevel.TRACE, message, t);
    124   }
    125
    126   /***
    127      We introduce a new printing method that takes the TRACE level.
    128   */
    129   public
    130   void trace(String message) {
    131     if(repository.isDisabled(XLevel.TRACE_INT))
    132       return;
    133     if(XLevel.TRACE.isGreaterOrEqual(this.getEffectiveLevel()))
    134       forcedLog(FQCN, XLevel.TRACE, message, null);
    135   }
    136
    137
    138
    139   // Any sub-class of Logger must also have its own implementation of
    140   // CategoryFactory.
    141   public static class XFactory implements LoggerFactory {
    142
    143     public XFactory() {
    144     }
    145
    146     public
    147     Logger  makeNewLoggerInstance(String name) {
    148       return new XLogger(name);
    149     }
    150   }
    151 }
    152
    153