Coverage Report - org.seasar.uruma.log.UrumaLogger
 
Classes in this File Line Coverage Branch Coverage Complexity
UrumaLogger
38%
34/89
21%
13/62
0
 
 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  4
     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  2184
         if (!initialized) {
 52  4
             initialize();
 53  
         }
 54  2184
         UrumaLogger logger = loggers.get(clazz);
 55  2184
         if (logger == null) {
 56  324
             logger = new UrumaLogger(clazz);
 57  324
             loggers.put(clazz, logger);
 58  
         }
 59  2184
         return logger;
 60  
     }
 61  
 
 62  
     /**
 63  
      * {@link UrumaLogger} を初期化します。<br />
 64  
      */
 65  
     public static synchronized void initialize() {
 66  4
         initialized = true;
 67  4
     }
 68  
 
 69  
     /**
 70  
      * リソースを開放します。<br />
 71  
      */
 72  
     public synchronized static void dispose() {
 73  0
         LogFactory.releaseAll();
 74  0
         loggers.clear();
 75  0
         initialized = false;
 76  0
     }
 77  
 
 78  324
     private UrumaLogger(final Class<?> clazz) {
 79  324
         log = LogFactory.getLog(clazz);
 80  324
     }
 81  
 
 82  
     /**
 83  
      * XML ファイル形式のコンフィグレーションを追加します。<br />
 84  
      * コンフィグレーションファイルの書式は log4j に準じます。
 85  
      * 
 86  
      * @param config
 87  
      *            コンフィグレーションファイルの {@link URL}
 88  
      */
 89  
     public void addXmlConfig(final URL config) {
 90  0
         AssertionUtil.assertNotNull("config", config);
 91  0
         DOMConfigurator.configure(config);
 92  0
     }
 93  
 
 94  
     /**
 95  
      * properties ファイル形式のコンフィグレーションを追加します。<br />
 96  
      * コンフィグレーションファイルの書式は log4j に準じます。
 97  
      * 
 98  
      * @param config
 99  
      *            コンフィグレーションファイルの {@link URL}
 100  
      */
 101  
     public void addPropertyConfig(final URL config) {
 102  0
         AssertionUtil.assertNotNull("config", config);
 103  0
         PropertyConfigurator.configure(config);
 104  0
     }
 105  
 
 106  
     /**
 107  
      * TRACE情報が出力されるかどうかを返します。<br />
 108  
      * 
 109  
      * @return TRACE情報が出力されるかどうか
 110  
      */
 111  
     public final boolean isTraceEnabled() {
 112  1624
         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  0
         if (isTraceEnabled()) {
 125  0
             log.trace(message, throwable);
 126  
         }
 127  0
     }
 128  
 
 129  
     /**
 130  
      * TRACE情報を出力します。<br />
 131  
      * 
 132  
      * @param message
 133  
      *            メッセージ
 134  
      */
 135  
     public final void trace(final Object message) {
 136  0
         if (isTraceEnabled()) {
 137  0
             log.trace(message);
 138  
         }
 139  0
     }
 140  
 
 141  
     /**
 142  
      * DEBUG情報が出力されるかどうかを返します。<br />
 143  
      * 
 144  
      * @return DEBUG情報が出力されるかどうか
 145  
      */
 146  
     public final boolean isDebugEnabled() {
 147  7196
         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  0
         if (isDebugEnabled()) {
 160  0
             log.debug(message, throwable);
 161  
         }
 162  0
     }
 163  
 
 164  
     /**
 165  
      * DEBUG情報を出力します。<br />
 166  
      * 
 167  
      * @param message
 168  
      *            メッセージ
 169  
      */
 170  
     public final void debug(final Object message) {
 171  0
         if (isDebugEnabled()) {
 172  0
             log.debug(message);
 173  
         }
 174  0
     }
 175  
 
 176  
     /**
 177  
      * INFO情報が出力されるかどうかを返します。<br />
 178  
      * 
 179  
      * @return INFO情報が出力されるかどうか
 180  
      */
 181  
     public final boolean isInfoEnabled() {
 182  124
         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  0
         if (isInfoEnabled()) {
 195  0
             log.info(message, throwable);
 196  
         }
 197  0
     }
 198  
 
 199  
     /**
 200  
      * INFO情報を出力します。<br />
 201  
      * 
 202  
      * @param message
 203  
      *            メッセージ
 204  
      */
 205  
     public final void info(final Object message) {
 206  0
         if (isInfoEnabled()) {
 207  0
             log.info(message);
 208  
         }
 209  0
     }
 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  0
         log.warn(message, throwable);
 221  0
     }
 222  
 
 223  
     /**
 224  
      * WARN情報を出力します。<br />
 225  
      * 
 226  
      * @param message
 227  
      *            メッセージ
 228  
      */
 229  
     public final void warn(final Object message) {
 230  0
         log.warn(message);
 231  0
     }
 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  0
         log.error(message, throwable);
 243  0
     }
 244  
 
 245  
     /**
 246  
      * ERROR情報を出力します。<br />
 247  
      * 
 248  
      * @param message
 249  
      *            メッセージ
 250  
      */
 251  
     public final void error(final Object message) {
 252  0
         log.error(message);
 253  0
     }
 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  0
         log.fatal(message, throwable);
 265  0
     }
 266  
 
 267  
     /**
 268  
      * FATAL情報を出力します。<br />
 269  
      * 
 270  
      * @param message
 271  
      *            メッセージ
 272  
      */
 273  
     public final void fatal(final Object message) {
 274  0
         log.fatal(message);
 275  0
     }
 276  
 
 277  
     /**
 278  
      * ログを出力します。<br />
 279  
      * 
 280  
      * @param throwable
 281  
      *            {@link Throwable} オブジェクト
 282  
      */
 283  
     public final void log(final Throwable throwable) {
 284  0
         error(throwable.getMessage(), throwable);
 285  0
     }
 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  3464
         log(messageCode, null, args);
 297  3464
     }
 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  3464
         char messageType = messageCode.charAt(0);
 312  3464
         if (isEnabledFor(messageType)) {
 313  124
             String message = MessageFormatter.getMessage(messageCode, args);
 314  
 
 315  124
             switch (messageType) {
 316  
             case 'T':
 317  0
                 log.trace(message, throwable);
 318  0
                 break;
 319  
             case 'D':
 320  0
                 log.debug(message, throwable);
 321  0
                 break;
 322  
             case 'I':
 323  0
                 log.info(message, throwable);
 324  0
                 break;
 325  
             case 'W':
 326  116
                 log.warn(message, throwable);
 327  116
                 break;
 328  
             case 'E':
 329  8
                 log.error(message, throwable);
 330  8
                 break;
 331  
             case 'F':
 332  0
                 log.fatal(message, throwable);
 333  0
                 break;
 334  
             default:
 335  0
                 throw new IllegalArgumentException(String.valueOf(messageType));
 336  
             }
 337  
         }
 338  3464
     }
 339  
 
 340  
     private boolean isEnabledFor(final char messageType) {
 341  3464
         switch (messageType) {
 342  
         case 'T':
 343  0
             return log.isTraceEnabled();
 344  
         case 'D':
 345  2780
             return log.isDebugEnabled();
 346  
         case 'I':
 347  560
             return log.isInfoEnabled();
 348  
         case 'W':
 349  116
             return log.isWarnEnabled();
 350  
         case 'E':
 351  8
             return log.isErrorEnabled();
 352  
         case 'F':
 353  0
             return log.isFatalEnabled();
 354  
         default:
 355  0
             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  428
         if (obj != null) {
 368  428
             return obj.getClass().getName() + UrumaConstants.AT_MARK
 369  
                     + Integer.toHexString(obj.hashCode());
 370  
         } else {
 371  0
             return "NULL";
 372  
         }
 373  
     }
 374  
 }