4 * License : The MIT License
5 * Copyright(c) 2009 olyutorskii
8 package jp.sfjp.jindolf.log;
10 import java.util.logging.Level;
11 import java.util.logging.LogRecord;
12 import java.util.logging.Logger;
16 * 現時点では java.util.logging のみサポート。
18 public class LogWrapper{
20 private final Logger jre14Logger;
24 * @param logger ラップ対象のjava.util.loggingロガー
26 public LogWrapper(Logger logger){
28 if(logger == null) throw new NullPointerException();
29 this.jre14Logger = logger;
38 this(Logger.getAnonymousLogger());
43 * ラップ対象のjava.util.loggingロガーを取得する。
44 * @return ラップ対象のjava.util.loggingロガー
46 public Logger getJre14Logger(){
47 return this.jre14Logger;
51 * ログレコードにスタックトレース情報を埋め込む。
52 * @param record ログレコード
54 private void fillStackInfo(LogRecord record){
55 Thread selfThread = Thread.currentThread();
56 StackTraceElement[] stacks = selfThread.getStackTrace();
58 String thisName = this.getClass().getName();
60 boolean foundMySelf = false;
61 for(StackTraceElement frame : stacks){
62 String frameClassName = frame.getClassName();
64 if( ! foundMySelf && frameClassName.equals(thisName) ){
69 if( foundMySelf && ! frameClassName.equals(thisName) ){
70 record.setSourceClassName(frameClassName);
71 record.setSourceMethodName(frame.getMethodName());
80 * java.util.loggingロガーへログ出力。
84 private void logJre14(Level level, CharSequence msg){
85 logJre14(level, msg, null);
90 * java.util.loggingロガーへログ出力。
95 private void logJre14(Level level, CharSequence msg, Throwable thrown){
97 if(msg == null) message = null;
98 else message = msg.toString();
100 LogRecord record = new LogRecord(level, message);
103 record.setThrown(thrown);
106 fillStackInfo(record);
108 this.jre14Logger.log(record);
117 public void info(CharSequence msg){
118 logJre14(Level.INFO, msg);
126 public void warn(CharSequence msg){
136 public void warn(CharSequence msg, Throwable thrown){
137 logJre14(Level.WARNING, msg, thrown);
145 public void fatal(CharSequence msg){
155 public void fatal(CharSequence msg, Throwable thrown){
156 logJre14(Level.SEVERE, msg, thrown);
160 // TODO Apache log4j サポート