Spring Boot 配置logback日志实现

application配置

默认情况下spring boot是不会将日志输出到日志文件中,我们可以配置application的方式进行日志输出

# 注意logging.file与logging.path同时使用时,不会进行叠加,即不会生成path+file的日志文件
logging.file=D:\\MyLog\\MyLog.log
logging.level.root=info
#logging.path=D:\\MyLog
  • 若只配置logging.path,那么将会在指定文件夹下生成一个日志文件名为spring.log的文件(该文件名是固定的,不能更改)。如果path路径不存在,会自动创建该文件夹
  • 若只配置logging.file,那将会在项目的当前路径下生成一个xxx.log日志文件。可以使用绝对路径,此时会在指定路径下创建文件夹和相应的日志文件
logging.file=D:\\MyLog\\MyLog.log
  • 注意logging.file与logging.path同时使用时,不会进行叠加,即不会生成path+file的日志文件

logback-spring.xml配置

https://docs.spring.io/spring-boot/docs/2.1.5.RELEASE/reference/htmlsingle/#boot-features-logging-format
通过搜索logback-spring.xml,我们可以知道官方默认的logback配置文件名为logback.xml, logback-spring.xml(推荐命名)
官方推荐使用的xml名字的格式为:logback-spring.xml而不是logback.xml,因为带spring后缀的可以使用这个标签



<configuration scan="false" scanPeriod="60 seconds" debug="false">
    
    <property name="LOG_HOME" value="/app/log"/>

    
    <property name="appName" value="mocha-springboot">property>

    
    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
        layout>
    appender>

    
    <appender name="appInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${LOG_HOME}/${appName}-info.logfile>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            
            <level>ERRORlevel>
            
            <onMatch>DENYonMatch>
            
            <onMismatch>ACCEPTonMismatch>
        filter>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${LOG_HOME}/${appName}-info-%d{yyyy-MM-dd}-%i.logfileNamePattern>
            
            <MaxHistory>365MaxHistory>
            
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>
        
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%npattern>
        layout>
    appender>

    
    <appender name="appErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${LOG_HOME}/${appName}-error.logfile>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            
            <level>ERRORlevel>
        filter>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${LOG_HOME}/${appName}-error-%d{yyyy-MM-dd}-%i.logfileNamePattern>
            
            <MaxHistory>365MaxHistory>
            
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>
        
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%npattern>
        layout>
    appender>

    
    
    <logger name="org.springframework" level="debug" additivity="false">logger>

    
    <logger name="com.apache.ibatis" level="TRACE"/>
    <logger name="java.sql.Connection" level="DEBUG"/>
    <logger name="java.sql.Statement" level="DEBUG"/>
    <logger name="java.sql.PreparedStatement" level="DEBUG"/>

    
    <root level="info">
        <appender-ref ref="stdout"/>
        <appender-ref ref="appInfoLog"/>
        <appender-ref ref="appErrorLog"/>
    root>
configuration>

你可能感兴趣的:(Java进化之旅,Spring,Boot,Java,日志,Spring,Boot,logback,日志实现)