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);
214 * Allows to check if DEBUG logging level is enabled.
216 * @return <b>true</b> if DEBUG logging level is enabled and <b>false</b> otherwise
218 public boolean isDebugEnabled() {
219 return _logger.isDebugEnabled();
223 * Log a debug message.
225 * @param exception the exception which originated the message
226 * @param message the message
228 public void debug(final String message, final Exception exception) {
229 _logger.debug(message, exception);
233 * Log a debug message.
235 * @param message the message
237 public void debug(final String message) {
238 _logger.debug(message);
242 * Log a debug message.
244 * @param context the message context (used to complete {0..n} elements)
245 * @param messageFormat the message format
247 public void debug(final String messageFormat, final Object... context) {
248 _logger.debug(String.format(messageFormat, context));
253 * format the message from the code and context.
255 * @param code the message code/key
256 * @param context the message context (used to complete {0..n} elements)
258 * @return the formated message
260 public String formatMessage(final String code, final Object... context) {
261 String translatedMessage;
262 String tempCode = code;
264 translatedMessage = I18nUtils.getMessageLocaleDefault(code, context);
265 } catch (Exception e) {
266 translatedMessage = "Translation error for code: ?" + code + "?";
267 _logger.error(translatedMessage, e);
269 if (tempCode == null) {
273 return new StringBuffer(tempCode).append(": ").append(translatedMessage).toString();