Source code

    001/**
    002 * Copyright (c) 2004-2011 QOS.ch
    003 * All rights reserved.
    004 *
    005 * Permission is hereby granted, free  of charge, to any person obtaining
    006 * a  copy  of this  software  and  associated  documentation files  (the
    007 * "Software"), to  deal in  the Software without  restriction, including
    008 * without limitation  the rights to  use, copy, modify,  merge, publish,
    009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
    010 * permit persons to whom the Software  is furnished to do so, subject to
    011 * the following conditions:
    012 *
    013 * The  above  copyright  notice  and  this permission  notice  shall  be
    014 * included in all copies or substantial portions of the Software.
    015 *
    016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
    017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
    018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
    019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
    022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    023 *
    024 */
    025package org.slf4j.cal10n;
    026
    027import org.slf4j.Logger;
    028import org.slf4j.Marker;
    029import org.slf4j.MarkerFactory;
    030import org.slf4j.ext.LoggerWrapper;
    031import org.slf4j.spi.LocationAwareLogger;
    032
    033import ch.qos.cal10n.IMessageConveyor;
    034import ch.qos.cal10n.MessageParameterObj;
    035
    036/**
    037 * A logger specialized in localized logging. Localization is based in the <a
    038 * href="http://cal10n.qos.ch">CAL10N project</p>.
    039 *
    040 * @author Ceki G&uuml;lc&uuml;
    041 */
    042public class LocLogger extends LoggerWrapper implements Logger {
    043
    044    private static final String FQCN = LocLogger.class.getName();
    045
    046    /**
    047     * Every localized message logged by a LocLogger will bear this marker. It
    048     * allows marker-aware implementations to perform additional processing on
    049     * localized messages.
    050     */
    051    static Marker LOCALIZED = MarkerFactory.getMarker("LOCALIZED");
    052
    053    final IMessageConveyor imc;
    054
    055    public LocLogger(Logger logger, IMessageConveyor imc) {
    056        super(logger, LoggerWrapper.class.getName());
    057        if (imc == null) {
    058            throw new IllegalArgumentException("IMessageConveyor cannot be null");
    059        }
    060        this.imc = imc;
    061    }
    062
    063    /**
    064     * Log a localized message at the TRACE level.
    065     *
    066     * @param key
    067     *          the key used for localization
    068     * @param args
    069     *          optional arguments
    070     */
    071    public void trace(Enum<?> key, Object... args) {
    072        if (!logger.isTraceEnabled()) {
    073            return;
    074        }
    075        String translatedMsg = imc.getMessage(key, args);
    076        MessageParameterObj mpo = new MessageParameterObj(key, args);
    077
    078        if (instanceofLAL) {
    079            ((LocationAwareLogger) logger).log(LOCALIZED, FQCN, LocationAwareLogger.TRACE_INT, translatedMsg, args, null);
    080        } else {
    081            logger.trace(LOCALIZED, translatedMsg, mpo);
    082        }
    083    }
    084
    085    /**
    086     * Log a localized message at the DEBUG level.
    087     *
    088     * @param key
    089     *          the key used for localization
    090     * @param args
    091     *          optional arguments
    092     */
    093    public void debug(Enum<?> key, Object... args) {
    094        if (!logger.isDebugEnabled()) {
    095            return;
    096        }
    097        String translatedMsg = imc.getMessage(key, args);
    098        MessageParameterObj mpo = new MessageParameterObj(key, args);
    099
    100        if (instanceofLAL) {
    101            ((LocationAwareLogger) logger).log(LOCALIZED, FQCN, LocationAwareLogger.DEBUG_INT, translatedMsg, args, null);
    102        } else {
    103            logger.debug(LOCALIZED, translatedMsg, mpo);
    104        }
    105    }
    106
    107    /**
    108     * Log a localized message at the INFO level.
    109     *
    110     * @param key
    111     *          the key used for localization
    112     * @param args
    113     *          optional arguments
    114     */
    115    public void info(Enum<?> key, Object... args) {
    116        if (!logger.isInfoEnabled()) {
    117            return;
    118        }
    119        String translatedMsg = imc.getMessage(key, args);
    120        MessageParameterObj mpo = new MessageParameterObj(key, args);
    121
    122        if (instanceofLAL) {
    123            ((LocationAwareLogger) logger).log(LOCALIZED, FQCN, LocationAwareLogger.INFO_INT, translatedMsg, args, null);
    124        } else {
    125            logger.info(LOCALIZED, translatedMsg, mpo);
    126        }
    127    }
    128
    129    /**
    130     * Log a localized message at the WARN level.
    131     *
    132     * @param key
    133     *          the key used for localization
    134     * @param args
    135     *          optional arguments
    136     */
    137    public void warn(Enum<?> key, Object... args) {
    138        if (!logger.isWarnEnabled()) {
    139            return;
    140        }
    141        String translatedMsg = imc.getMessage(key, args);
    142        MessageParameterObj mpo = new MessageParameterObj(key, args);
    143
    144        if (instanceofLAL) {
    145            ((LocationAwareLogger) logger).log(LOCALIZED, FQCN, LocationAwareLogger.WARN_INT, translatedMsg, args, null);
    146        } else {
    147            logger.warn(LOCALIZED, translatedMsg, mpo);
    148        }
    149    }
    150
    151    /**
    152     * Log a localized message at the ERROR level.
    153     *
    154     * @param key
    155     *          the key used for localization
    156     * @param args
    157     *          optional arguments
    158     */
    159    public void error(Enum<?> key, Object... args) {
    160        if (!logger.isErrorEnabled()) {
    161            return;
    162        }
    163        String translatedMsg = imc.getMessage(key, args);
    164        MessageParameterObj mpo = new MessageParameterObj(key, args);
    165
    166        if (instanceofLAL) {
    167            ((LocationAwareLogger) logger).log(LOCALIZED, FQCN, LocationAwareLogger.ERROR_INT, translatedMsg, args, null);
    168        } else {
    169            logger.error(LOCALIZED, translatedMsg, mpo);
    170        }
    171    }
    172
    173}