<dependency>
<groupId>log4j <groupId>
<artifactId>log4j<artifactId>
<version>1.2.9 <version>
<dependency>
If you are using common logging, add following dependency
<dependency>
<groupId>commons-logging <groupId>
<artifactId>commons-logging<artifactId>
<version>1.1.1<version>
<dependency>
Step 2 :
Create a log4j.properties or xml as per your requirement.
Here is sample log4j.properties
# following line defines the log level and mode of outputs. Console and rolling file
log4j.rootLogger= INFO, CONSOLE, ROLLING
log4j.logger.com= INFO, CONSOLE, ROLLING, DAILY
# Print only messages of level WARN or above in the package org.apache.
log4j.logger.org.apache=WARN
# Print only messages of level WARN or above in the package org.springframework.
log4j.logger.org.springframework=WARN
# CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d %-5p [YOURPROJECTNAME] [%t] %C{2} (%F:%L) - %m%n
# ROLLING
log4j.appender.ROLLING=org.apache.log4j.RollingFileAppender
log4j.appender.ROLLING.File=yourprojectname.log
log4j.appender.ROLLING.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLING.layout.ConversionPattern=%d %-5p [YOURPROJECTNAME] [%t] %C{2} (%F:%L) - %m%n
log4j.appender.ROLLING.MaxFileSize=10MB
log4j.appender.ROLLING.MaxBackupIndex=5
# DAILY
log4j.appender.DAILY=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DAILY.layout=org.apache.log4j.PatternLayout
log4j.appender.DAILY.layout.ConversionPattern=%d %-5p [YOURPROJECTNAME] [%t] %C{2} (%F:%L) - %m%n
log4j.appender.DAILY.File=yourprojectnamedaily.log
log4j.appender.DAILY.DatePattern=yyyy-MM-dd
Step 3.
Add following line in application-context.xml
<bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
<property name="targetMethod" value="initLogging" />
<property name="arguments">
<list>
<value>classpath:log4j.properties<value>
<list>
<property>
<bean>
That’s it. You are done!!!
******************************************************************************
Change Log level without re-starting server –
If it is required that you have to change log level frequently, it would be
frustrating to restart the server every time. To avoid restarting application
server whenever you modify log4j.properties file.
Simply add one line of configuration in application-context.xml that is –
<value type="long">enter time here<value>
*Time is in millisecond.
So the configuration will look like as follows.
<bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="org.springframework.util.Log4jConfigurer"/>
<property name="targetMethod" value="initLogging" />
<property name="arguments">
<list>
<value>classpath:log4j.properties<value>
<value type="long">10000000<value>
<list>
<property>
<bean>
But you should not use above configuration in production server. Please visit the
following link for more information
Code Assistance Tip:
Here are one useful tip if you are using Eclipse
1.1. Go to window->Perspective->Java->Editor->Templates
1.2. Create new.
1.3. Give following parameters.
Name – yourprojectnameLog
Description - it will create a logger instance for enclosing class of the project.
Pattern :
If you are using common logging then use
private static final Log logger=LogFactory.getLog(${primary_type_name}.class);
If you are using log4j then use
private static final Logger logger=Logger.getLogger(${enclosing_type}.class.getName());
To use it in java classes, simply press ‘yourproje’ and click ctrl+space and select
yourprojectnameLog.