Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j:how to define a common layout and ConversionPattern for multiple appenders

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.

like image 702
cdarwin Avatar asked Nov 08 '11 16:11

cdarwin


1 Answers

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,

like image 68
Glever Avatar answered Sep 17 '22 12:09

Glever