kmccoy
kmccoy

Reputation: 2861

How do I get log4cplus to log different levels to a file and stdout?

I want to log TRACE messages to a LogFile and INFO messages to stdout. I also want to limit a particular logger "SomeLogger" to DEBUG.

My log4cplus.properties file is as follows but I am not getting SomeLogger messages to stdout. Any advise on how to fix this?

### log4cplus.properties
# root logger
log4cplus.rootLogger=TRACE, LogFile, stdout

# specific logger
log4cplus.logger.SomeLogger=DEBUG, LogFile
log4cplus.additivity.SomeLogger=false

# appender that automatically rolls files
log4cplus.appender.LogFile=log4cplus::RollingFileAppender
log4cplus.appender.LogFile.File=/home/ken/logs/log4cplus.log
log4cplus.appender.LogFile.Append=true
log4cplus.appender.LogFile.MaxBackupIndex=9
log4cplus.appender.LogFile.MaxFileSize=50MB
log4cplus.appender.LogFile.layout=log4cplus::PatternLayout
log4cplus.appender.LogFile.layout.ConversionPattern=%D{%H:%M:%S %Q} %-4r %-5p T%T |%c{2}| %M.%L %x - %m %n

# Direct log messages to stdout
log4cplus.appender.stdout=log4cplus::ConsoleAppender
log4cplus.appender.stdout.Target=System.out
log4cplus.appender.stdout.Threshold=INFO
log4cplus.appender.stdout.layout=log4cplus::PatternLayout
log4cplus.appender.stdout.layout.ConversionPattern=%D{%H:%M:%S.%q} %-5p %m%n

Upvotes: 2

Views: 6553

Answers (2)

kmccoy
kmccoy

Reputation: 2861

I have now had a chance to revisit this problem and see the solution is to replace

log4cplus.logger.SomeLogger=DEBUG, LogFile

with

log4cplus.logger.SomeLogger=DEBUG, LogFile, stdout

This line along with

log4cplus.rootLogger=TRACE, LogFile, stdout
log4cplus.appender.stdout.Threshold=INFO

will now give me:

(1.1) trace messages in the LogFile...

(1.2) ...while restricting SomeLogger to debug messages in the LogFile, and

(2) info message to the console.

Upvotes: 2

wilx
wilx

Reputation: 18248

The line log4cplus.additivity.SomeLogger=false in your configuration means that whatever is logged into SomeLogger is not propagated further up the hierarchy, i.e., the rootLogger.

Upvotes: 2

Related Questions