    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.logging.impl;
    019
    020import java.io.Serializable;
    021import java.util.logging.Level;
    022import java.util.logging.Logger;
    023
    024import org.apache.commons.logging.Log;
    025
    026/**
    027 * Implements the {@code org.apache.commons.logging.Log}
    028 * interface to wrap the standard JDK logging mechanisms that were
    029 * introduced in the Merlin release (JDK 1.4).
    030 */
    031public class Jdk14Logger implements Log, Serializable {
    032
    033    /** Serializable version identifier. */
    034    private static final long serialVersionUID = 4784713551416303804L;
    035
    036    /**
    037     * This member variable simply ensures that any attempt to initialize
    038     * this class in a pre-1.4 JVM will result in an ExceptionInInitializerError.
    039     * It must not be private, as an optimizing compiler could detect that it
    040     * is not used and optimize it away.
    041     */
    042    protected static final Level dummyLevel = Level.FINE;
    043
    044    /**
    045     * The underlying Logger implementation we are using.
    046     */
    047    protected transient Logger logger;
    048
    049    /**
    050     * The name of the logger we are wrapping.
    051     */
    052    protected String name;
    053
    054    /**
    055     * Constructs a named instance of this Logger.
    056     *
    057     * @param name Name of the logger to be constructed
    058     */
    059    public Jdk14Logger(final String name) {
    060        this.name = name;
    061        logger = getLogger();
    062    }
    063
    064    /**
    065     * Logs a message with {@link java.util.logging.Level#FINE}.
    066     *
    067     * @param message to log
    068     * @see org.apache.commons.logging.Log#debug(Object)
    069     */
    070    @Override
    071    public void debug(final Object message) {
    072        log(Level.FINE, String.valueOf(message), null);
    073    }
    074
    075    /**
    076     * Logs a message with {@link java.util.logging.Level#FINE}.
    077     *
    078     * @param message to log
    079     * @param exception log this cause
    080     * @see org.apache.commons.logging.Log#debug(Object, Throwable)
    081     */
    082    @Override
    083    public void debug(final Object message, final Throwable exception) {
    084        log(Level.FINE, String.valueOf(message), exception);
    085    }
    086
    087    /**
    088     * Logs a message with {@link java.util.logging.Level#SEVERE}.
    089     *
    090     * @param message to log
    091     * @see org.apache.commons.logging.Log#error(Object)
    092     */
    093    @Override
    094    public void error(final Object message) {
    095        log(Level.SEVERE, String.valueOf(message), null);
    096    }
    097
    098    /**
    099     * Logs a message with {@link java.util.logging.Level#SEVERE}.
    100     *
    101     * @param message to log
    102     * @param exception log this cause
    103     * @see org.apache.commons.logging.Log#error(Object, Throwable)
    104     */
    105    @Override
    106    public void error(final Object message, final Throwable exception) {
    107        log(Level.SEVERE, String.valueOf(message), exception);
    108    }
    109
    110    /**
    111     * Logs a message with {@link java.util.logging.Level#SEVERE}.
    112     *
    113     * @param message to log
    114     * @see org.apache.commons.logging.Log#fatal(Object)
    115     */
    116    @Override
    117    public void fatal(final Object message) {
    118        log(Level.SEVERE, String.valueOf(message), null);
    119    }
    120
    121    /**
    122     * Logs a message with {@link java.util.logging.Level#SEVERE}.
    123     *
    124     * @param message to log
    125     * @param exception log this cause
    126     * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
    127     */
    128    @Override
    129    public void fatal(final Object message, final Throwable exception) {
    130        log(Level.SEVERE, String.valueOf(message), exception);
    131    }
    132
    133    /**
    134     * Gets the native Logger instance we are using.
    135     *
    136     * @return  the native Logger instance we are using.
    137     */
    138    public Logger getLogger() {
    139        if (logger == null) {
    140            logger = Logger.getLogger(name);
    141        }
    142        return logger;
    143    }
    144
    145    /**
    146     * Logs a message with {@link java.util.logging.Level#INFO}.
    147     *
    148     * @param message to log
    149     * @see org.apache.commons.logging.Log#info(Object)
    150     */
    151    @Override
    152    public void info(final Object message) {
    153        log(Level.INFO, String.valueOf(message), null);
    154    }
    155
    156    /**
    157     * Logs a message with {@link java.util.logging.Level#INFO}.
    158     *
    159     * @param message to log
    160     * @param exception log this cause
    161     * @see org.apache.commons.logging.Log#info(Object, Throwable)
    162     */
    163    @Override
    164    public void info(final Object message, final Throwable exception) {
    165        log(Level.INFO, String.valueOf(message), exception);
    166    }
    167
    168    /**
    169     * Is debug logging currently enabled?
    170     */
    171    @Override
    172    public boolean isDebugEnabled() {
    173        return getLogger().isLoggable(Level.FINE);
    174    }
    175
    176    /**
    177     * Is error logging currently enabled?
    178     */
    179    @Override
    180    public boolean isErrorEnabled() {
    181        return getLogger().isLoggable(Level.SEVERE);
    182    }
    183
    184    /**
    185     * Is fatal logging currently enabled?
    186     */
    187    @Override
    188    public boolean isFatalEnabled() {
    189        return getLogger().isLoggable(Level.SEVERE);
    190    }
    191
    192    /**
    193     * Is info logging currently enabled?
    194     */
    195    @Override
    196    public boolean isInfoEnabled() {
    197        return getLogger().isLoggable(Level.INFO);
    198    }
    199
    200    /**
    201     * Is trace logging currently enabled?
    202     */
    203    @Override
    204    public boolean isTraceEnabled() {
    205        return getLogger().isLoggable(Level.FINEST);
    206    }
    207
    208    /**
    209     * Is warn logging currently enabled?
    210     */
    211    @Override
    212    public boolean isWarnEnabled() {
    213        return getLogger().isLoggable(Level.WARNING);
    214    }
    215
    216    /**
    217     * Logs a message at the given level.
    218     * @param level The level.
    219     * @param msg The message.
    220     * @param ex The exception.
    221     */
    222    protected void log(final Level level, final String msg, final Throwable ex) {
    223        final Logger logger = getLogger();
    224        if (logger.isLoggable(level)) {
    225            // Hack (?) to get the stack trace.
    226            final Throwable dummyException = new Throwable();
    227            final StackTraceElement[] locations = dummyException.getStackTrace();
    228            // LOGGING-132: use the provided logger name instead of the class name
    229            final String cname = name;
    230            String method = "unknown";
    231            // Caller will be the third element
    232            if (locations != null && locations.length > 2) {
    233                final StackTraceElement caller = locations[2];
    234                method = caller.getMethodName();
    235            }
    236            if (ex == null) {
    237                logger.logp(level, cname, method, msg);
    238            } else {
    239                logger.logp(level, cname, method, msg, ex);
    240            }
    241        }
    242    }
    243
    244    /**
    245     * Logs a message with {@link java.util.logging.Level#FINEST}.
    246     *
    247     * @param message to log
    248     * @see org.apache.commons.logging.Log#trace(Object)
    249     */
    250    @Override
    251    public void trace(final Object message) {
    252        log(Level.FINEST, String.valueOf(message), null);
    253    }
    254
    255    /**
    256     * Logs a message with {@link java.util.logging.Level#FINEST}.
    257     *
    258     * @param message to log
    259     * @param exception log this cause
    260     * @see org.apache.commons.logging.Log#trace(Object, Throwable)
    261     */
    262    @Override
    263    public void trace(final Object message, final Throwable exception) {
    264        log(Level.FINEST, String.valueOf(message), exception);
    265    }
    266
    267    /**
    268     * Logs a message with {@link java.util.logging.Level#WARNING}.
    269     *
    270     * @param message to log
    271     * @see org.apache.commons.logging.Log#warn(Object)
    272     */
    273    @Override
    274    public void warn(final Object message) {
    275        log(Level.WARNING, String.valueOf(message), null);
    276    }
    277
    278    /**
    279     * Logs a message with {@link java.util.logging.Level#WARNING}.
    280     *
    281     * @param message to log
    282     * @param exception log this cause
    283     * @see org.apache.commons.logging.Log#warn(Object, Throwable)
    284     */
    285    @Override
    286    public void warn(final Object message, final Throwable exception) {
    287        log(Level.WARNING, String.valueOf(message), exception);
    288    }
    289}