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?