Sample log4j.properties file
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
Tags: log4j
JA said,
November 9, 2009 at 5:45 am
Merci
Abhishek said,
December 9, 2009 at 11:56 pm
Thanks alot. It’s helpful.
Perkins said,
December 18, 2009 at 1:51 am
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.
Ben said,
December 19, 2009 at 11:11 am
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.
gagan said,
February 24, 2010 at 10:12 pm
Thanks a lot for the stuff! Keep it up!
Likide said,
April 23, 2010 at 2:53 pm
it’s was really useful!!
Jeff Oakes said,
July 14, 2010 at 11:01 am
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?
Mandy said,
September 27, 2010 at 1:37 am
If possible, please give some SMTPAppender example
Thanks,
Mandy
mon said,
October 18, 2010 at 12:52 pm
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
Amit Kondewar said,
December 1, 2010 at 11:35 pm
hi Thanks Benjamin thanks for this valuable guidence.
Miral said,
March 7, 2011 at 6:33 pm
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…?
Ankur said,
March 16, 2011 at 2:54 am
Thanks , it is really help ful.
Ankur said,
March 16, 2011 at 4:35 am
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.
Wasi said,
May 25, 2011 at 11:24 pm
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?
lwpro2 said,
June 20, 2011 at 3:40 am
thanks. it always helps.
MAK said,
October 13, 2011 at 4:06 am
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