Springboot 日志管理配置logback-spring.xml

作为企业级的或者稍大型的开发项目,日志是必不可少的模块,在这里介绍了基础springboot的日志配置.

首先创建一个管理日志的配置文件,然在resources包下面:
这里写图片描述

配置文件名规定的起logback-spring.xml获取logback.xml都是可以的.
我这里日志用到的slfj4作为标准,logbac作为日志系统 .

那就直接上代码吧!





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


    
    <appender name="fileDebugLog" class="ch.qos.logback.core.rolling.RollingFileAppender">        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">  
            <level>DEBUGlevel>                     
            <onMatch>ACCEPTonMatch>                     
            <onMismatch>DENYonMismatch>                     
        filter>
        <encoder>
            <pattern>
               %d - %c- %msg%n
            pattern>
            <charset>UTF-8charset>
        encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>D:/MsSystemSettingService/log/debug.%d.logfileNamePattern>


        rollingPolicy>
    appender>

    
    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">   
            <level>INFOlevel>                     
            <onMatch>ACCEPTonMatch>               
            <onMismatch>DENYonMismatch>           
        filter>
        <encoder>
            <pattern>
                %d - %c - %msg%n
            pattern>
            <charset>UTF-8charset>
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>D:/MsSystemSettingService/log/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>
                %d - %c - %msg%n
            pattern>
            <charset>UTF-8charset>
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>D:/MsSystemSettingService/log/error.%d.logfileNamePattern>
        rollingPolicy>
    appender>

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

这里就完成了简单的日志配置,已经足够满足一般项目的需求了,当然了要想用到这个功能,项目具体的代码里面还要写上

    private Logger log = LoggerFactory.getLogger(***.class);
    log.info("--------- *********------------");

这样完成了日志输出管理了.

你可能感兴趣的:(java开发小知识笔录)