Salome HOME
Fixed processing of runtime (Throwable) exceptions.
[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         // Debug
212
213         /**
214          * Allows to check if DEBUG logging level is enabled.
215          * 
216          * @return <b>true</b> if DEBUG logging level is enabled and <b>false</b> otherwise 
217          */
218         public boolean isDebugEnabled() {
219                 return _logger.isDebugEnabled();
220         }
221
222         /**
223          * Log a debug message.
224          * 
225          * @param exception the exception which originated the message
226          * @param message the message
227          */
228         public void debug(final String message, final Exception exception) {
229                 _logger.debug(message, exception);
230         }
231
232         /**
233          * Log a debug message.
234          * 
235          * @param message the message
236          */
237         public void debug(final String message) {
238                 _logger.debug(message);
239         }
240
241         /**
242          * Log a debug message.
243          * 
244          * @param context the message context (used to complete {0..n} elements)
245          * @param messageFormat the message format
246          */
247         public void debug(final String messageFormat, final Object... context) {
248                 _logger.debug(String.format(messageFormat, context));
249         }
250
251         // Utils methods
252         /**
253          * format the message from the code and context.
254          * 
255          * @param code the message code/key
256          * @param context the message context (used to complete {0..n} elements)
257          * 
258          * @return the formated message
259          */
260         public String formatMessage(final String code, final Object... context) {
261                 String translatedMessage;
262                 String tempCode = code;
263                 try {
264                         translatedMessage = I18nUtils.getMessageLocaleDefault(code, context);
265                 } catch (Exception e) {
266                         translatedMessage = "Translation error for code: ?" + code + "?";
267                         _logger.error(translatedMessage, e);
268
269                         if (tempCode == null) {
270                                 tempCode = "null";
271                         }
272                 }
273                 return new StringBuffer(tempCode).append(": ").append(translatedMessage).toString();
274         }
275 }