spring boot(三)logback日志配置

spring boot中默认继承了logback的日志配置。

  • 配置application.properties
# config logback
logging.config=classpath:logback-spring.xml
logging.path=../webLog/demo-1.0.0
logging.level.root=info

logging.path可以是相对路径,也可以是绝对路径
logging.config是logback的配置路径

  • 添加loaback的配置文件 logback-spring.xml
<configuration scan="true" scanPeriod="10 seconds">
    <include resource="org/springframework/boot/logging/logback/base.xml" />

    <appender name="INFO_FILE"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_PATH}/info.logFile>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/info-%d{yyyyMMdd}.log.%ifileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>500MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
            <maxHistory>2maxHistory>
        rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}
                -%msg%n
            Pattern>
        layout>
    appender>

    <appender name="ERROR_FILE"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERRORlevel>
        filter>
        <File>${LOG_PATH}/error.logFile>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/error-%d{yyyyMMdd}.log.%i
            fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>500MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
            <maxHistory>2maxHistory>
        rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}
                -%msg%n

            Pattern>
        layout>
    appender>

    
    

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

configuration>

logback的配置文件请自行百度。
这样在具体的class中使用
private Logger logger = LoggerFactory.getLogger(getClass());
就可以打印出日志啦。

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