spring整合log4j日志管理配置

时间:2020-12-28 21:49:07

1.引入spring和log4j的相关jar包;

2.log4j配置文件

#---------------------------------------------------------
# Log4J Settings for log4j 1.2.x (via jakarta-commons-logging)
#
# The five logging levels used by Log are (in order):
#
# 1. DEBUG (the least serious)
# 2. INFO
# 3. WARN
# 4. ERROR
# 5. FATAL (the most serious)
#
#---------------------------------------------------------
# 日志输出级别
#---------------------------------------------------------
log4j.rootLogger=DEBUG, stdout, debug_log, info_log, error_log

#---------------------------------------------------------
# 输出到控制台
#---------------------------------------------------------
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout=org.apache.log4j.TTCCLayout

#---------------------------------------------------------
# info_log 文件位置不要写相对路径,无法生成文件
# ${BusinessCollaboration}在web.xml中进行配置,为当前应用的别名
#---------------------------------------------------------
log4j.appender.debug_log=org.apache.log4j.DailyRollingFileAppender
log4j.appender.debug_log.File=${BusinessCollaboration}/usr/logs/SpringCache/debug.log
log4j.appender.debug_log.Append=true
log4j.appender.debug_log.Threshold=DEBUG
log4j.appender.debug_log.layout=org.apache.log4j.PatternLayout
log4j.appender.debug_log.DatePattern='.'yyyy-MM-dd
log4j.appender.debug_log.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss:SSS} %p [%M] %c %L %m%n

#---------------------------------------------------------
# info_log
#---------------------------------------------------------
log4j.appender.info_log=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info_log.File=${BusinessCollaboration}/usr/logs/SpringCache/info.log
log4j.appender.info_log.Append=true
log4j.appender.info_log.Threshold=INFO
log4j.appender.info_log.layout=org.apache.log4j.PatternLayout
log4j.appender.info_log.DatePattern='.'yyyy-MM-dd
log4j.appender.info_log.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss:SSS} %p [%M] %c %L %m%n

#---------------------------------------------------------
# error_log
#---------------------------------------------------------
log4j.appender.error_log=org.apache.log4j.DailyRollingFileAppender
log4j.appender.error_log.File=${BusinessCollaboration}/usr/logs/SpringCache/error.log
log4j.appender.error_log.Append=true
log4j.appender.error_log.Threshold=ERROR
log4j.appender.error_log.layout=org.apache.log4j.PatternLayout
log4j.appender.error_log.DatePattern='.'yyyy-MM-dd
log4j.appender.error_log.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss:SSS} %p [%M] %c %L %m%n

3.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>Archetype Created Web Application</display-name>
<!-- spring配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<!--由Spring载入的Log4j配置文件位置-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<!-- spring监听log4j监听器的参数,定时检查log4j配置文件 -->
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<!-- 为当前应用设置别名(BusinessCollaboration)可随意写,能够在log4j配置文件中通过${BusinessCollaboration},获取当前应用的根目录路径 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>BusinessCollaboration</param-value>
</context-param>

<!--Spring log4j 监听器-->
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--Spring监听器-->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!--Springmvc的前端控制器 -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>/index.html</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>

不使用Log4jConfigListener监听器spring也能够通过commons-logging找到log4j,但是使用spring中的Log4jConfigListener有如如下好处:
1. 动态的改变记录级别和策略,不需要重启Web应用,如《Effective Enterprise Java》所说。
2. 把log文件定在 /WEB-INF/logs/ 而不需要写绝对路径。
因为 系统把web目录的路径压入一个叫webapp.root的系统变量。这样写log文件路径时不用写绝对路径了.
log4j.appender.logfile.File=${webapp.root}/WEB-INF/logs/myfuse.log
3. 可以把log4j.properties和其他properties一起放在/WEB-INF/ ,而不是Class-Path。
4. log4jRefreshInterval为6000表示 开一条watchdog线程每6秒扫描一下配置文件的变化;