Salome HOME
Study validation cycle operations are implemented according to the specification.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / log / AppLogger.java
1 /*****************************************************************************
2  * Company         OPEN CASCADE
3  * Application     SIMAN
4  * File            $Id$ 
5  * Creation date   02.10.2012
6  * @author         $Author$
7  * @version        $Revision$
8  *****************************************************************************/
9
10 package org.splat.log; 
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.splat.i18n.I18nUtils;
15
16 /**
17  * 
18  * Application Logger.
19  * This class supply methods to log app messages
20  * @author Maria KRUCHININA
21  *
22  */
23
24 public class AppLogger {
25
26         // logger common logging
27         /** The logger. */
28         protected transient Log _logger = null;
29
30         /**
31          * Constructor with a class param.
32          * 
33          * @param categorie criteria to choose the logger to use
34          * 
35          * @return the logger
36          */
37         public static AppLogger getLogger(final Class<?> categorie) {
38                 return new AppLogger(LogFactory.getLog(categorie));
39         }
40
41         /**
42          * Constructor with a String param.
43          * 
44          * @param categorie the categorie
45          * 
46          * @return the logger
47          */
48         public static AppLogger getLogger(final String categorie) {
49                 return new AppLogger(LogFactory.getLog(categorie));
50         }
51
52         /**
53          * protected parameter.
54          * 
55          * @param logger the logger
56          */
57         protected AppLogger(final Log logger) {
58                 _logger = logger;
59         }
60
61         // Fatal
62
63         /**
64          * Log a fatal message.
65          * 
66          * @param code the message code/key
67          * @param context the message context (used to complete {0..n} elements)
68          */
69         public void fatal(final String code, final Object... context) {
70                 _logger.fatal(formatMessage(code, context));
71         }
72
73         /**
74          * Log a fatal message.
75          * 
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)
79          */
80         public void fatal(final String code, final Exception exception, final Object... context) {
81                 _logger.fatal(formatMessage(code, context), exception);
82         }
83
84         /**
85          * Log a fatal message.
86          * 
87          * @param msg the message
88          * @param exc the exc
89          */
90         public void fatalMsg(final String msg, final Exception exc) {
91                 _logger.fatal(msg, exc);
92         }
93
94         /**
95          * Log a fatal message.
96          * 
97          * @param msg the message
98          */
99         public void fatalMsg(final String msg) {
100                 _logger.fatal(msg);
101         }
102
103         // Error
104
105         /**
106          * Log an error message.
107          * 
108          * @param code the message code/key
109          * @param context the message context (used to complete {0..n} elements)
110          */
111         public void error(final String code, final Object... context) {
112                 _logger.error(formatMessage(code, context));
113         }
114
115         /**
116          * Log an error message.
117          * 
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)
121          */
122         public void error(final String code, final Exception exception, final Object... context) {
123                 _logger.error(formatMessage(code, context), exception);
124         }
125
126         /**
127          * Log an error message.
128          * 
129          * @param msg the message
130          */
131         public void errorMsg(final String msg) {
132                 _logger.error(msg);
133         }
134
135         /**
136          * Log an error message.
137          * 
138          * @param msg the message
139          * @param exception the exception
140          */
141         public void errorMsg(final String msg, final Throwable exception) {
142                 _logger.error(msg, exception);
143         }
144
145         // Warn
146
147         /**
148          * Log a warning message.
149          * 
150          * @param code the message code/key
151          * @param context the message context (used to complete {0..n} elements)
152          */
153         public void warn(final String code, final Object... context) {
154                 _logger.warn(formatMessage(code, context));
155         }
156
157         /**
158          * Log a warning message.
159          * 
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)
163          */
164         public void warn(final String code, final Exception exception, final Object... context) {
165                 _logger.warn(formatMessage(code, context), exception);
166         }
167
168         /**
169          * Log an error message.
170          * 
171          * @param msg the message
172          */
173         public void warnMsg(final String msg) {
174                 _logger.warn(msg);
175         }
176
177         /**
178          * Log an error message.
179          * 
180          * @param msg the message
181          * @param exception the exception
182          */
183         public void warnMsg(final String msg, final Exception exception) {
184                 _logger.warn(msg, exception);
185         }
186
187         
188         // Info
189
190         /**
191          * Log an info message.
192          * 
193          * @param code the message code/key
194          * @param context the message context (used to complete {0..n} elements)
195          */
196         public void info(final String code, final Object... context) {
197                 _logger.info(formatMessage(code, context));
198         }
199
200         /**
201          * Log an info message.
202          * 
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)
206          */
207         public void info(final String code, final Exception exception, final Object... context) {
208                 _logger.info(formatMessage(code, context), exception);
209         }
210
211         /**
212          * Allows to check if INFO logging level is enabled.
213          * 
214          * @return <b>true</b> if INFO logging level is enabled and <b>false</b> otherwise 
215          */
216         public boolean isInfoEnabled() {
217                 return _logger.isInfoEnabled();
218         }
219
220         /**
221          * Allows to check if ERROR logging level is enabled.
222          * 
223          * @return <b>true</b> if ERROR logging level is enabled and <b>false</b> otherwise 
224          */
225         public boolean isErrorEnabled() {
226                 return _logger.isErrorEnabled();
227         }
228
229         /**
230          * Allows to check if WARN logging level is enabled.
231          * 
232          * @return <b>true</b> if WARN logging level is enabled and <b>false</b> otherwise 
233          */
234         public boolean isWarnEnabled() {
235                 return _logger.isWarnEnabled();
236         }
237
238         // Debug
239
240         /**
241          * Allows to check if DEBUG logging level is enabled.
242          * 
243          * @return <b>true</b> if DEBUG logging level is enabled and <b>false</b> otherwise 
244          */
245         public boolean isDebugEnabled() {
246                 return _logger.isDebugEnabled();
247         }
248
249         /**
250          * Log a debug message.
251          * 
252          * @param exception the exception which originated the message
253          * @param message the message
254          */
255         public void debug(final String message, final Throwable exception) {
256                 _logger.debug(message, exception);
257         }
258
259         /**
260          * Log a debug message.
261          * 
262          * @param message the message
263          */
264         public void debug(final String message) {
265                 _logger.debug(message);
266         }
267
268         /**
269          * Log a debug message.
270          * 
271          * @param context the message context (used to complete {0..n} elements)
272          * @param messageFormat the message format
273          */
274         public void debug(final String messageFormat, final Object... context) {
275                 _logger.debug(String.format(messageFormat, context));
276         }
277
278         // Utils methods
279         /**
280          * format the message from the code and context.
281          * 
282          * @param code the message code/key
283          * @param context the message context (used to complete {0..n} elements)
284          * 
285          * @return the formated message
286          */
287         public String formatMessage(final String code, final Object... context) {
288                 String translatedMessage;
289                 String tempCode = code;
290                 try {
291                         translatedMessage = I18nUtils.getMessageLocaleDefault(code, context);
292                 } catch (Exception e) {
293                         translatedMessage = "Translation error for code: ?" + code + "?";
294                         _logger.error(translatedMessage, e);
295
296                         if (tempCode == null) {
297                                 tempCode = "null";
298                         }
299                 }
300                 return new StringBuffer(tempCode).append(": ").append(translatedMessage).toString();
301         }
302 }