Freaky Thommi
Freaky Thommi

Reputation: 746

Log messages except from a particular package to a file

I am using log4j for logging , my requirement is to log all the logs from package

pkg1 (except pkg1.pkg2) to pkg1.log

and pkg1.pkg2 (a sub package of the above package) to pkg2.log

is this possible ? I know i can achieve it through custom appenders but is there a way I can do it through configurations.

I am using the below properties file. Here the problem is PKG1.log contains logs from PKG2 also.

    # Root logger option
log4j.rootLogger=WARN, CONSOLE
# Direct log messages to a log file

log4j.logger.com.pkg1=DEBUG, PKG1 log4j.appender.PKG1=org.apache.log4j.RollingFileAppender
log4j.appender.PKG1.File=logs/PKG1.log
log4j.appender.PKG1.MaxFileSize=1MB
log4j.appender.PKG1.MaxBackupIndex=1
log4j.appender.PKG1.layout=org.apache.log4j.PatternLayout
log4j.appender.PKG1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


log4j.logger.com.pkg1.pkg2=DEBUG, PKG2
log4j.appender.PKG2=org.apache.log4j.RollingFileAppender
log4j.appender.PKG2.File=logs/PKG2.log
log4j.appender.PKG2.MaxFileSize=1MB
log4j.appender.PKG2.MaxBackupIndex=1
log4j.appender.PKG2.layout=org.apache.log4j.PatternLayout
log4j.appender.PKG2.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


# Direct log messages to CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Upvotes: 2

Views: 438

Answers (1)

Mureinik
Mureinik

Reputation: 311163

You can stop PKG2's logs from reaching it's ancestor's appenders by disabling the additivity flag in the configuration:

 log4j.appender.PKG2.additivity=false

Upvotes: 1

Related Questions