Reputation: 81667
I have a log4j.properties
file where I have to define ~ 12 different appenders. In fact, all of these appenders are similar, except for the name and the File
attribute.
I have something like that:
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
As you can see, all lines are identical except log4j.logger.X
and log4j.appender.fileX.File
Is there a way to define default values for others attributes (MaxFileSize
, MaxBackupIndex
, layout
, etc.) of the appenders, something like an "inheritance" of appender?
Thanks
Upvotes: 2
Views: 88
Reputation: 4232
Try to implement your custom default Appender
and then use it instead 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"));
}
}
Upvotes: 1