SpringMVC中配置分离,配置logback

时间:2020-01-14
本文章向大家介绍SpringMVC中配置分离,配置logback,主要包括SpringMVC中配置分离,配置logback使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

pom.xml

<!-- LOGGING begin -->
<dependency>
   <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
     <version>${logback.version}</version>
<exclusions>
    <exclusion>
     <artifactId>slf4j-api</artifactId>
    <groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
<!--<scope>test</scope> -->
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>${logback.version}</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-access</artifactId>
    <version>${logback.version}</version>
</dependency>
<dependency>
    <groupId>org.logback-extensions</groupId>
    <artifactId>logback-ext-spring</artifactId>
    <version>0.1.4</version>
</dependency>
<!-- LOGGING end -->

 

 


logback.xml

<configuration scan="true" scanPeriod="60 seconds">      <!-- Auto Scan config file  -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>info</level>        <!-- only show error msg in stdout  -->
        </filter>
        <encoder>
            <pattern>[lf-1][edums][%d{yyyy-MM-dd HH:mm:ss.SSS}][%-5level][%thread][%file:%line] - %msg%n</pattern>
        </encoder>
    </appender>


    <appender name="ERROR_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/home/logs/edums/edums-error.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>/home/logs/edums/edums-error.log.%d{yyyy-MM-dd}</fileNamePattern>
            <maxHistory>10</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>[lf-1][edums][%d{yyyy-MM-dd HH:mm:ss.SSS}][%-5level][%thread][%file:%line] - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>warn</level>        <!-- only log warn/error msg -->
        </filter>
    </appender>


    <appender name="NORMAL_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/home/logs/edums/edums.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>/home/logs/edums/edums.log.%d{yyyyMMdd}</fileNamePattern>
            <maxHistory>10</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>[lf-1][edums][%d{yyyy-MM-dd HH:mm:ss.SSS}][%-5level][%thread][%file:%line] - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>info</level>        <!-- only log warn/error msg -->
        </filter>
    </appender>


    <!-- #################################################################### -->


    <root level="debug">                <!-- level=trace/debug/info/warn/error/all/off -->
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="ERROR_LOG"/>
        <appender-ref ref="NORMAL_LOG"/>
    </root

</configuration>

 

web.xml

<context-param>
<param-name>logbackConfigLocation</param-name>
<param-value>file:D:\home\conf\edums\logback.xml</param-value>
</context-param>

      <!-- 顺序1 -->

<listener>
<listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class>

</listener>

  <!-- 顺序2-->

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

 

 //顺序颠倒可能导致异常的log打印

  

                                                                               微信公众号: 

                                               

                                                                             JAVA程序猿成长之路

                              分享资源,记录程序猿成长点滴。专注于Java,Spring,SpringBoot,SpringCloud,分布式,微服务。

$flag 上一页 下一页