LogXF xref

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  package org.apache.log4j;
    18
    19  import org.apache.log4j.spi.LoggingEvent;
    20
    21  /**
    22   * This is a base class for LogMF and LogSF parameterized logging classes.
    23   *
    24   *
    25   * @see org.apache.log4j.LogMF
    26   * @see org.apache.log4j.LogSF
    27   * @since 1.2.16
    28   */
    29  public abstract class LogXF {
    30      /**
    31       * Trace level.
    32       */
    33      protected static final Level TRACE = new Level(5000, "TRACE", 7);
    34      /**
    35       * Fully Qualified Class Name of this class.
    36       */
    37      private static final String FQCN = LogXF.class.getName();
    38
    39      protected LogXF() {
    40      }
    41
    42      /**
    43       * Returns a Boolean instance representing the specified boolean.
    44       * Boolean.valueOf was added in JDK 1.4.
    45       *
    46       * @param b a boolean value.
    47       * @return a Boolean instance representing b.
    48       */
    49      protected static Boolean valueOf(final boolean b) {
    50          if (b) {
    51              return Boolean.TRUE;
    52          }
    53          return Boolean.FALSE;
    54      }
    55
    56      /**
    57       * Returns a Character instance representing the specified char.
    58       * Character.valueOf was added in JDK 1.5.
    59       *
    60       * @param c a character value.
    61       * @return a Character instance representing c.
    62       */
    63      protected static Character valueOf(final char c) {
    64          return new Character(c);
    65      }
    66
    67      /**
    68       * Returns a Byte instance representing the specified byte.
    69       * Byte.valueOf was added in JDK 1.5.
    70       *
    71       * @param b a byte value.
    72       * @return a Byte instance representing b.
    73       */
    74      protected static Byte valueOf(final byte b) {
    75          return new Byte(b);
    76      }
    77
    78      /**
    79       * Returns a Short instance representing the specified short.
    80       * Short.valueOf was added in JDK 1.5.
    81       *
    82       * @param b a short value.
    83       * @return a Byte instance representing b.
    84       */
    85      protected static Short valueOf(final short b) {
    86          return new Short(b);
    87      }
    88
    89      /**
    90       * Returns an Integer instance representing the specified int.
    91       * Integer.valueOf was added in JDK 1.5.
    92       *
    93       * @param b an int value.
    94       * @return an Integer instance representing b.
    95       */
    96      protected static Integer valueOf(final int b) {
    97          return new Integer(b);
    98      }
    99
    100     /**
    101      * Returns a Long instance representing the specified long.
    102      * Long.valueOf was added in JDK 1.5.
    103      *
    104      * @param b a long value.
    105      * @return a Long instance representing b.
    106      */
    107     protected static Long valueOf(final long b) {
    108         return new Long(b);
    109     }
    110
    111     /**
    112      * Returns a Float instance representing the specified float.
    113      * Float.valueOf was added in JDK 1.5.
    114      *
    115      * @param b a float value.
    116      * @return a Float instance representing b.
    117      */
    118     protected static Float valueOf(final float b) {
    119         return new Float(b);
    120     }
    121
    122     /**
    123      * Returns a Double instance representing the specified double.
    124      * Double.valueOf was added in JDK 1.5.
    125      *
    126      * @param b a double value.
    127      * @return a Byte instance representing b.
    128      */
    129     protected static Double valueOf(final double b) {
    130         return new Double(b);
    131     }
    132
    133     /**
    134      * Create new array.
    135      *
    136      * @param param1 parameter 1.
    137      * @return new array.
    138      */
    139     protected static Object[] toArray(final Object param1) {
    140         return new Object[]{
    141                 param1
    142         };
    143     }
    144
    145     /**
    146      * Create new array.
    147      *
    148      * @param param1 parameter 1.
    149      * @param param2 parameter 2.
    150      * @return new array.
    151      */
    152     protected static Object[] toArray(final Object param1,
    153                                       final Object param2) {
    154         return new Object[]{
    155                 param1, param2
    156         };
    157     }
    158
    159     /**
    160      * Create new array.
    161      *
    162      * @param param1 parameter 1.
    163      * @param param2 parameter 2.
    164      * @param param3 parameter 3.
    165      * @return new array.
    166      */
    167     protected static Object[] toArray(final Object param1,
    168                                       final Object param2,
    169                                       final Object param3) {
    170         return new Object[]{
    171                 param1, param2, param3
    172         };
    173     }
    174
    175     /**
    176      * Create new array.
    177      *
    178      * @param param1 parameter 1.
    179      * @param param2 parameter 2.
    180      * @param param3 parameter 3.
    181      * @param param4 parameter 4.
    182      * @return new array.
    183      */
    184     protected static Object[] toArray(final Object param1,
    185                                       final Object param2,
    186                                       final Object param3,
    187                                       final Object param4) {
    188         return new Object[]{
    189                 param1, param2, param3, param4
    190         };
    191     }
    192
    193     /**
    194      * Log an entering message at DEBUG level.
    195      *
    196      * @param logger       logger, may not be null.
    197      * @param sourceClass  source class, may be null.
    198      * @param sourceMethod method, may be null.
    199      */
    200     public static void entering(final Logger logger,
    201                                 final String sourceClass,
    202                                 final String sourceMethod) {
    203         if (logger.isDebugEnabled()) {
    204             logger.callAppenders(new LoggingEvent(FQCN, logger, Level.DEBUG,
    205                     sourceClass + "." + sourceMethod + " ENTRY", null));
    206         }
    207     }
    208
    209     /**
    210      * Log an entering message with a parameter at DEBUG level.
    211      *
    212      * @param logger       logger, may not be null.
    213      * @param sourceClass  source class, may be null.
    214      * @param sourceMethod method, may be null.
    215      * @param param        parameter, may be null.
    216      */
    217     public static void entering(final Logger logger,
    218                                 final String sourceClass,
    219                                 final String sourceMethod,
    220                                 final String param) {
    221         if (logger.isDebugEnabled()) {
    222             String msg = sourceClass + "." + sourceMethod + " ENTRY " + param;
    223             logger.callAppenders(new LoggingEvent(FQCN, logger, Level.DEBUG,
    224                     msg, null));
    225         }
    226     }
    227
    228     /**
    229      * Log an entering message with a parameter at DEBUG level.
    230      *
    231      * @param logger       logger, may not be null.
    232      * @param sourceClass  source class, may be null.
    233      * @param sourceMethod method, may be null.
    234      * @param param        parameter, may be null.
    235      */
    236     public static void entering(final Logger logger,
    237                                 final String sourceClass,
    238                                 final String sourceMethod,
    239                                 final Object param) {
    240         if (logger.isDebugEnabled()) {
    241             String msg = sourceClass + "." + sourceMethod + " ENTRY ";
    242             if (param == null) {
    243                 msg += "null";
    244             } else {
    245                 try {
    246                     msg += param;
    247                 } catch(Throwable ex) {
    248                     msg += "?";
    249                 }
    250             }
    251             logger.callAppenders(new LoggingEvent(FQCN, logger, Level.DEBUG,
    252                     msg, null));
    253         }
    254     }
    255
    256     /**
    257      * Log an entering message with an array of parameters at DEBUG level.
    258      *
    259      * @param logger       logger, may not be null.
    260      * @param sourceClass  source class, may be null.
    261      * @param sourceMethod method, may be null.
    262      * @param params       parameters, may be null.
    263      */
    264     public static void entering(final Logger logger,
    265                                 final String sourceClass,
    266                                 final String sourceMethod,
    267                                 final Object[] params) {
    268         if (logger.isDebugEnabled()) {
    269             String msg = sourceClass + "." + sourceMethod + " ENTRY ";
    270             if (params != null && params.length > 0) {
    271                 String delim = "{";
    272                 for (int i = 0; i < params.length; i++) {
    273                     try {
    274                         msg += delim + params[i];
    275                     } catch(Throwable ex) {
    276                         msg += delim + "?";
    277                     }
    278                     delim = ",";
    279                 }
    280                 msg += "}";
    281             } else {
    282                 msg += "{}";
    283             }
    284             logger.callAppenders(new LoggingEvent(FQCN, logger, Level.DEBUG,
    285                     msg, null));
    286         }
    287     }
    288
    289     /**
    290      * Log an exiting message at DEBUG level.
    291      *
    292      * @param logger       logger, may not be null.
    293      * @param sourceClass  source class, may be null.
    294      * @param sourceMethod method, may be null.
    295      */
    296     public static void exiting(final Logger logger,
    297                                final String sourceClass,
    298                                final String sourceMethod) {
    299         if (logger.isDebugEnabled()) {
    300             logger.callAppenders(new LoggingEvent(FQCN, logger, Level.DEBUG,
    301                     sourceClass + "." + sourceMethod + " RETURN", null));
    302         }
    303     }
    304
    305     /**
    306      * Log an exiting message with result at DEBUG level.
    307      *
    308      * @param logger       logger, may not be null.
    309      * @param sourceClass  source class, may be null.
    310      * @param sourceMethod method, may be null.
    311      * @param result       result, may be null.
    312      */
    313     public static void exiting(
    314             final Logger logger,
    315             final String sourceClass,
    316             final String sourceMethod,
    317             final String result) {
    318         if (logger.isDebugEnabled()) {
    319             logger.callAppenders(new LoggingEvent(FQCN, logger, Level.DEBUG,
    320                     sourceClass + "." + sourceMethod + " RETURN " + result, null));
    321         }
    322     }
    323
    324     /**
    325      * Log an exiting message with result at DEBUG level.
    326      *
    327      * @param logger       logger, may not be null.
    328      * @param sourceClass  source class, may be null.
    329      * @param sourceMethod method, may be null.
    330      * @param result       result, may be null.
    331      */
    332     public static void exiting(
    333             final Logger logger,
    334             final String sourceClass,
    335             final String sourceMethod,
    336             final Object result) {
    337         if (logger.isDebugEnabled()) {
    338             String msg = sourceClass + "." + sourceMethod + " RETURN ";
    339             if (result == null) {
    340                 msg += "null";
    341             } else {
    342                 try {
    343                     msg += result;
    344                 } catch(Throwable ex) {
    345                     msg += "?";
    346                 }
    347             }
    348             logger.callAppenders(new LoggingEvent(FQCN, logger, Level.DEBUG,
    349                     msg, null));
    350         }
    351     }
    352
    353     /**
    354      * Logs a throwing message at DEBUG level.
    355      *
    356      * @param logger       logger, may not be null.
    357      * @param sourceClass  source class, may be null.
    358      * @param sourceMethod method, may be null.
    359      * @param thrown      throwable, may be null.
    360      */
    361     public static void throwing(
    362             final Logger logger,
    363             final String sourceClass,
    364             final String sourceMethod,
    365             final Throwable thrown) {
    366         if (logger.isDebugEnabled()) {
    367             logger.callAppenders(new LoggingEvent(FQCN, logger, Level.DEBUG,
    368                     sourceClass + "." + sourceMethod + " THROW", thrown));
    369         }
    370     }
    371 }