logback(四): 输出日志到不同文件

为了方便查看和归档:
不同包的日志可能要放到不同的文件中,如service层和dao层的日志;
不同日志级别:调试、信息、警告和错误等也要分文件输出。

一、根据包输出,通过logger的name属性指定不同的包

1. 创建不同包下的两个类:

1)ObligationService.java

    package com.john.service;  
      
    @Service  
    public class ObligationService {  
        static final Logger log = LoggerFactory.getLogger(ObligationService.class);  
      
        @Resource  
        ObligationDao dao;  
          
        public void obligate() {  
            log.debug("obligate start...");  
            dao.queryObligations();  
            log.error("An error occurred during the progress");  
            log.debug("obligate end...");  
        }  
    }  

2) ObligationDao.java 

    package com.john.dao;  
      
    @Repository  
    public class ObligationDao {  
        static final Logger log = LoggerFactory.getLogger(ObligationDao.class);  
          
        public void queryObligations() {  
            log.debug("query obligations start...");  
            log.error("An error occurred during the query");  
            log.debug("query obligations end...");  
        }  
    }  

2,在logback(-test).xml中添加:

    <appender name="serviceAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">  
        <file>/logs/bribery.log</file>  
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
            <fileNamePattern>/logs/bribery-%d{yyyy-MM-dd}.log</fileNamePattern>  
        </rollingPolicy>  
        <encoder>  
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>  
        </encoder>  
    </appender>  
      
    <appender name="daoAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">  
        <file>/logs/venality.log</file>  
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
            <fileNamePattern>/logs/venality-%d{yyyy-MM-dd}.log</fileNamePattern>  
        </rollingPolicy>  
        <encoder>  
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>  
        </encoder>  
    </appender>  
      
    <logger name="com.john.service" level="DEBUG" additivity="false">  
        <appender-ref ref="serviceAppender" />  
    </logger>  
      
    <logger name="com.john.dao" level="DEBUG" additivity="false">  
        <appender-ref ref="daoAppender" />  
    </logger>  

在类路径下创建Spring的beans.xml文件:(beans节点省略)

<context:component-scan base-package="com.john" /> 

3,测试方法:

    @Test  
    public void dispatchByPackageTest() {  
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");  
        ObligationService service = ctx.getBean(ObligationService.class);  
        service.obligate();  
    }  

执行测试后,
bribery.log:
15:59:59.839 [main] DEBUG c.j.s.ObligationService - obligate start...
15:59:59.844 [main] ERROR c.j.s.ObligationService - An error occurred during the progress
15:59:59.844 [main] DEBUG c.j.s.ObligationService - obligate end...

venality.log:
15:59:59.844 [main] DEBUG c.j.d.ObligationDao - query obligations start...
15:59:59.844 [main] ERROR c.j.d.ObligationDao - An error occurred during the query
15:59:59.844 [main] DEBUG c.j.d.ObligationDao - query obligations end...

二、根据日志级别输出

这里把所有级别为ERROR的日志输出到一个文件中

1. 修改logback(-test).xml文件

serviceAppender和daoAppender都加入以下级别过滤器,这样错误日志就不会在bribery.log和venality.log中打印了:

    <filter class="ch.qos.logback.classic.filter.LevelFilter">  
        <level>ERROR</level>  
        <onMatch>DENY</onMatch>  
        <onMismatch>ACCEPT</onMismatch>  
    </filter>  

新建一个错误日志的appender:

    <appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">  
        <file>/logs/error.log</file>  
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
            <fileNamePattern>/logs/venality-%d{yyyy-MM-dd}.log</fileNamePattern>  
        </rollingPolicy>  
        <encoder>  
            <pattern>%d{HH:mm:ss.SSS} %thread %X{invokeNo} %logger{40} %msg%n</pattern>  
        </encoder>  
        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 -->  
            <level>ERROR</level>  
            <onMatch>ACCEPT</onMatch>  
            <onMismatch>DENY</onMismatch>  
        </filter>  
    </appender>  

name为com.john.dao和com.john.service的logger都加上errorAppender:

    <appender-ref ref="errorAppender" />  

2. 执行上面的测试方法,service和dao的错误日志都打印到error.log文件中了:
16:12:51,433 main com.john.dao.ObligationDao An error occurred during the query
16:12:51,433 main com.john.service.ObligationService An error occurred during the progress

 

展开阅读全文

没有更多推荐了,返回首页