Spring+Log4J.xml在web中的使用方法

1.通过Maven导入Jar包依赖

  • Spring依赖的包:spring-core,spring-beans, spring-context, spring-web

    org.springframework
    spring-context
    4.2.3.RELEASE


    org.springframework
    spring-core
    4.2.3.RELEASE


    org.springframework
    spring-web
    4.2.3.RELEASE


    org.springframework
    spring-beans
    4.2.3.RELEASE
  • Log4J依赖包:

    log4j
    log4j
    1.2.17

2. web.xml配置

"1.0" encoding="UTF-8"?>
"http://www.w3.org/2001/XMLSchema-instance" version="2.5"
        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_2_5.xsd">

  Archetype Created Web Application

  
    contextConfigLocation
    classpath*:config/spring/core/appcontext-*.xml
  

  
    log4jConfigLocation
    
    classpath:log/log4j.xml
  

  
    log4jRefreshInterval
    60000
  

  
  
  
    class>org.springframework.web.util.Log4jConfigListenerlistener-class>
  listener>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
web-app>

3. Log4J.xml 配置

log4j.xml 的详细配置可参考文章




<log4j:configuration>

    
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%p][%d{yyyy-MM-dd HH:mm:ss SSS}][%c]-[%m]%n" />
        layout>

        
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            
            <param name="levelMin" value="INFO" />
            
            <param name="levelMax" value="ERROR" />
        filter>
    appender>


    
    <appender name="rootAppender" class="org.apache.log4j.DailyRollingFileAppender">
        
        <param name="File" value="/data/applogs/own/root.log" />
        
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%p][%d{HH:mm:ss SSS}][%c]-[%m]%n" />
        layout>
    appender>


    
    <root>
        <priority value ="debug"/>
        <appender-ref ref="console"/>
        <appender-ref ref="rootAppender"/>
    root>

log4j:configuration>

4. 代码测试

@Component
public class LogTest {
    Logger logger = Logger.getLogger(LogTest.class);

    @PostConstruct
    public void test(){
        logger.info("----Log.Info----");
        logger.info("----Log.Info----");
        logger.info("----Log.Info----");
    }
}

5. 结果展示:

  • 控制台输出:
    Spring+Log4J.xml在web中的使用方法_第1张图片

  • 文件输出
    Spring+Log4J.xml在web中的使用方法_第2张图片

6. Spring + Log4j + Slf4j

Slf4j为所有日志组件提供统一的门面,在使用时,如果需要换日志组件(如从logback换成log4j),只需要替换相应的jar即可, 理论上是不需要修改代码。
此处以Log4j + Slf4j 为例:

需要jar包:




<dependency>
    <groupId>org.slf4jgroupId>
    <artifactId>slf4j-apiartifactId>
    <version>1.7.9version>
dependency>


<dependency>
    <groupId>org.slf4jgroupId>
    <artifactId>slf4j-log4j12artifactId>
    <version>1.7.9version>
dependency>


<dependency>
    <groupId>log4jgroupId>
    <artifactId>log4jartifactId>
    <version>1.2.17version>
dependency>

测试代码

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;

@Component
public class LogTest {
    // 通过slf4j提供的工厂方法,构建logger
    private static final Logger logger = LoggerFactory.getLogger(LogTest.class);

    @PostConstruct
    public void test(){
        logger.info("----Log.Info----");
        logger.info("----Log.Info----");
        logger.info("----Log.Info----");
    }
}

你可能感兴趣的:(Spring+Log4J.xml在web中的使用方法)