12
votes

I have multiple appenders in my log4j config, and I was wondering whether it would be possible to define the layout and conversion pattern once for all of them. I.e. I have

log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss} %-5p %c %x - %m%n

log4j.appender.FA=org.apache.log4j.RollingFileAppender
log4j.appender.FA.MaxBackupIndex=0
log4j.appender.FA.MaxFileSize=100Mb
log4j.appender.FA.File=log/my.log
log4j.appender.FA.layout=org.apache.log4j.PatternLayout
log4j.appender.FA.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss} %-5p %c %x - %m%n

and I'd like to know if and how I could define the last two rows of each appender's config only once.

2

2 Answers

21
votes

Not sure if it is exactly what you want, but using the following you can define the layout in 1 place:

PATTERN = %-4r [%t] %-5p %c %x - %m%n

log4j.rootLogger=info, A
log4j.logger.FILE = info, FILE

log4j.appender.A=org.apache.log4j.ConsoleAppender
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=${PATTERN}

log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=log.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=${PATTERN}

Regards,

3
votes

Checking deeper the implementation of config parsing in log4j i've realised that it is just simple parsing and no reference like features are implemented. So, just duplicate :)