1

I am using log4j in springframework based application. I need to direct log messages generated by spring framework to app.log file and business logic related logs for business.log file. My log4j.property file is like following.

log4j.rootLogger=DEBUG, appLogger, bizLogger

log4j.appender.appLogger=org.apache.log4j.RollingFileAppender
log4j.appender.appLogger.File=./logs/app.log
log4j.appender.appLogger.MaxFileSize=200KB
log4j.appender.appLogger.MaxBackupIndex=2
log4j.appender.appLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.appLogger.layout.ConversionPattern=%d [%c] %p - %m%n

log4j.appender.bizLogger=org.apache.log4j.RollingFileAppender
log4j.appender.bizLogger.File=./logs/biz.log
log4j.appender.bizLogger.MaxFileSize=200KB
log4j.appender.bizLogger.MaxBackupIndex=2
log4j.appender.bizLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.bizLogger.layout.ConversionPattern=%d [%c] %p - %m%n

I am getting the bizLogger in code and use it for logging. But both files contain both log statements. Please help me to resolve this issue.

Mr. 14
  • 9,228
  • 6
  • 37
  • 54
user2307172
  • 141
  • 1
  • 9
  • You need to define logs per package. Check out this link http://stackoverflow.com/questions/5297686/log4j-package-specific-logging – Mani Dec 27 '13 at 17:26

1 Answers1

2

As Mani's comment suggests, you need to define logs per package.

So, if your spring framework packages begin with org.springframework and your business logic packages begin with com.myapp, then you can add the following lines to your log4j.properties:

log4j.logger.org.springframework=DEBUG,appLogger
log4j.logger.com.myapp=DEBUG,bizLogger

Of course, set the threshold (DEBUG, INFO, etc.) to your preference. You may also wish to set additivity to false:

log4j.additivity.org.springframework=false
log4j.additivity.com.myapp=false
armstrhb
  • 4,054
  • 3
  • 20
  • 28