Jdk14Logger.java

  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.commons.logging.impl;

  18. import java.io.Serializable;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;

  21. import org.apache.commons.logging.Log;

  22. /**
  23.  * Implements the {@code org.apache.commons.logging.Log}
  24.  * interface to wrap the standard JDK logging mechanisms that were
  25.  * introduced in the Merlin release (JDK 1.4).
  26.  */
  27. public class Jdk14Logger implements Log, Serializable {

  28.     /** Serializable version identifier. */
  29.     private static final long serialVersionUID = 4784713551416303804L;

  30.     /**
  31.      * This member variable simply ensures that any attempt to initialize
  32.      * this class in a pre-1.4 JVM will result in an ExceptionInInitializerError.
  33.      * It must not be private, as an optimizing compiler could detect that it
  34.      * is not used and optimize it away.
  35.      */
  36.     protected static final Level dummyLevel = Level.FINE;

  37.     /**
  38.      * The underlying Logger implementation we are using.
  39.      */
  40.     protected transient Logger logger;

  41.     /**
  42.      * The name of the logger we are wrapping.
  43.      */
  44.     protected String name;

  45.     /**
  46.      * Constructs a named instance of this Logger.
  47.      *
  48.      * @param name Name of the logger to be constructed
  49.      */
  50.     public Jdk14Logger(final String name) {
  51.         this.name = name;
  52.         logger = getLogger();
  53.     }

  54.     /**
  55.      * Logs a message with {@link java.util.logging.Level#FINE}.
  56.      *
  57.      * @param message to log
  58.      * @see org.apache.commons.logging.Log#debug(Object)
  59.      */
  60.     @Override
  61.     public void debug(final Object message) {
  62.         log(Level.FINE, String.valueOf(message), null);
  63.     }

  64.     /**
  65.      * Logs a message with {@link java.util.logging.Level#FINE}.
  66.      *
  67.      * @param message to log
  68.      * @param exception log this cause
  69.      * @see org.apache.commons.logging.Log#debug(Object, Throwable)
  70.      */
  71.     @Override
  72.     public void debug(final Object message, final Throwable exception) {
  73.         log(Level.FINE, String.valueOf(message), exception);
  74.     }

  75.     /**
  76.      * Logs a message with {@link java.util.logging.Level#SEVERE}.
  77.      *
  78.      * @param message to log
  79.      * @see org.apache.commons.logging.Log#error(Object)
  80.      */
  81.     @Override
  82.     public void error(final Object message) {
  83.         log(Level.SEVERE, String.valueOf(message), null);
  84.     }

  85.     /**
  86.      * Logs a message with {@link java.util.logging.Level#SEVERE}.
  87.      *
  88.      * @param message to log
  89.      * @param exception log this cause
  90.      * @see org.apache.commons.logging.Log#error(Object, Throwable)
  91.      */
  92.     @Override
  93.     public void error(final Object message, final Throwable exception) {
  94.         log(Level.SEVERE, String.valueOf(message), exception);
  95.     }

  96.     /**
  97.      * Logs a message with {@link java.util.logging.Level#SEVERE}.
  98.      *
  99.      * @param message to log
  100.      * @see org.apache.commons.logging.Log#fatal(Object)
  101.      */
  102.     @Override
  103.     public void fatal(final Object message) {
  104.         log(Level.SEVERE, String.valueOf(message), null);
  105.     }

  106.     /**
  107.      * Logs a message with {@link java.util.logging.Level#SEVERE}.
  108.      *
  109.      * @param message to log
  110.      * @param exception log this cause
  111.      * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
  112.      */
  113.     @Override
  114.     public void fatal(final Object message, final Throwable exception) {
  115.         log(Level.SEVERE, String.valueOf(message), exception);
  116.     }

  117.     /**
  118.      * Gets the native Logger instance we are using.
  119.      *
  120.      * @return  the native Logger instance we are using.
  121.      */
  122.     public Logger getLogger() {
  123.         if (logger == null) {
  124.             logger = Logger.getLogger(name);
  125.         }
  126.         return logger;
  127.     }

  128.     /**
  129.      * Logs a message with {@link java.util.logging.Level#INFO}.
  130.      *
  131.      * @param message to log
  132.      * @see org.apache.commons.logging.Log#info(Object)
  133.      */
  134.     @Override
  135.     public void info(final Object message) {
  136.         log(Level.INFO, String.valueOf(message), null);
  137.     }

  138.     /**
  139.      * Logs a message with {@link java.util.logging.Level#INFO}.
  140.      *
  141.      * @param message to log
  142.      * @param exception log this cause
  143.      * @see org.apache.commons.logging.Log#info(Object, Throwable)
  144.      */
  145.     @Override
  146.     public void info(final Object message, final Throwable exception) {
  147.         log(Level.INFO, String.valueOf(message), exception);
  148.     }

  149.     /**
  150.      * Is debug logging currently enabled?
  151.      */
  152.     @Override
  153.     public boolean isDebugEnabled() {
  154.         return getLogger().isLoggable(Level.FINE);
  155.     }

  156.     /**
  157.      * Is error logging currently enabled?
  158.      */
  159.     @Override
  160.     public boolean isErrorEnabled() {
  161.         return getLogger().isLoggable(Level.SEVERE);
  162.     }

  163.     /**
  164.      * Is fatal logging currently enabled?
  165.      */
  166.     @Override
  167.     public boolean isFatalEnabled() {
  168.         return getLogger().isLoggable(Level.SEVERE);
  169.     }

  170.     /**
  171.      * Is info logging currently enabled?
  172.      */
  173.     @Override
  174.     public boolean isInfoEnabled() {
  175.         return getLogger().isLoggable(Level.INFO);
  176.     }

  177.     /**
  178.      * Is trace logging currently enabled?
  179.      */
  180.     @Override
  181.     public boolean isTraceEnabled() {
  182.         return getLogger().isLoggable(Level.FINEST);
  183.     }

  184.     /**
  185.      * Is warn logging currently enabled?
  186.      */
  187.     @Override
  188.     public boolean isWarnEnabled() {
  189.         return getLogger().isLoggable(Level.WARNING);
  190.     }

  191.     /**
  192.      * Logs a message at the given level.
  193.      * @param level The level.
  194.      * @param msg The message.
  195.      * @param ex The exception.
  196.      */
  197.     protected void log(final Level level, final String msg, final Throwable ex) {
  198.         final Logger logger = getLogger();
  199.         if (logger.isLoggable(level)) {
  200.             // Hack (?) to get the stack trace.
  201.             final Throwable dummyException = new Throwable();
  202.             final StackTraceElement[] locations = dummyException.getStackTrace();
  203.             // LOGGING-132: use the provided logger name instead of the class name
  204.             final String cname = name;
  205.             String method = "unknown";
  206.             // Caller will be the third element
  207.             if (locations != null && locations.length > 2) {
  208.                 final StackTraceElement caller = locations[2];
  209.                 method = caller.getMethodName();
  210.             }
  211.             if (ex == null) {
  212.                 logger.logp(level, cname, method, msg);
  213.             } else {
  214.                 logger.logp(level, cname, method, msg, ex);
  215.             }
  216.         }
  217.     }

  218.     /**
  219.      * Logs a message with {@link java.util.logging.Level#FINEST}.
  220.      *
  221.      * @param message to log
  222.      * @see org.apache.commons.logging.Log#trace(Object)
  223.      */
  224.     @Override
  225.     public void trace(final Object message) {
  226.         log(Level.FINEST, String.valueOf(message), null);
  227.     }

  228.     /**
  229.      * Logs a message with {@link java.util.logging.Level#FINEST}.
  230.      *
  231.      * @param message to log
  232.      * @param exception log this cause
  233.      * @see org.apache.commons.logging.Log#trace(Object, Throwable)
  234.      */
  235.     @Override
  236.     public void trace(final Object message, final Throwable exception) {
  237.         log(Level.FINEST, String.valueOf(message), exception);
  238.     }

  239.     /**
  240.      * Logs a message with {@link java.util.logging.Level#WARNING}.
  241.      *
  242.      * @param message to log
  243.      * @see org.apache.commons.logging.Log#warn(Object)
  244.      */
  245.     @Override
  246.     public void warn(final Object message) {
  247.         log(Level.WARNING, String.valueOf(message), null);
  248.     }

  249.     /**
  250.      * Logs a message with {@link java.util.logging.Level#WARNING}.
  251.      *
  252.      * @param message to log
  253.      * @param exception log this cause
  254.      * @see org.apache.commons.logging.Log#warn(Object, Throwable)
  255.      */
  256.     @Override
  257.     public void warn(final Object message, final Throwable exception) {
  258.         log(Level.WARNING, String.valueOf(message), exception);
  259.     }
  260. }