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 */
25package org.slf4j.cal10n;
26
27import org.slf4j.Logger;
28import org.slf4j.Marker;
29import org.slf4j.MarkerFactory;
30import org.slf4j.ext.LoggerWrapper;
31import org.slf4j.spi.LocationAwareLogger;
32
33import ch.qos.cal10n.IMessageConveyor;
34import ch.qos.cal10n.MessageParameterObj;
35
36/**
37 * A logger specialized in localized logging. Localization is based in the <a
38 * href="http://cal10n.qos.ch">CAL10N project</p>.
39 *
40 * @author Ceki G&uuml;lc&uuml;
41 */
42public class LocLogger extends LoggerWrapper implements Logger {
43
44    private static final String FQCN = LocLogger.class.getName();
45
46    /**
47     * Every localized message logged by a LocLogger will bear this marker. It
48     * allows marker-aware implementations to perform additional processing on
49     * localized messages.
50     */
51    static Marker LOCALIZED = MarkerFactory.getMarker("LOCALIZED");
52
53    final IMessageConveyor imc;
54
55    public LocLogger(Logger logger, IMessageConveyor imc) {
56        super(logger, LoggerWrapper.class.getName());
57        if (imc == null) {
58            throw new IllegalArgumentException("IMessageConveyor cannot be null");
59        }
60        this.imc = imc;
61    }
62
63    /**
64     * Log a localized message at the TRACE level.
65     *
66     * @param key
67     *          the key used for localization
68     * @param args
69     *          optional arguments
70     */
71    public void trace(Enum<?> key, Object... args) {
72        if (!logger.isTraceEnabled()) {
73            return;
74        }
75        String translatedMsg = imc.getMessage(key, args);
76        MessageParameterObj mpo = new MessageParameterObj(key, args);
77
78        if (instanceofLAL) {
79            ((LocationAwareLogger) logger).log(LOCALIZED, FQCN, LocationAwareLogger.TRACE_INT, translatedMsg, args, null);
80        } else {
81            logger.trace(LOCALIZED, translatedMsg, mpo);
82        }
83    }
84
85    /**
86     * Log a localized message at the DEBUG level.
87     *
88     * @param key
89     *          the key used for localization
90     * @param args
91     *          optional arguments
92     */
93    public void debug(Enum<?> key, Object... args) {
94        if (!logger.isDebugEnabled()) {
95            return;
96        }
97        String translatedMsg = imc.getMessage(key, args);
98        MessageParameterObj mpo = new MessageParameterObj(key, args);
99
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}
174