Logback配置详解-配置demo-控制台彩色输出-解决中文乱码-日志输出到压缩包和文件

Logback配置详解-配置demo-控制台彩色输出-解决中文乱码-日志输出到压缩包和文件_第1张图片Logback配置详解-配置demo-控制台彩色输出-解决中文乱码-日志输出到压缩包和文件_第2张图片

1 在resources目录下建立logback.xml文件

2 logback.xml


<configuration scan="false" scanPeriod="10 seconds">
    
    <property name="APP_NAME" value="bookstore-maven" />

    
    <property name="LOG_HOME" value="../logs" />
    
    <logger name="org.springframework" level="INFO" />
    <logger name="org.apache.activemq" level="INFO" />
    <logger name="org.apache.zookeeper" level="INFO" />
    
    <logger name="com.alibaba.dubbo" level="WARN" />
   
    <logger name="com.gk.repository" level="DEBUG" />

    
    
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_HOME}/${APP_NAME}.logFile>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <FileNamePattern>${LOG_HOME}/${APP_NAME}/${APP_NAME}-%d{yyyy-MM-dd}-%i.zipFileNamePattern>
            <maxFileSize>50MBmaxFileSize>
            <maxHistory>30maxHistory>
            <totalSizeCap>10GBtotalSizeCap>
        rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>[ %-5level ] [%date{yyyy-MM-dd HH:mm:ss}] [%thread-%logger{96}-%mdc{client}-%line line]  - %msg%npattern>
            <charset>UTF-8charset> 
        encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERRORlevel>
            <onMatch>DENYonMatch>
            <onMismatch>NEUTRALonMismatch>
        filter>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>WARNlevel>
            <onMatch>DENYonMatch>
            <onMismatch>NEUTRALonMismatch>
        filter>

        
        <prudent>falseprudent>
    appender>

    
    <appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_HOME}/${APP_NAME}.error.logFile>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <FileNamePattern>${LOG_HOME}/${APP_NAME}/${APP_NAME}.error-%d{yyyy-MM-dd}-%i.zipFileNamePattern>
            <maxFileSize>50MBmaxFileSize>
            <maxHistory>30maxHistory>
            <totalSizeCap>10GBtotalSizeCap>
            
        rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>[ %-5level ] [%date{yyyy-MM-dd HH:mm:ss}] [%thread-%logger{96}-%mdc{client}-%line line]  - %msg%npattern>
            <charset>UTF-8charset> 
        encoder>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERRORlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>NEUTRALonMismatch>
        filter>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>WARNlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>


    
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        
        <encoder>
            <pattern>[ %highlight(%-5level) ] %green([%date{yyyy-MM-dd HH:mm:ss}]) %boldMagenta([%thread-%logger{96}-%mdc{client}-%line line])  - %cyan(%msg%n)pattern>
            <charset>UTF-8charset> 
        encoder>
        
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>DEBUGlevel>
        filter>
    appender>

    

    <root level="INFO">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
        <appender-ref ref="FILE_ERROR" />
    root>
configuration>

你可能感兴趣的:(Java)