View Javadoc

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   */
25  package org.slf4j.cal10n;
26  
27  import org.slf4j.Logger;
28  import org.slf4j.Marker;
29  import org.slf4j.MarkerFactory;
30  import org.slf4j.ext.LoggerWrapper;
31  import org.slf4j.spi.LocationAwareLogger;
32  
33  import ch.qos.cal10n.IMessageConveyor;
34  import 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   */
42  public 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,
80            LocationAwareLogger.TRACE_INT, translatedMsg, args, null);
81      } else {
82        logger.trace(LOCALIZED, translatedMsg, mpo);
83      }
84    }
85  
86    /**
87     * Log a localized message at the DEBUG level.
88     * 
89     * @param key
90     *          the key used for localization
91     * @param args
92     *          optional arguments
93     */
94    public void debug(Enum<?> key, Object... args) {
95      if (!logger.isDebugEnabled()) {
96        return;
97      }
98      String translatedMsg = imc.getMessage(key, args);
99      MessageParameterObj mpo = new MessageParameterObj(key, args);
100 
101     if (instanceofLAL) {
102       ((LocationAwareLogger) logger).log(LOCALIZED, FQCN,
103           LocationAwareLogger.DEBUG_INT, translatedMsg, args, null);
104     } else {
105       logger.debug(LOCALIZED, translatedMsg, mpo);
106     }
107   }
108 
109   /**
110    * Log a localized message at the INFO level.
111    * 
112    * @param key
113    *          the key used for localization
114    * @param args
115    *          optional arguments
116    */
117   public void info(Enum<?> key, Object... args) {
118     if (!logger.isInfoEnabled()) {
119       return;
120     }
121     String translatedMsg = imc.getMessage(key, args);
122     MessageParameterObj mpo = new MessageParameterObj(key, args);
123 
124     if (instanceofLAL) {
125       ((LocationAwareLogger) logger).log(LOCALIZED, FQCN,
126           LocationAwareLogger.INFO_INT, translatedMsg, args, null);
127     } else {
128       logger.info(LOCALIZED, translatedMsg, mpo);
129     }
130   }
131 
132   /**
133    * Log a localized message at the WARN level.
134    * 
135    * @param key
136    *          the key used for localization
137    * @param args
138    *          optional arguments
139    */
140   public void warn(Enum<?> key, Object... args) {
141     if (!logger.isWarnEnabled()) {
142       return;
143     }
144     String translatedMsg = imc.getMessage(key, args);
145     MessageParameterObj mpo = new MessageParameterObj(key, args);
146 
147     if (instanceofLAL) {
148       ((LocationAwareLogger) logger).log(LOCALIZED, FQCN,
149           LocationAwareLogger.WARN_INT, translatedMsg, args, null);
150     } else {
151       logger.warn(LOCALIZED, translatedMsg, mpo);
152     }
153   }
154 
155   /**
156    * Log a localized message at the ERROR level.
157    * 
158    * @param key
159    *          the key used for localization
160    * @param args
161    *          optional arguments
162    */
163   public void error(Enum<?> key, Object... args) {
164     if (!logger.isErrorEnabled()) {
165       return;
166     }
167     String translatedMsg = imc.getMessage(key, args);
168     MessageParameterObj mpo = new MessageParameterObj(key, args);
169 
170     if (instanceofLAL) {
171       ((LocationAwareLogger) logger).log(LOCALIZED, FQCN,
172           LocationAwareLogger.ERROR_INT, translatedMsg, args, null);
173     } else {
174       logger.error(LOCALIZED, translatedMsg, mpo);
175     }
176   }
177 
178 }