It is really easy task:
I assumed that you already has spring 3 stuff in place.
- You need add log4j to your libraries in your project
- Add information about log4j to web.xml file (It should be /MyCozyProject/src/main/webapp/WEB-INF/web.xml
- Add information about location for log4j.xml in context.xml
- Configure your log4j
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
In 3. in context.xml (usually should be /src/main/webapp/META-INF/context.xml ) add information about where your log4j.xml file is
<Context displayName=" MyCozyProject ">
<Parameter name="log4jConfigLocation" value="file:C:\myCozyProject\log4j.xml"/>
</Context>
In 4 You just configure your log4j to your needs (below is example of saving log to file)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="tofile" class="org.apache.log4j.FileAppender">
<param name="file" value="C:\\myCozyProject.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/>
</layout>
</appender>
<root>
<priority value ="info" />
<appender-ref ref="tofile" />
</root>
</log4j:configuration>
btw. If you want log4j.xml inside project, then you can use my example or check this blog :
http://th1rty7.blogspot.com/2009/03/configure-logging-in-spring-with-log4j.html
No comments:
Post a Comment