Showing posts with label log4j. Show all posts
Showing posts with label log4j. Show all posts

Tuesday, 23 November 2010

Log4j to different files

How to log to different files based on packages.

Sample log4j.properties file:


log4j.rootLogger=WARN,

#Overriding log levels in certain packages:
log4j.logger = INFO, all
log4j.logger.com.drkw.swaptradesubscriber=INFO, tradeReadingFile
log4j.logger.com.drkw.tibco=INFO, tibcoMessages

# For stdout use this one:
#log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.tibcoMessages=org.apache.log4j.FileAppender
log4j.appender.tibcoMessages.File=C:/temp/tradeSubscriber-tibco.log
log4j.appender.tibcoMessages.layout=org.apache.log4j.PatternLayout
log4j.appender.tibcoMessages.layout.ConversionPattern=%d{ISO8601} %p %t %c - %m%n
log4j.appender.tibcoMessages.Append=false
log4j.appender.tibcoMessages.Threshold=DEBUG

log4j.appender.tradeReadingFile=org.apache.log4j.FileAppender
log4j.appender.tradeReadingFile.File=C:/temp/tradeSubscriber-load.log
log4j.appender.tradeReadingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.tradeReadingFile.layout.ConversionPattern=%d{ISO8601} %p %t %c - %m%n
log4j.appender.tradeReadingFile.Append=false

log4j.appender.all=org.apache.log4j.FileAppender
log4j.appender.all.File=C:/temp/tradeSubscriber-all.log
log4j.appender.all.layout=org.apache.log4j.PatternLayout
log4j.appender.all.layout.ConversionPattern=%d{ISO8601} %p %t %c - %m%n
log4j.appender.all.Append=false

Friday, 20 August 2010

Log4j reloading a properties file

Heads up for a funky log4j story:

PropertyConfigurator has a lovely method ' configureAndWatch()' which will reload the properties file if it changes.

However it doesn't completely reload it as the existing configuration is not cleared nor reset. Hence you can only change the level of existing logs and not comment out or add new package level logs.

To fix the above and completely reload the log4j file you must add log4j.reset=true to your properties file. This will cause the old properties data to be wiped before the file is loaded.

Sourced from the log4j mailing list

Tuesday, 23 June 2009

log4j problems

If you are having trouble working out which log4j.properties file is being used then use this:

-Dlog4j.debug

for more info click here

Please remember that log4j will load in the properties files in CLASSPATH order.

To force your code to use a specific log file use this:

-Dlog4j.configuration=file:C:\temp\log4j.xml
or
-Dlog4j.properties=file:C:\temp\log4j.properties

Sample log4j.properties files:
click here

If you want to change what is written to the logs [dates / classname etc]:
http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html

sample log4j.xml file:
http://www.google.com/codesearch/p?hl=en#3hqpXVqVs-0/trunk/restlet/log4j.xml&q=log4j%20lang:xml&sa=N&cd=6&ct=rc