logback配置和使用

一、依赖包

<properties>
  <logback.version>1.2.3logback.version>
properties>

<dependency>
    <groupId>ch.qos.logbackgroupId>
    <artifactId>logback-coreartifactId>
    <version>${logback.version}version>
dependency>
<dependency>
    <groupId>ch.qos.logbackgroupId>
    <artifactId>logback-classicartifactId>
    <version>${logback.version}version>
dependency>
<dependency>
    <groupId>ch.qos.logbackgroupId>
    <artifactId>logback-accessartifactId>
    <version>${logback.version}version>
dependency>

 

二、logback.xml

xml version="1.0" encoding="UTF-8"?>



<configuration  scan="true" scanPeriod="30 seconds">
    
    <property resource="logback.properties" />
    
    
    <property name="CONSOLE_LOG_PATTERN"
             value="%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %c : %m%n"/>
    <property name="FILE_LOG_PATTERN"
             value="%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %c : %m%n"/>
     
    
    <contextName>${APP_NAME}contextName>
     
    
    <property name="DEBUG_LOG_FILE_NAME_PATTERN" value="${log.dir}/${app.name}-debug.%d{yyyy-MM-dd}.%i.log"/>
    <property name="INFO_LOG_FILE_NAME_PATTERN" value="${log.dir}/${app.name}-info.%d{yyyy-MM-dd}.%i.log"/>
    <property name="WARN_LOG_FILE_NAME_PATTERN" value="${log.dir}/${app.name}-warn.%d{yyyy-MM-dd}.%i.log"/>
    <property name="ERROR_LOG_FILE_NAME_PATTERN" value="${log.dir}/${app.name}-error.%d{yyyy-MM-dd}.%i.log"/>
        
        
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}pattern>
        encoder>
    appender>
        
        
    <appender name="debug_file" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${DEBUG_LOG_FILE_NAME_PATTERN}fileNamePattern>
            
            <maxHistory>${debug.file.max.history}maxHistory>
            
            <totalSizeCap>${debug.file.total.size.cap}totalSizeCap>
            
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>${debug.file.max.file.size}maxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>
        <encoder>
            <pattern>${FILE_LOG_PATTERN}pattern>
        encoder>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>debuglevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>

    
    <appender name="info_file" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${INFO_LOG_FILE_NAME_PATTERN}fileNamePattern>
            
            <maxHistory>${info.file.max.history}maxHistory>
            
            <totalSizeCap>${info.file.total.size.cap}totalSizeCap>
            
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>${info.file.max.file.size}maxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>
        <encoder>
            <pattern>${FILE_LOG_PATTERN}pattern>
        encoder>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>infolevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>

    
    <appender name="warn_file" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${WARN_LOG_FILE_NAME_PATTERN}fileNamePattern>
            
            <maxHistory>${warn.file.max.history}maxHistory>
            
            <totalSizeCap>${warn.file.total.size.cap}totalSizeCap>
            
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>${warn.file.max.file.size}maxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>
        <encoder>
            <pattern>${FILE_LOG_PATTERN}pattern>
        encoder>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>warnlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>

    
    <appender name="error_file" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${ERROR_LOG_FILE_NAME_PATTERN}fileNamePattern>
            
            <maxHistory>${error.file.max.history}maxHistory>
            
            <totalSizeCap>${error.file.total.size.cap}totalSizeCap>
            
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>${error.file.max.file.size}maxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>
        <encoder>
            <pattern>${FILE_LOG_PATTERN}pattern>
        encoder>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>errorlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>
    
    
    
    <logger name="com.sourceplan" level="debug"/>
    <root level="debug">
        <appender-ref ref="console"/>
        <appender-ref ref="debug_file"/>
        <appender-ref ref="info_file"/>
        <appender-ref ref="warn_file"/>
        <appender-ref ref="error_file"/>
    root>
    
    
    
    
 configuration>

 

三、logback.properties

# 应用名称
app.name=logback-test

# 日志存放地址
log.dir=logs

# 日志存放时间配置(单位:天)
debug.file.max.history=30
info.file.max.history=30
warn.file.max.history=30
error.file.max.history=30

# 日志文件存放上限制,超过指定值清除历史日志文件
debug.file.total.size.cap=1GB
info.file.total.size.cap=10GB
warn.file.total.size.cap=1GB
error.file.total.size.cap=1GB

# 单文件最大值
debug.file.max.file.size=100MB
info.file.max.file.size=100MB
warn.file.max.file.size=100MB
error.file.max.file.size=100MB

 

四、使用方式

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class App {
    
    public static Logger logger = LoggerFactory.getLogger(App.class);
    
    public static void main( String[] args ) {
        
        logger.debug( "logback {}", "debug" );
        logger.info( "logback {}", "info" );
        logger.warn( "logback {}", "warn" );
        logger.error( "logback {}", "error" );
        
    }
}

 

 

参考:https://blog.csdn.net/liaoguolingxian/article/details/79297188

 

你可能感兴趣的:(logback配置和使用)