0
我有Log4j的擴展Logger類。我只想要訪問log4j.properties文件中的屬性。 String dateFormat; 以下是我的代碼。在擴展日誌記錄類中訪問log4j屬性
public class TestLogger extends Logger {
final Logger target;
String dateFormat; //Need datePatttern from properties file
SimpleDateFormat format = new SimpleDateFormat("yyyyddMM-HH:mm:ss");
public TestLogger (Logger target) {
super("");
this.target = target;
/**
* Some thing that will give me **DateAppender** property
* to store in variable 'dateFormat'
*/
}
@SuppressWarnings("unused")
public void error(Object message, String errCode, Throwable t) {
Logger logger = Logger.getLogger(TestLogger.class);
Appender appender = logger.getAppender("filer");
Date date = new Date();
String msg = String.format("%1$-11s %2$-4s %3$-17s %4$s","ERROR",errCode,format.format(date),message+": "+t.getMessage());
target.error(msg,t);
}
Log4j.properties文件
log4j.rootLogger=DEBUG,stdout,filer,error
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.datePattern='.'MM/dd/yyyy
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyyddMM-HH:mm:ss} %m%n
log4j.appender.filer=org.apache.log4j.RollingFileAppender
log4j.appender.filer.layout=org.apache.log4j.PatternLayout
log4j.appender.filer.layout.ConversionPattern=%m%n
log4j.appender.filer.File=./trace.log
log4j.appender.filer.MaxFileSize=5MB
log4j.appender.filer.MaxBackupIndex=5
log4j.appender.filer.append=true