Spring Tip: Log4j configuration

Place log4j.properties in WEB-INF directory of the web application,

log4j.rootLogger=INFO, stdout, fileAppender

# Used only for development.
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} [%t] %-5p (%F:%L) – %m%n
log4j.logger.net.sf.ehcache=TRACE

# File appender used in production.
log4j.appender.fileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.fileAppender.File=${app.logdir}/app.log

log4j.appender.fileAppender.MaxFileSize=1MB
log4j.appender.fileAppender.MaxBackupIndex=5

log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.fileAppender.layout.ConversionPattern=%d{ISO8601} %t %p %c – %m%n

In web.xml add

   	
<context-param>
	<param-name>log4jConfigLocation</param-name>
	<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>

<context-param>
	<param-name>log4jRefreshInterval</param-name>
	<param-value>1000</param-value>
</context-param>
	
<listener>
	<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
		

3 Comments »

  1. KarlP said

    And how do you make sure that only the production appender is used when you’re running in production, and the stdout appender is used only on development?

  2. unnisworld said

    One way is to keep two separate environment specific log4j properties files and pick the right one during build process. Check out this blog post – http://sujitpal.blogspot.com/2006/10/maven2-multi-environment-filter-setup.html

  3. daringtakers said

    I dont know why, when log4j used in stand alone application, it don’t find log4j.properties

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.