Reputation: 9457
I know it's been asked many times before and I thought I knew the answer but I still don't seem to have the perfect solution.
I'm trying have different programs log to different files, using log4j. The programs are not neccesariy in different packages, but some are. I have the following setup:
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=ERROR, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n
# Log to a file for the ProgA
log4j.logger.com.foo.apps.research.ProgA=INFO, ProgALog
log4j.additivity.com.foo.apps.research.ProgA=false
log4j.appender.ProgALog=org.apache.log4j.FileAppender
log4j.appender.ProgALog.File=/tmp/ProgA.log
log4j.appender.ProgALog.ImmediateFlush=true
log4j.appender.ProgALog.Threshold=info
log4j.appender.ProgALog.Append=false
log4j.appender.ProgALog.layout=org.apache.log4j.PatternLayout
log4j.appender.ProgALog.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n
# Log to a file for the ProgB program
log4j.logger.com.foo.apps.research.ProgB=INFO, ProgBLog
log4j.additivity.com.foo.apps.research.ProgB=false
log4j.appender.ProgBLog=org.apache.log4j.FileAppender
log4j.appender.ProgBLog.File=/tmp/ProgB.log
log4j.appender.ProgBLog.ImmediateFlush=true
log4j.appender.ProgBLog.Threshold=info
log4j.appender.ProgBLog.Append=false
log4j.appender.ProgBLog.layout=org.apache.log4j.PatternLayout
log4j.appender.ProgBLog.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n
# Log to a file for the ProgC program
log4j.logger.com.foo.apps.blah.ProgC=INFO, ProgCLog
log4j.additivity.com.foo.apps.blah.ProgC=false
log4j.appender.ProgCLog=org.apache.log4j.FileAppender
log4j.appender.ProgCLog.File=/tmp/ProgC.log
log4j.appender.ProgCLog.ImmediateFlush=true
log4j.appender.ProgCLog.Threshold=info
log4j.appender.ProgCLog.Append=false
log4j.appender.ProgCLog.layout=org.apache.log4j.PatternLayout
log4j.appender.ProgCLog.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n
The problem is that every time I run any of the 3 programs, all 3 log files are created. Only the appropriate one gets populated with the log messages of the program but a 0-length log file still gets created for the other 2. This is obviously not what I want.
Can anyone please enlighten me about what I'm doing wrong?
Upvotes: 2
Views: 1467
Reputation: 3062
It would seem that your problem has more to do with how log4j
instantiates log files than with the actual "multiple application-multiple log file" part. Thus, if I understand you correctly, what you're really trying to achieve is to design a file appender definition that dictates the creation of log file in a more lazy-like fashion (as opposed to the default, i.e. instant formation). As far as I am aware, though, the latter is just how log4j
works and there is no fast and easy way around it.
EDIT: See the replies to this SO question for a more detailed discussion & possible solutions to your problem.
Another quick fix would be to chop your log4j.properties
file up into multiple subprogram-specific pieces, but that might turn out to be suboptimal depending on the constraints & requirements employed upon your application. (Again, see this SO question for more information on that particular solution).
Upvotes: 1