Sample log4j.properties file
07/18/2009
I always find the hardest part of getting started with log4j is creating a log4j.properties file. For that reason, I’ve posted an example below. This file configures log4j to log any messages of level info or higher to the console except for classes under the com.dappit.Dapper.parser or org.w3c.tidy packages.
#------------------------------------------------------------------------------
#
# The following properties set the logging levels and log appender. The
# log4j.rootCategory variable defines the default log level and one or more
# appenders. For the console, use 'S'. For the daily rolling file, use 'R'.
# For an HTML formatted log, use 'H'.
#
# To override the default (rootCategory) log level, define a property of the
# form (see below for available values):
#
# log4j.logger. =
#
# Available logger names:
# TODO
#
# Possible Log Levels:
# FATAL, ERROR, WARN, INFO, DEBUG
#
#------------------------------------------------------------------------------
log4j.rootCategory=INFO, S
log4j.logger.com.dappit.Dapper.parser=ERROR
log4j.logger.org.w3c.tidy=FATAL
#------------------------------------------------------------------------------
#
# The following properties configure the console (stdout) appender.
# See http://logging.apache.org/log4j/docs/api/index.html for details.
#
#------------------------------------------------------------------------------
log4j.appender.S = org.apache.log4j.ConsoleAppender
log4j.appender.S.layout = org.apache.log4j.PatternLayout
log4j.appender.S.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n
#------------------------------------------------------------------------------
#
# The following properties configure the Daily Rolling File appender.
# See http://logging.apache.org/log4j/docs/api/index.html for details.
#
#------------------------------------------------------------------------------
log4j.appender.R = org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File = logs/bensApps.log
log4j.appender.R.Append = true
log4j.appender.R.DatePattern = '.'yyy-MM-dd
log4j.appender.R.layout = org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n
#------------------------------------------------------------------------------
#
# The following properties configure the Rolling File appender in HTML.
# See http://logging.apache.org/log4j/docs/api/index.html for details.
#
#------------------------------------------------------------------------------
log4j.appender.H = org.apache.log4j.RollingFileAppender
log4j.appender.H.File = logs/bensApps.html
log4j.appender.H.MaxFileSize = 100KB
log4j.appender.H.Append = false
log4j.appender.H.layout = org.apache.log4j.HTMLLayout
Merci 🙂
Thanks alot. It’s helpful.
Thanks Benjamin for the post.
I still have some questions about this:
– Which is the location of log4j.properties in the project?
– How log4j knows where to find the properties file?
– It is necessary to add the log4j.properties file to the build path?
Thanks in advance,
Cheers.
Hi Perkins,
The log4j.properties file needs to be on your classpath. Very often this is in a resources directory. For example, if you’re using Maven, then a good place to put it is in the src/main/resources directory.
Thanks a lot for the stuff! Keep it up!
it’s was really useful!!
This is quite clear. However, I am trying to set the log file location based on the value of an environment variable. So my log4j.properties file has an entry like:
log4j.appender.Default.File=${MYAPP_HOME}/log/myapp.log
Even though MYAPP_HOME is set, the result is log4j looks for a log file in /log/myapp.log
It can not find this and so it throws an exception.
Do you have any suggestions?
If possible, please give some SMTPAppender example
Thanks,
Mandy
do you know what to put in the directory path for windows xp. I used to deploy my apps in a linux machine but now i just deploy it in my windows xp. do you know what to put in here?
log4j.appender.R.File=/usr/local/tomcat/logs/mylog.log
hi Thanks Benjamin thanks for this valuable guidence.
its a useful post…
Benjamin , I have a doubt ..
I was using log4j for my application logging.it was working fine.
But recently I checked , it was not creating the index files for the log.
the setting were as max size = 40 mb with 100 index files.
the same file was being overwritten.
but when i changed the file name, it created the index files.
what could be the reason for this strange behaviour…?
Thanks , it is really help ful.
How does the max file size limitation works?means if I want to take a back up of the existing log file and create a new one after the size exceeds say 100 kb.
Please suggest.
i have to catch log details and send it through web service…how can i do please guide me
How to catch the log details before reaching the log files?
thanks. it always helps.
Thanks for the above examples. It helps me a lot.
Can you please tell how to implement log4j for XML file.
Thanks in advance…
MAK