Logback的XML配置方式

Logback的XML配置方式

先在项目的resources目录下创建logback-spring.xml文件:

基本的配置

只定制输出到控制台的格式


<configuration>

    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            
            <pattern>
                %d - %msg%n
            pattern>
        layout>
    appender>

    <root level="info">
        <appender-ref ref="consoleLog"/>
    root>

configuration>

在基础配置之上实现每天产生一个日志文件

要注意的是,这次layout变成了encoder


<configuration>

    
    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            
            <pattern>
                %d - %msg%n
            pattern>
        layout>
    appender>

    
    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>
                %msg%n
            pattern>
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>E:\jack\code\sell\info.%d.logfileNamePattern>
        rollingPolicy>
    appender>

    <root level="info">
        <appender-ref ref="consoleLog"/>
        <appender-ref ref="fileInfoLog"/>
    root>

configuration>

区分Info和Error的日志,输出到不同文件

使用filter标签


<configuration>

    
    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            
            <pattern>
                %d - %msg%n
            pattern>
        layout>
    appender>

    
    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERRORlevel>
            
            <onMatch>DENYonMatch>
            
            <onMismatch>ACCEPTonMismatch>
        filter>
        <encoder>
            <pattern>
                %msg%n
            pattern>
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>E:\jack\code\sell\info.%d.logfileNamePattern>
        rollingPolicy>
    appender>

    
    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERRORlevel>
        filter>
        <encoder>
            <pattern>
                %msg%n
            pattern>
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>E:\jack\code\sell\error.%d.logfileNamePattern>
        rollingPolicy>
    appender>

    <root level="info">
        <appender-ref ref="consoleLog"/>
        <appender-ref ref="fileInfoLog"/>
        <appender-ref ref="fileErrorLog"/>
    root>

configuration>

你可能感兴趣的:(log)