View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.uruma.log;
17  
18  import java.net.URL;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.log4j.PropertyConfigurator;
25  import org.apache.log4j.xml.DOMConfigurator;
26  import org.seasar.framework.message.MessageFormatter;
27  import org.seasar.uruma.core.UrumaConstants;
28  import org.seasar.uruma.util.AssertionUtil;
29  
30  /**
31   * ログ出力を提供するクラスです。<br />
32   * 
33   * @author y-komori
34   */
35  public final class UrumaLogger {
36  
37      private static final Map<Class<?>, UrumaLogger> loggers = new HashMap<Class<?>, UrumaLogger>();
38  
39      private static boolean initialized;
40  
41      private final Log log;
42  
43      /**
44       * {@link UrumaLogger} を返します。<br />
45       * 
46       * @param clazz
47       *            {@link Class} オブジェクト
48       * @return {@link UrumaLogger} オブジェクト
49       */
50      public static synchronized UrumaLogger getLogger(final Class<?> clazz) {
51          if (!initialized) {
52              initialize();
53          }
54          UrumaLogger logger = loggers.get(clazz);
55          if (logger == null) {
56              logger = new UrumaLogger(clazz);
57              loggers.put(clazz, logger);
58          }
59          return logger;
60      }
61  
62      /**
63       * {@link UrumaLogger} を初期化します。<br />
64       */
65      public static synchronized void initialize() {
66          initialized = true;
67      }
68  
69      /**
70       * リソースを開放します。<br />
71       */
72      public synchronized static void dispose() {
73          LogFactory.releaseAll();
74          loggers.clear();
75          initialized = false;
76      }
77  
78      private UrumaLogger(final Class<?> clazz) {
79          log = LogFactory.getLog(clazz);
80      }
81  
82      /**
83       * XML ファイル形式のコンフィグレーションを追加します。<br />
84       * コンフィグレーションファイルの書式は log4j に準じます。
85       * 
86       * @param config
87       *            コンフィグレーションファイルの {@link URL}
88       */
89      public void addXmlConfig(final URL config) {
90          AssertionUtil.assertNotNull("config", config);
91          DOMConfigurator.configure(config);
92      }
93  
94      /**
95       * properties ファイル形式のコンフィグレーションを追加します。<br />
96       * コンフィグレーションファイルの書式は log4j に準じます。
97       * 
98       * @param config
99       *            コンフィグレーションファイルの {@link URL}
100      */
101     public void addPropertyConfig(final URL config) {
102         AssertionUtil.assertNotNull("config", config);
103         PropertyConfigurator.configure(config);
104     }
105 
106     /**
107      * TRACE情報が出力されるかどうかを返します。<br />
108      * 
109      * @return TRACE情報が出力されるかどうか
110      */
111     public final boolean isTraceEnabled() {
112         return log.isTraceEnabled();
113     }
114 
115     /**
116      * TRACE情報を出力します。<br />
117      * 
118      * @param message
119      *            メッセージ
120      * @param throwable
121      *            {@link Throwable} オブジェクト
122      */
123     public final void trace(final Object message, final Throwable throwable) {
124         if (isTraceEnabled()) {
125             log.trace(message, throwable);
126         }
127     }
128 
129     /**
130      * TRACE情報を出力します。<br />
131      * 
132      * @param message
133      *            メッセージ
134      */
135     public final void trace(final Object message) {
136         if (isTraceEnabled()) {
137             log.trace(message);
138         }
139     }
140 
141     /**
142      * DEBUG情報が出力されるかどうかを返します。<br />
143      * 
144      * @return DEBUG情報が出力されるかどうか
145      */
146     public final boolean isDebugEnabled() {
147         return log.isDebugEnabled();
148     }
149 
150     /**
151      * DEBUG情報を出力します。<br />
152      * 
153      * @param message
154      *            メッセージ
155      * @param throwable
156      *            {@link Throwable} オブジェクト
157      */
158     public final void debug(final Object message, final Throwable throwable) {
159         if (isDebugEnabled()) {
160             log.debug(message, throwable);
161         }
162     }
163 
164     /**
165      * DEBUG情報を出力します。<br />
166      * 
167      * @param message
168      *            メッセージ
169      */
170     public final void debug(final Object message) {
171         if (isDebugEnabled()) {
172             log.debug(message);
173         }
174     }
175 
176     /**
177      * INFO情報が出力されるかどうかを返します。<br />
178      * 
179      * @return INFO情報が出力されるかどうか
180      */
181     public final boolean isInfoEnabled() {
182         return log.isInfoEnabled();
183     }
184 
185     /**
186      * INFO情報を出力します。<br />
187      * 
188      * @param message
189      *            メッセージ
190      * @param throwable
191      *            {@link Throwable} オブジェクト
192      */
193     public final void info(final Object message, final Throwable throwable) {
194         if (isInfoEnabled()) {
195             log.info(message, throwable);
196         }
197     }
198 
199     /**
200      * INFO情報を出力します。<br />
201      * 
202      * @param message
203      *            メッセージ
204      */
205     public final void info(final Object message) {
206         if (isInfoEnabled()) {
207             log.info(message);
208         }
209     }
210 
211     /**
212      * WARN情報を出力します。<br />
213      * 
214      * @param message
215      *            メッセージ
216      * @param throwable
217      *            {@link Throwable} オブジェクト
218      */
219     public final void warn(final Object message, final Throwable throwable) {
220         log.warn(message, throwable);
221     }
222 
223     /**
224      * WARN情報を出力します。<br />
225      * 
226      * @param message
227      *            メッセージ
228      */
229     public final void warn(final Object message) {
230         log.warn(message);
231     }
232 
233     /**
234      * ERROR情報を出力します。<br />
235      * 
236      * @param message
237      *            メッセージ
238      * @param throwable
239      *            {@link Throwable} オブジェクト
240      */
241     public final void error(final Object message, final Throwable throwable) {
242         log.error(message, throwable);
243     }
244 
245     /**
246      * ERROR情報を出力します。<br />
247      * 
248      * @param message
249      *            メッセージ
250      */
251     public final void error(final Object message) {
252         log.error(message);
253     }
254 
255     /**
256      * FATAL情報を出力します。<br />
257      * 
258      * @param message
259      *            メッセージ
260      * @param throwable
261      *            {@link Throwable} オブジェクト
262      */
263     public final void fatal(final Object message, final Throwable throwable) {
264         log.fatal(message, throwable);
265     }
266 
267     /**
268      * FATAL情報を出力します。<br />
269      * 
270      * @param message
271      *            メッセージ
272      */
273     public final void fatal(final Object message) {
274         log.fatal(message);
275     }
276 
277     /**
278      * ログを出力します。<br />
279      * 
280      * @param throwable
281      *            {@link Throwable} オブジェクト
282      */
283     public final void log(final Throwable throwable) {
284         error(throwable.getMessage(), throwable);
285     }
286 
287     /**
288      * ログを出力します。<br />
289      * 
290      * @param messageCode
291      *            メッセージコード
292      * @param args
293      *            引数
294      */
295     public final void log(final String messageCode, final Object... args) {
296         log(messageCode, null, args);
297     }
298 
299     /**
300      * ログを出力します。<br />
301      * 
302      * @param messageCode
303      *            メッセージコード
304      * @param throwable
305      *            {@link Throwable} オブジェクト
306      * @param args
307      *            引数
308      */
309     public final void log(final String messageCode, final Throwable throwable,
310             final Object... args) {
311         char messageType = messageCode.charAt(0);
312         if (isEnabledFor(messageType)) {
313             String message = MessageFormatter.getMessage(messageCode, args);
314 
315             switch (messageType) {
316             case 'T':
317                 log.trace(message, throwable);
318                 break;
319             case 'D':
320                 log.debug(message, throwable);
321                 break;
322             case 'I':
323                 log.info(message, throwable);
324                 break;
325             case 'W':
326                 log.warn(message, throwable);
327                 break;
328             case 'E':
329                 log.error(message, throwable);
330                 break;
331             case 'F':
332                 log.fatal(message, throwable);
333                 break;
334             default:
335                 throw new IllegalArgumentException(String.valueOf(messageType));
336             }
337         }
338     }
339 
340     private boolean isEnabledFor(final char messageType) {
341         switch (messageType) {
342         case 'T':
343             return log.isTraceEnabled();
344         case 'D':
345             return log.isDebugEnabled();
346         case 'I':
347             return log.isInfoEnabled();
348         case 'W':
349             return log.isWarnEnabled();
350         case 'E':
351             return log.isErrorEnabled();
352         case 'F':
353             return log.isFatalEnabled();
354         default:
355             throw new IllegalArgumentException(String.valueOf(messageType));
356         }
357     }
358 
359     /**
360      * オブジェクトの詳細情報を返します。<br />
361      * 
362      * @param obj
363      *            オブジェクト
364      * @return 詳細情報
365      */
366     public static final String getObjectDescription(final Object obj) {
367         if (obj != null) {
368             return obj.getClass().getName() + UrumaConstants.AT_MARK
369                     + Integer.toHexString(obj.hashCode());
370         } else {
371             return "NULL";
372         }
373     }
374 }