第八篇 SpringBoot 2 x日志配置

在应用中如何使用日志一直以来都是一个问题,前些年流行用AOP的方式在主要方式上切进去,最近几天又看到一篇博文证明了使用这个方式是很不科学的,所以自己记录一篇关于日志的理解吧。 首先关于选用哪种日志也是纠结了半天,log4j,log4j2,slf4j,最终还是选择了LogBack来记录日志,为什么呢,因为这个是SpringBoot自带的呀...

在SpringBoot中LogBack是作为默认的日志依赖关于他的配置的只需要在src/main/resource下面新建一个logback-spring.xml就可以了。为什么是logback-spring.xml而不是logback.xml呢,因为这个文件可以针对不同的profile使用不同的日志配置 ###logback-spring.xml

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

    "logger.path" value="D:/logs" />

    
    
    "clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
    "wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
    "wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
    
    "CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>


    
    "CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        "ch.qos.logback.classic.filter.ThresholdFilter">
            info
        
        
            ${CONSOLE_LOG_PATTERN}
            UTF-8
        
    


    
    
    "DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        ${logger.path}/log_debug.log
        
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
            UTF-8 
        
        
        "ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            ${logger.path}/debug/log-debug-%d{yyyy-MM-dd}.%i.log
            "ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                100MB
            
            
            15
        
        
        "ch.qos.logback.classic.filter.LevelFilter">
            debug
            ACCEPT
            DENY
        
    

    
    "INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        ${logger.path}/log_info.log
        
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
            UTF-8
        
        
        "ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            ${logger.path}/info/log-info-%d{yyyy-MM-dd}.%i.log
            "ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                100MB
            
            
            15
        
        
        "ch.qos.logback.classic.filter.LevelFilter">
            info
            ACCEPT
            DENY
        
    

    
    "WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        ${logger.path}/log_warn.log
        
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
            UTF-8 
        
        
        "ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            ${logger.path}/warn/log-warn-%d{yyyy-MM-dd}.%i.log
            "ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                100MB
            
            
            15
        
        
        "ch.qos.logback.classic.filter.LevelFilter">
            warn
            ACCEPT
            DENY
        
    


    
    "ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        ${logger.path}/log_error.log
        
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
            UTF-8 
        
        
        "ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            ${logger.path}/error/log-error-%d{yyyy-MM-dd}.%i.log
            "ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                100MB
            
            
            15
        
        
        "ch.qos.logback.classic.filter.LevelFilter">
            ERROR
            ACCEPT
            DENY
        
    
    

    "info">
        "CONSOLE" />
        "DEBUG_FILE" />
        "INFO_FILE" />
        "WARN_FILE" />
        "ERROR_FILE" />
    


复制代码

复制粘贴就好了,如果不需要某个level在文件里删掉就OK

DemoController.java

package com.priv.gabriel.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * Created with Intellij IDEA.
 *
 * @Author: Gabriel
 * @Date: 2018-10-14
 * @Description:
 */
@RestController
@RequestMapping("/demo")
public class DemoController {

	private static final Logger logger = LoggerFactory.getLogger(DemoController.class);

	@RequestMapping("/test")
	public void test(){
		logger.info("进入test方法");
		logger.debug("进入test方法");
		logger.warn("进入test方法");
		logger.error("进入test方法");
	}
}
复制代码

好啦,logback的整合就结束了。 项目下载地址

转载于:https://juejin.im/post/5c1ca9d9e51d4522f54e9bbe

你可能感兴趣的:(第八篇 SpringBoot 2 x日志配置)