XLogger xref

View Javadoc

    1   /**
    2    * Copyright (c) 2004-2011 QOS.ch
    3    * All rights reserved.
    4    *
    5    * Permission is hereby granted, free  of charge, to any person obtaining
    6    * a  copy  of this  software  and  associated  documentation files  (the
    7    * "Software"), to  deal in  the Software without  restriction, including
    8    * without limitation  the rights to  use, copy, modify,  merge, publish,
    9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
    10   * permit persons to whom the Software  is furnished to do so, subject to
    11   * the following conditions:
    12   *
    13   * The  above  copyright  notice  and  this permission  notice  shall  be
    14   * included in all copies or substantial portions of the Software.
    15   *
    16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
    17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
    18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
    19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
    22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    23   *
    24   */
    25  package org.slf4j.ext;
    26
    27  import org.slf4j.Logger;
    28  import org.slf4j.Marker;
    29  import org.slf4j.MarkerFactory;
    30  import org.slf4j.helpers.FormattingTuple;
    31  import org.slf4j.helpers.MessageFormatter;
    32  import org.slf4j.spi.LocationAwareLogger;
    33
    34  /**
    35   * A utility that provides standard mechanisms for logging certain kinds of
    36   * activities.
    37   *
    38   * @author Ralph Goers
    39   * @author Ceki G&uuml;lc&uuml;
    40   */
    41  public class XLogger extends LoggerWrapper implements Logger {
    42
    43    private static final String FQCN = XLogger.class.getName();
    44    static Marker FLOW_MARKER = MarkerFactory.getMarker("FLOW");
    45    static Marker ENTRY_MARKER = MarkerFactory.getMarker("ENTRY");
    46    static Marker EXIT_MARKER = MarkerFactory.getMarker("EXIT");
    47
    48    static Marker EXCEPTION_MARKER = MarkerFactory.getMarker("EXCEPTION");
    49    static Marker THROWING_MARKER = MarkerFactory.getMarker("THROWING");
    50    static Marker CATCHING_MARKER = MarkerFactory.getMarker("CATCHING");
    51
    52    static String EXIT_MESSAGE_0 = "exit";
    53    static String EXIT_MESSAGE_1 = "exit with ({})";
    54
    55    static String ENTRY_MESSAGE_0 = "entry";
    56    static String ENTRY_MESSAGE_1 = "entry with ({})";
    57    static String ENTRY_MESSAGE_2 = "entry with ({}, {})";
    58    static String ENTRY_MESSAGE_3 = "entry with ({}, {}, {})";
    59    static String ENTRY_MESSAGE_4 = "entry with ({}, {}, {}, {})";
    60    static int ENTRY_MESSAGE_ARRAY_LEN = 5;
    61    static String[] ENTRY_MESSAGE_ARRAY = new String[ENTRY_MESSAGE_ARRAY_LEN];
    62    static {
    63      ENTRY_MARKER.add(FLOW_MARKER);
    64      EXIT_MARKER.add(FLOW_MARKER);
    65      THROWING_MARKER.add(EXCEPTION_MARKER);
    66      CATCHING_MARKER.add(EXCEPTION_MARKER);
    67
    68      ENTRY_MESSAGE_ARRAY[0] = ENTRY_MESSAGE_0;
    69      ENTRY_MESSAGE_ARRAY[1] = ENTRY_MESSAGE_1;
    70      ENTRY_MESSAGE_ARRAY[2] = ENTRY_MESSAGE_2;
    71      ENTRY_MESSAGE_ARRAY[3] = ENTRY_MESSAGE_3;
    72      ENTRY_MESSAGE_ARRAY[4] = ENTRY_MESSAGE_4;
    73    }
    74
    75    public enum Level {
    76      TRACE("TRACE", LocationAwareLogger.TRACE_INT), DEBUG("DEBUG",
    77          LocationAwareLogger.DEBUG_INT), INFO("INFO",
    78          LocationAwareLogger.INFO_INT), WARN("WARN",
    79          LocationAwareLogger.WARN_INT), ERROR("ERROR",
    80          LocationAwareLogger.ERROR_INT);
    81
    82      private final String name;
    83      private final int level;
    84
    85      public String toString() {
    86        return this.name;
    87      }
    88
    89      public int intValue() {
    90        return this.level;
    91      }
    92
    93      private Level(String name, int level) {
    94        this.name = name;
    95        this.level = level;
    96      }
    97    }
    98
    99    /**
    100    * Given an underlying logger, construct an XLogger
    101    *
    102    * @param logger
    103    *          underlying logger
    104    */
    105   public XLogger(Logger logger) {
    106     // If class B extends A, assuming B does not override method x(), the caller
    107     // of new B().x() is A and not B, see also
    108     // http://bugzilla.slf4j.org/show_bug.cgi?id=114
    109     super(logger, LoggerWrapper.class.getName());
    110   }
    111
    112   /**
    113    * Log method entry.
    114    *
    115    * @param argArray
    116    *          supplied parameters
    117    */
    118   public void entry(Object... argArray) {
    119     if (instanceofLAL && logger.isTraceEnabled(ENTRY_MARKER)) {
    120       String messagePattern = null;
    121       if (argArray.length < ENTRY_MESSAGE_ARRAY_LEN) {
    122         messagePattern = ENTRY_MESSAGE_ARRAY[argArray.length];
    123       } else {
    124         messagePattern = buildMessagePattern(argArray.length);
    125       }
    126       FormattingTuple tp = MessageFormatter.arrayFormat(messagePattern, argArray);
    127       ((LocationAwareLogger) logger).log(ENTRY_MARKER, FQCN,
    128           LocationAwareLogger.TRACE_INT, tp.getMessage(), argArray, tp.getThrowable());
    129     }
    130   }
    131
    132   /**
    133    * Log method exit
    134    */
    135   public void exit() {
    136     if (instanceofLAL && logger.isTraceEnabled(ENTRY_MARKER)) {
    137       ((LocationAwareLogger) logger).log(EXIT_MARKER, FQCN,
    138           LocationAwareLogger.TRACE_INT, EXIT_MESSAGE_0, null, null);
    139     }
    140   }
    141
    142   /**
    143    * Log method exit
    144    *
    145    * @param result
    146    *          The result of the method being exited
    147    */
    148   public void exit(Object result) {
    149     if (instanceofLAL && logger.isTraceEnabled(ENTRY_MARKER)) {
    150       FormattingTuple tp = MessageFormatter.format(EXIT_MESSAGE_1, result);
    151       ((LocationAwareLogger) logger).log(EXIT_MARKER, FQCN,
    152           LocationAwareLogger.TRACE_INT, tp.getMessage(),
    153           new Object[] { result }, tp.getThrowable());
    154     }
    155   }
    156
    157   /**
    158    * Log an exception being thrown. The generated log event uses Level ERROR.
    159    *
    160    * @param throwable
    161    *          the exception being caught.
    162    */
    163   public void throwing(Throwable throwable) {
    164     if (instanceofLAL) {
    165       ((LocationAwareLogger) logger).log(THROWING_MARKER, FQCN,
    166           LocationAwareLogger.ERROR_INT, "throwing", null, throwable);
    167     }
    168   }
    169
    170   /**
    171    * Log an exception being thrown allowing the log level to be specified.
    172    *
    173    * @param level
    174    *          the logging level to use.
    175    * @param throwable
    176    *          the exception being caught.
    177    */
    178   public void throwing(Level level, Throwable throwable) {
    179     if (instanceofLAL) {
    180       ((LocationAwareLogger) logger).log(THROWING_MARKER, FQCN, level.level,
    181           "throwing", null, throwable);
    182     }
    183   }
    184
    185   /**
    186    * Log an exception being caught. The generated log event uses Level ERROR.
    187    *
    188    * @param throwable
    189    *          the exception being caught.
    190    */
    191   public void catching(Throwable throwable) {
    192     if (instanceofLAL) {
    193       ((LocationAwareLogger) logger).log(CATCHING_MARKER, FQCN,
    194           LocationAwareLogger.ERROR_INT, "catching", null, throwable);
    195     }
    196   }
    197
    198   /**
    199    * Log an exception being caught allowing the log level to be specified.
    200    *
    201    * @param level
    202    *          the logging level to use.
    203    * @param throwable
    204    *          the exception being caught.
    205    */
    206   public void catching(Level level, Throwable throwable) {
    207     if (instanceofLAL) {
    208       ((LocationAwareLogger) logger).log(CATCHING_MARKER, FQCN, level.level,
    209           "catching", null, throwable);
    210     }
    211   }
    212
    213   private static String buildMessagePattern(int len) {
    214     StringBuilder sb = new StringBuilder();
    215     sb.append(" entry with (");
    216     for (int i = 0; i < len; i++) {
    217       sb.append("{}");
    218       if (i != len - 1)
    219         sb.append(", ");
    220     }
    221     sb.append(')');
    222     return sb.toString();
    223   }
    224 }