1 /*****************************************************************************
5 * Creation date 02.10.2012
8 *****************************************************************************/
10 package org.splat.log;
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.splat.i18n.I18nUtils;
19 * This class supply methods to log app messages
20 * @author Maria KRUCHININA
24 public class AppLogger {
26 // logger common logging
28 protected transient Log _logger = null;
31 * Constructor with a class param.
33 * @param categorie criteria to choose the logger to use
37 public static AppLogger getLogger(final Class<?> categorie) {
38 return new AppLogger(LogFactory.getLog(categorie));
42 * Constructor with a String param.
44 * @param categorie the categorie
48 public static AppLogger getLogger(final String categorie) {
49 return new AppLogger(LogFactory.getLog(categorie));
53 * protected parameter.
55 * @param logger the logger
57 protected AppLogger(final Log logger) {
64 * Log a fatal message.
66 * @param code the message code/key
67 * @param context the message context (used to complete {0..n} elements)
69 public void fatal(final String code, final Object... context) {
70 _logger.fatal(formatMessage(code, context));
74 * Log a fatal message.
76 * @param code the message code/key
77 * @param exception the exception which originated the message
78 * @param context the message context (used to complete {0..n} elements)
80 public void fatal(final String code, final Exception exception, final Object... context) {
81 _logger.fatal(formatMessage(code, context), exception);
85 * Log a fatal message.
87 * @param msg the message
90 public void fatalMsg(final String msg, final Exception exc) {
91 _logger.fatal(msg, exc);
95 * Log a fatal message.
97 * @param msg the message
99 public void fatalMsg(final String msg) {
106 * Log an error message.
108 * @param code the message code/key
109 * @param context the message context (used to complete {0..n} elements)
111 public void error(final String code, final Object... context) {
112 _logger.error(formatMessage(code, context));
116 * Log an error message.
118 * @param code the message code/key
119 * @param exception the exception which originated the message
120 * @param context the message context (used to complete {0..n} elements)
122 public void error(final String code, final Exception exception, final Object... context) {
123 _logger.error(formatMessage(code, context), exception);
127 * Log an error message.
129 * @param msg the message
131 public void errorMsg(final String msg) {
136 * Log an error message.
138 * @param msg the message
139 * @param exception the exception
141 public void errorMsg(final String msg, final Throwable exception) {
142 _logger.error(msg, exception);
148 * Log a warning message.
150 * @param code the message code/key
151 * @param context the message context (used to complete {0..n} elements)
153 public void warn(final String code, final Object... context) {
154 _logger.warn(formatMessage(code, context));
158 * Log a warning message.
160 * @param code the message code/key
161 * @param exception the exception which originated the message
162 * @param context the message context (used to complete {0..n} elements)
164 public void warn(final String code, final Exception exception, final Object... context) {
165 _logger.warn(formatMessage(code, context), exception);
169 * Log an error message.
171 * @param msg the message
173 public void warnMsg(final String msg) {
178 * Log an error message.
180 * @param msg the message
181 * @param exception the exception
183 public void warnMsg(final String msg, final Exception exception) {
184 _logger.warn(msg, exception);
191 * Log an info message.
193 * @param code the message code/key
194 * @param context the message context (used to complete {0..n} elements)
196 public void info(final String code, final Object... context) {
197 _logger.info(formatMessage(code, context));
201 * Log an info message.
203 * @param code the message code/key
204 * @param exception the exception which originated the message
205 * @param context the message context (used to complete {0..n} elements)
207 public void info(final String code, final Exception exception, final Object... context) {
208 _logger.info(formatMessage(code, context), exception);
212 * Allows to check if INFO logging level is enabled.
214 * @return <b>true</b> if INFO logging level is enabled and <b>false</b> otherwise
216 public boolean isInfoEnabled() {
217 return _logger.isInfoEnabled();
221 * Allows to check if ERROR logging level is enabled.
223 * @return <b>true</b> if ERROR logging level is enabled and <b>false</b> otherwise
225 public boolean isErrorEnabled() {
226 return _logger.isErrorEnabled();
230 * Allows to check if WARN logging level is enabled.
232 * @return <b>true</b> if WARN logging level is enabled and <b>false</b> otherwise
234 public boolean isWarnEnabled() {
235 return _logger.isWarnEnabled();
241 * Allows to check if DEBUG logging level is enabled.
243 * @return <b>true</b> if DEBUG logging level is enabled and <b>false</b> otherwise
245 public boolean isDebugEnabled() {
246 return _logger.isDebugEnabled();
250 * Log a debug message.
252 * @param exception the exception which originated the message
253 * @param message the message
255 public void debug(final String message, final Throwable exception) {
256 _logger.debug(message, exception);
260 * Log a debug message.
262 * @param message the message
264 public void debug(final String message) {
265 _logger.debug(message);
269 * Log a debug message.
271 * @param context the message context (used to complete {0..n} elements)
272 * @param messageFormat the message format
274 public void debug(final String messageFormat, final Object... context) {
275 _logger.debug(String.format(messageFormat, context));
280 * format the message from the code and context.
282 * @param code the message code/key
283 * @param context the message context (used to complete {0..n} elements)
285 * @return the formated message
287 public String formatMessage(final String code, final Object... context) {
288 String translatedMessage;
289 String tempCode = code;
291 translatedMessage = I18nUtils.getMessageLocaleDefault(code, context);
292 } catch (Exception e) {
293 translatedMessage = "Translation error for code: ?" + code + "?";
294 _logger.error(translatedMessage, e);
296 if (tempCode == null) {
300 return new StringBuffer(tempCode).append(": ").append(translatedMessage).toString();