zoukankan      html  css  js  c++  java
  • springMVC集成logback日志系统

    一、项目结构

    项目介绍:maven搭建的web项目,实现Java日志记录功能。其中logback.xml为日志配置文件,spring-mvc-servlet.xml为spring controller级别的上下文配置,appServlet-servlet.xml为系统级别的配置文件

    二、代码清单

    logback.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration scan="true" scanPeriod="10000" debug="false">
        <!-- 日志文件目录 -->
        <property name="LOG_HOME"  value="/applog"/>  
        <!--滚动记录文件 -->
        <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
             <!-- 文件输出的日志 的格式 -->
            <encoder>
                <pattern>
                    [ %-5level] [%date{yyyy-MM-dd HH:mm:ss}] %logger{96} [%line] - %msg%n
                </pattern>
                <charset>UTF-8</charset> <!-- 此处设置字符集,防止中文乱码 -->
            </encoder>
             
            <!-- 配置日志所生成的目录以及生成文件名的规则 在logs/mylog-2016-10-31.0.log -->
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>/applog/bcyb-app-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
                    <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                        <!-- 最大64MB 超过最大值,会重新建一个文件-->
                    <maxFileSize>64MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
            </rollingPolicy>
              
            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                <level>DEBUG</level>
            </filter>
              
              <prudent>true</prudent>
        </appender>
        <!-- 控制台输出 -->   
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> 
                 <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符--> 
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>   
            </encoder> 
        </appender>
        
        
        <appender name="STDOUT.access" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <!-- <file>${LOG_HOME}/mylog.txt</file> -->
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                  <!-- /app/jboss7/jboss-as-7.1.1.Final/standalone/deployments/ -->
                  <fileNamePattern>/applog/access/%d{yyyy-MM-dd}.access.%i.log.zip</fileNamePattern>
                   <maxHistory>7</maxHistory> 
                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <!-- or whenever the file size reaches 100MB -->
                    <maxFileSize>100MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
            </rollingPolicy>
            <encoder>
                   <pattern>操作ID|%date{yyyy-MM-dd}| %date{HH:mm:ss}|1|%X{logbackFilterIp}|操作来源MAC地址|%msg%n</pattern>
            </encoder>
          </appender>
        
        
        <appender name="STDOUT.authorityChange" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <!-- <file>${LOG_HOME}/mylog.txt</file> -->
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                  <!-- rollover daily -->
                  <fileNamePattern>/applog/authority_change/%d{yyyy-MM-dd}.authority_change.%i.log.zip</fileNamePattern>
               
                   <maxHistory>7</maxHistory> 
                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <!-- or whenever the file size reaches 100MB -->
                    <maxFileSize>100MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
            </rollingPolicy>
            <encoder>
               <pattern>操作ID|%date{yyyy-MM-dd}|%date{HH:mm:ss}|2|%X{logbackFilterIp}|操作来源MAC地址|%msg%n</pattern>
            </encoder>
          </appender>
        
        <!-- 
            为com.cn.project.controller.UserRoleManageController 类提供单独的日志打印文件
         -->
        <logger name="com.cn.project.controller.UserRoleManageController"  level="DEBUG" additivity="false">
            <appender-ref ref="STDOUT.access"/>
        </logger>
        <logger name="com.cn.project.controller.UserRoleManageController"  level="DEBUG" additivity="false">
            <appender-ref ref="STDOUT.authorityChange"/>
        </logger>
        
        <logger name="test"  level="INFO" additivity="false">
            <appender-ref ref="STDOUT"/>
        </logger>
    
        <!-- 日志输出级别 -->
        <root level="INFO">
            <appender-ref ref="FILE" />
            <appender-ref ref="FILE.access" />
            <appender-ref ref="FILE.authorityChange" />
            <appender-ref ref="STDOUT" />
        </root> 
    </configuration>

    appServlet-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd ">
            
    </beans>

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID">
    
        <display-name>HelloSpringMVC</display-name>
    
        <!-- 系统上下文配置application.xml -->
        <servlet>
            <servlet-name>appServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>appServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
        <!-- controller级别配置,作用于controller上下文 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc-servlet.xml</param-value>
        </context-param>
    
        <!-- 加载其他的xml配置 -->
        <context-param>
            <param-name>logbackConfigLocation</param-name>
            <param-value>classpath:/log/logback.xml</param-value>
        </context-param>
    
    
        <!-- logback监听器(注意顺序,在spring监听器上面) -->
        <listener>
             <listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class>
        </listener>
    
        <!-- Spring 监听器 -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <!-- 页面编码设置 -->
        <filter>
            <filter-name>encodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>encodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
            <welcome-file>default.html</welcome-file>
            <welcome-file>default.htm</welcome-file>
            <welcome-file>default.jsp</welcome-file>
        </welcome-file-list>
    
    </web-app>

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.cn</groupId>
        <artifactId>HelloSpringMVC</artifactId>
        <packaging>war</packaging>
        <version>0.0.1-SNAPSHOT</version>
        <name>HelloSpringMVC Maven Webapp</name>
        <url>http://maven.apache.org</url>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
    
            <!-- Servlet Library -->
            <!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </dependency>
    
            <!-- Spring dependencies -->
            <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>4.1.4.RELEASE</version>
            </dependency>
    
            <!-- http://mvnrepository.com/artifact/org.springframework/spring-web -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>4.1.4.RELEASE</version>
            </dependency>
    
            <!-- http://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.1.4.RELEASE</version>
            </dependency>
    
            <!-- 返回json数据 -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.8.1</version>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.8.1</version>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>2.8.1</version>
            </dependency>
    
            <!-- logback日志 -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.6.0</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>1.0.13</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.0.13</version>
            </dependency>
            <dependency>
                <groupId>org.logback-extensions</groupId>
                <artifactId>logback-ext-spring</artifactId>
                <version>0.1.2</version>
            </dependency>
    
        </dependencies>
        <build>
            <finalName>HelloSpringMVC</finalName>
        </build>
    </project>

    三、日志实现

    private static Logger log = LoggerFactory.getLogger(HelloWorldController.class);
    @RequestMapping("/insert")
        @ResponseBody
        public void insert(@RequestBody TProjectInfo projectInfo){
            
            log.info("11111");  //logback打印日志 2018-09-19 15:06:15.161 [http-nio-8080-exec-5] INFO  com.cn.project.controller.HelloWorldController - 11111
        }
  • 相关阅读:
    mock static方法
    Spring Boot 例一 实现jsonp接口
    安装Linux虚拟机到执行Java程序
    Mock单元测试
    通用分页请求返回类
    Collectors.groupingBy分组后的排序问题
    按权重分配任务
    Java正则表达式-捕获组
    远程抓取图片
    正则
  • 原文地址:https://www.cnblogs.com/x-jingxin/p/9674970.html
Copyright © 2011-2022 走看看