OSDN Git Service

Maven3対応。
[jindolf/Jindolf.git] / src / main / java / jp / sfjp / jindolf / log / LogWrapper.java
1 /*
2  * log wrapper
3  *
4  * License : The MIT License
5  * Copyright(c) 2009 olyutorskii
6  */
7
8 package jp.sfjp.jindolf.log;
9
10 import java.util.logging.Level;
11 import java.util.logging.LogRecord;
12 import java.util.logging.Logger;
13
14 /**
15  * 各種ログAPIへの共通ラッパー。
16  * 現時点では java.util.logging のみサポート。
17  */
18 public class LogWrapper{
19
20     private final Logger jre14Logger;
21
22     /**
23      * コンストラクタ。
24      * @param logger ラップ対象のjava.util.loggingロガー
25      */
26     public LogWrapper(Logger logger){
27         super();
28         if(logger == null) throw new NullPointerException();
29         this.jre14Logger = logger;
30         return;
31     }
32
33     /**
34      * コンストラクタ。
35      * 新規生成された匿名ロガーを囲う。
36      */
37     public LogWrapper(){
38         this(Logger.getAnonymousLogger());
39         return;
40     }
41
42     /**
43      * ラップ対象のjava.util.loggingロガーを取得する。
44      * @return ラップ対象のjava.util.loggingロガー
45      */
46     public Logger getJre14Logger(){
47         return this.jre14Logger;
48     }
49
50     /**
51      * ログレコードにスタックトレース情報を埋め込む。
52      * @param record ログレコード
53      */
54     private void fillStackInfo(LogRecord record){
55         Thread selfThread = Thread.currentThread();
56         StackTraceElement[] stacks = selfThread.getStackTrace();
57
58         String thisName = this.getClass().getName();
59
60         boolean foundMySelf = false;
61         for(StackTraceElement frame : stacks){
62             String frameClassName = frame.getClassName();
63
64             if( ! foundMySelf && frameClassName.equals(thisName) ){
65                 foundMySelf = true;
66                 continue;
67             }
68
69             if( foundMySelf &&  ! frameClassName.equals(thisName) ){
70                 record.setSourceClassName(frameClassName);
71                 record.setSourceMethodName(frame.getMethodName());
72                 break;
73             }
74         }
75
76         return;
77     }
78
79     /**
80      * java.util.loggingロガーへログ出力。
81      * @param level ログレベル
82      * @param msg メッセージ
83      */
84     private void logJre14(Level level, CharSequence msg){
85         logJre14(level, msg, null);
86         return;
87     }
88
89     /**
90      * java.util.loggingロガーへログ出力。
91      * @param level ログレベル
92      * @param msg メッセージ
93      * @param thrown 例外
94      */
95     private void logJre14(Level level, CharSequence msg, Throwable thrown){
96         String message;
97         if(msg == null) message = null;
98         else            message = msg.toString();
99
100         LogRecord record = new LogRecord(level, message);
101
102         if(thrown != null){
103             record.setThrown(thrown);
104         }
105
106         fillStackInfo(record);
107
108         this.jre14Logger.log(record);
109
110         return;
111     }
112
113     /**
114      * 単純な情報を出力する。
115      * @param msg メッセージ
116      */
117     public void info(CharSequence msg){
118         logJre14(Level.INFO, msg);
119         return;
120     }
121
122     /**
123      * 警告を出力する。
124      * @param msg メッセージ
125      */
126     public void warn(CharSequence msg){
127         warn(msg, null);
128         return;
129     }
130
131     /**
132      * 警告を出力する。
133      * @param msg メッセージ
134      * @param thrown 例外
135      */
136     public void warn(CharSequence msg, Throwable thrown){
137         logJre14(Level.WARNING, msg, thrown);
138         return;
139     }
140
141     /**
142      * 致命的な障害情報を出力する。
143      * @param msg メッセージ
144      */
145     public void fatal(CharSequence msg){
146         fatal(msg, null);
147         return;
148     }
149
150     /**
151      * 致命的な障害情報を出力する。
152      * @param msg メッセージ
153      * @param thrown 例外
154      */
155     public void fatal(CharSequence msg, Throwable thrown){
156         logJre14(Level.SEVERE, msg, thrown);
157         return;
158     }
159
160     // TODO Apache log4j サポート
161 }