View Javadoc

1   /*
2    * Copyright (c) 2004-2009 QOS.ch All rights reserved.
3    * 
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   * 
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   * 
14   * THE SOFTWARE IS PROVIDED "AS  IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  package org.slf4j.cal10n;
23  
24  import org.slf4j.Logger;
25  import org.slf4j.Marker;
26  import org.slf4j.MarkerFactory;
27  import org.slf4j.ext.LoggerWrapper;
28  import org.slf4j.spi.LocationAwareLogger;
29  
30  import ch.qos.cal10n.IMessageConveyor;
31  import ch.qos.cal10n.MessageParameterObj;
32  
33  /**
34   * A logger specialized in localized logging. Localization is based in the <a
35   * href="http://cal10n.qos.ch">CAL10N project</p>.
36   * 
37   * @author Ceki G&uuml;lc&uuml;
38   */
39  public class LocLogger extends LoggerWrapper implements Logger {
40  
41    private static final String FQCN = LocLogger.class.getName();
42  
43    /**
44     * Every localized message logged by a LocLogger will bear this marker. It
45     * allows marker-aware implementations to perform additional processing on
46     * localized messages.
47     */
48    static Marker LOCALIZED = MarkerFactory.getMarker("LOCALIZED");
49  
50    final IMessageConveyor imc;
51  
52    public LocLogger(Logger logger,  IMessageConveyor imc) {
53      super(logger, LoggerWrapper.class.getName());
54      if(imc == null) {
55        throw new IllegalArgumentException("IMessageConveyor cannot be null");
56      }
57      this.imc = imc;
58    }
59  
60    /**
61     * Log a localized message at the TRACE level.
62     * 
63     * @param key
64     *          the key used for localization
65     * @param args
66     *          optional arguments
67     */
68    public void trace(Enum<?> key, Object... args) {
69      if (!logger.isTraceEnabled()) {
70        return;
71      }
72      String translatedMsg = imc.getMessage(key, args);
73      MessageParameterObj mpo = new MessageParameterObj(key, args);
74  
75      if (instanceofLAL) {
76        ((LocationAwareLogger) logger).log(LOCALIZED, FQCN,
77            LocationAwareLogger.TRACE_INT, translatedMsg, null);
78      } else {
79        logger.trace(LOCALIZED, translatedMsg, mpo);
80      }
81    }
82  
83    /**
84     * Log a localized message at the DEBUG level.
85     * 
86     * @param key
87     *          the key used for localization
88     * @param args
89     *          optional arguments
90     */
91    public void debug(Enum<?> key, Object... args) {
92      if (!logger.isDebugEnabled()) {
93        return;
94      }
95      String translatedMsg = imc.getMessage(key, args);
96      MessageParameterObj mpo = new MessageParameterObj(key, args);
97  
98      if (instanceofLAL) {
99        ((LocationAwareLogger) logger).log(LOCALIZED, FQCN,
100           LocationAwareLogger.DEBUG_INT, translatedMsg, null);
101     } else {
102       logger.debug(LOCALIZED, translatedMsg, mpo);
103     }
104   }
105 
106   /**
107    * Log a localized message at the INFO level.
108    * 
109    * @param key
110    *          the key used for localization
111    * @param args
112    *          optional arguments
113    */
114   public void info(Enum<?> key, Object... args) {
115     if (!logger.isInfoEnabled()) {
116       return;
117     }
118     String translatedMsg = imc.getMessage(key, args);
119     MessageParameterObj mpo = new MessageParameterObj(key, args);
120 
121     if (instanceofLAL) {
122       ((LocationAwareLogger) logger).log(LOCALIZED, FQCN,
123           LocationAwareLogger.INFO_INT, translatedMsg, 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,
146           LocationAwareLogger.WARN_INT, translatedMsg, null);
147     } else {
148       logger.warn(LOCALIZED, translatedMsg, mpo);
149     }
150   }
151 
152   /**
153    * Log a localized message at the ERROR level.
154    * 
155    * @param key
156    *          the key used for localization
157    * @param args
158    *          optional arguments
159    */
160   public void error(Enum<?> key, Object... args) {
161     if (!logger.isErrorEnabled()) {
162       return;
163     }
164     String translatedMsg = imc.getMessage(key, args);
165     MessageParameterObj mpo = new MessageParameterObj(key, args);
166 
167     if (instanceofLAL) {
168       ((LocationAwareLogger) logger).log(LOCALIZED, FQCN,
169           LocationAwareLogger.ERROR_INT, translatedMsg, null);
170     } else {
171       logger.error(LOCALIZED, translatedMsg, mpo);
172     }
173   }
174 
175 }