我有一个log4j.properties
文件,我必须定义~12个不同的appender。实际上,除了名称和File
属性之外,所有这些appender都是相似的。
我有类似的东西:
log4j.logger.FOO=DEBUG, fileFOO
log4j.appender.fileFOO=org.apache.log4j.RollingFileAppender
log4j.appender.fileFOO.File=/some/path/file-FOO.log
log4j.appender.fileFOO.MaxFileSize=5MB
log4j.appender.fileFOO.MaxBackupIndex=20
log4j.appender.fileFOO.layout=org.apache.log4j.PatternLayout
log4j.appender.fileFOO.layout.ConversionPattern=%d %-5p ~ %m%n
log4j.logger.BAR=DEBUG, fileBAR
log4j.appender.fileBAR=org.apache.log4j.RollingFileAppender
log4j.appender.fileBAR.File=/some/path/file-BAR.log
log4j.appender.fileBAR.MaxFileSize=5MB
log4j.appender.fileBAR.MaxBackupIndex=20
log4j.appender.fileBAR.layout=org.apache.log4j.PatternLayout
log4j.appender.fileBAR.layout.ConversionPattern=%d %-5p ~ %m%n
如您所见,除log4j.logger.X
和log4j.appender.fileX.File
有没有办法为appender的其他属性(MaxFileSize
,MaxBackupIndex
,layout
等)定义默认值,类似于appender的“继承”? / p>
由于
答案 0 :(得分:1)
尝试实施自定义默认Appender
,然后使用RollingFileAppender
log4j.logger.FOO=DEBUG, fileFOO
log4j.appender.fileFOO=me.project.MyRollingFileAppender
log4j.appender.fileFOO.File=/some/path/file-FOO.log
public class MyRollingFileAppender extends RollingFileAppender {
public MyRollingFileAppender() {
super();
init();
}
public MyRollingFileAppender(Layout layout, String filename) throws IOException {
super(layout, filename);
init();
}
public MyRollingFileAppender(Layout layout, String filename, boolean append)
throws IOException {
super(layout, filename, append);
init();
}
private void init() {
this.setMaxFileSize("5MB");
this.setMaxBackupIndex(20);
this.setLayout( new PatternLayout("%d %-5p ~ %m%n"));
}
}