2,351 バイト追加
、 2020年2月15日 (土) 07:34
==log4j 設定==
====log4j.properties 設定例====
### Stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MMM-dd HH:mm:ss} %5p - %m%n
### DailyRolling Log ###
log4j.appender.dailyfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyfile.File=c:/Program Files/eclipse3.1/workspace/fugitive/log/fugitive.log
log4j.appender.dailyfile.DatePattern='.'yyyy-MM-dd
log4j.appender.dailyfile.layout=org.apache.log4j.PatternLayout
log4j.appender.dailyfile.layout.ConversionPattern=%d{yyyy-MMM-dd HH:mm:ss} %5p - %m%n
### Rolling Log ###
log4j.appender.rollfile=org.apache.log4j.RollingFileAppender
log4j.appender.rollfile.File=c:/Program Files/eclipse3.1/workspace/fugitive/log/fugitive.log
log4j.appender.rollfile.MaxFileSize=5MB
log4j.appender.rollfile.MaxBackupIndex=5
log4j.appender.rollfile.layout=org.apache.log4j.PatternLayout
log4j.appender.rollfile.layout.ConversionPattern=%d{yyyy-MMM-dd HH:mm:ss} %5p - %m%n
### Logger Setting (for debugging) ###
#log4j.rootLogger=debug,stdout,rollfile
#log4j.logger.info.typea.testApp=debug,stdout,rollfile
### Logger Setting ###
log4j.logger.info.typea.testApp=info,rollfile
====設定ファイルを指定する====
http://www.nurs.or.jp/~sug/soft/log4j/log4j3.htm
=====プログラム上で、明示的にプロパティファイルを指定するためのクラス。=====
import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class LogUtil {
static {
try {
Properties prop = new Properties();
String url = "/" + LogUtil.class.getPackage().getName().replaceAll("\\.", "/")
+ "/log4j.properties";
prop.load(LogUtil.class.getResourceAsStream(url));
PropertyConfigurator.configure(prop);
} catch (Exception re) {
re.printStackTrace();
}
}
private LogUtil() {}
/**
* Log4Jロガーを取得する
* @param clazz
* @return
*/
public static Logger getLogger(Class clazz) {
return Logger.getLogger(clazz);
}
}
=====使用法=====
import org.apache.log4j.Logger;
protected Logger log = LogUtil.getLogger(Dao.class);