springboot中logback日志配置

spring-boot 中的logback日志学习使用

文档目录

  • spring-boot 中的logback日志学习使用
    • 1. 日志级别
    • 2. Logback日志
    • 3. 配置Logback日志
    • 4. 错误日志输入到日志文件中

1. 日志级别

日志记录器(Logger)的行为分等级的。如下表所示:

OFF FATAL ERROR WARN DEBUG ALL

默认情况下,spring boot 从控制台打印出来的日志级别只有INFO及以上级别,可以配置日志级别

#设置日志级别
loging.level.root=WARN

这种方式只能将日志打印在控制台上


2. Logback日志

spring boot内部使用Logback作为日志实现的框架。

Logback和log4j非常相似,如果你对log4j很熟悉,那对logback很快就会得心应手。

logback相对于log4j的一些优点:https://blog.csdn.net/caisini_vc/article/details/48551287


3. 配置Logback日志

1. 删除appplication.properties 或application.yml 中的所有日志配置

2. 安装idea彩色日志插件: grep-console, (此处可忽略,springboot集成了该插件)

3.resources 中创建logback-spring.xml 文件,该文件内容如下:


<configuration  scan="true" scanPeriod="10 seconds">
    
    
    
    
    <contextName>logbackcontextName>
    
    
    <property name="log.path" value="E:/ProcessDocuments/项目测试/online-education/log" />
    
    
    
    
    
    
    
    <property name="CONSOLE_LOG_PATTERN" value="%yellow(%date{yyyy-MM-dd HH:mm:ss}) |%highlight(%-5level) |%blue(%thread) |%blue(%file:%line) |%green(%logger) |%cyan(%msg%n)"/>
    
    
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">

        

        

        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">

            <level>INFOlevel>

        filter>

        <encoder>

            <Pattern>${CONSOLE_LOG_PATTERN}Pattern>

            

            <charset>UTF-8charset>

        encoder>

    appender>

    

    

    <appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">

        

        <file>${log.path}/log_info.logfile>

        

        <encoder>

            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>

            <charset>UTF-8charset>

        encoder>

        

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

            

            <fileNamePattern>${log.path}/info/log-info-%d{yyyy-MM-dd}.%i.logfileNamePattern>

            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">

                <maxFileSize>100MBmaxFileSize>

            timeBasedFileNamingAndTriggeringPolicy>

            

            <maxHistory>15maxHistory>

        rollingPolicy>

        

        <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">

        

        <file>${log.path}/log_warn.logfile>

        

        <encoder>

            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>

            <charset>UTF-8charset> 

        encoder>

        

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

            <fileNamePattern>${log.path}/warn/log-warn-%d{yyyy-MM-dd}.%i.logfileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">

                <maxFileSize>100MBmaxFileSize>

            timeBasedFileNamingAndTriggeringPolicy>

            

            <maxHistory>15maxHistory>

        rollingPolicy>

        

        <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">

        

        <file>${log.path}/log_error.logfile>

        

        <encoder>

            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>

            <charset>UTF-8charset> 

        encoder>

        

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

            <fileNamePattern>${log.path}/error/log-error-%d{yyyy-MM-dd}.%i.logfileNamePattern>

            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">

                <maxFileSize>100MBmaxFileSize>

            timeBasedFileNamingAndTriggeringPolicy>

            

            <maxHistory>15maxHistory>

        rollingPolicy>

        

        <filter class="ch.qos.logback.classic.filter.LevelFilter">

            <level>ERRORlevel>

            <onMatch>ACCEPTonMatch>

            <onMismatch>DENYonMismatch>

        filter>

    appender>

    

    

    

    <springProfile name="dev">

        

        <logger name="com.guli" level="INFO" />

        
        <root level="INFO">
            <appender-ref ref="CONSOLE" />
            <appender-ref ref="INFO_FILE" />
            <appender-ref ref="WARN_FILE" />
            <appender-ref ref="ERROR_FILE" />
        root>
    springProfile>
    

    <springProfile name="pro">

        <root level="INFO">
            <appender-ref ref="CONSOLE" />
            <appender-ref ref="DEBUG_FILE" />
            <appender-ref ref="INFO_FILE" />
            <appender-ref ref="ERROR_FILE" />
            <appender-ref ref="WARN_FILE" />
        root>
    springProfile>
configuration>

4. 错误日志输入到日志文件中

  1. 在异常处理类或其他类上添加@Slf4j 1注解
  2. 在该类中的方法上加上 log.error(e.getMessage()); 1 语句
//@ControllerAdvice是一个为控制器增强的注解,通常用于统一异常的处理
@ControllerAdvice
@ResponseBody
@Slf4j		//  <------  添加@Slf4j 注解
public class GlobalExceptionHandler {
    @ExceptionHandler(MyException.class)
    public CommonResult errorMyExceptionHandler(MyException myException){
        myException.printStackTrace();
        //将错误信息写入日志中, 添加 " log.error(e.getMessage()); " 语句
        log.error(ExceptionUtil.getMessage(myException));
        return CommonResult.error().code(myException.getCode()).message(myException.getMsg());
    }
}

ExceptionUtil.java工具类链接https://editor.csdn.net/md/?articleId=126191046


  1. 属于lombok 依赖 ↩︎ ↩︎

你可能感兴趣的:(springboot,spring,boot,java)