springboot日志配置,Spring Boot系列——日志配置

springboot默认是用logback来记日志的。每次启动服务时见下面最先打印出来的日志:

"C:\Program Files\Java\jdk1.8.0_40\bin\java.exe" ...
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/workspace/m3/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/workspace/m3/org/slf4j/slf4j-log4j12/1.7.26/slf4j-log4j12-1.7.26.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]

、、、各种初始化、、、
、、、接下来是那个熟悉的图标、、、

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.7.RELEASE)

 

如果不做日志配置,默认是在服务下生成spring.log。

 

要自定义日志配置,简单的说,可在application.properties里借助logging.file或logging.path做如下设置:

logging.file=logs/draft_spider.log
logging.level.com.draft.mapper= debug

 

因为系统不同业务日志量较大,为了方便定位日志快速排查问题,我要把某个业务的日志打印在特定的文件里。于是,在项目的resources下创建logback.xml,细化日志的记录。

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="false" scanPeriod="300 seconds" debug="true">
    <!--本地日志目录-->
    <property name="USER_HOME" value="logs/"/>
    <property name="LOG_MSG" value="%X{sid}%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p [%c:%L] - %m%n"/>
    <property name="LOG_DIR" value="${USER_HOME}/%d{yyyyMMdd}"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${LOG_MSG}</pattern>
        </encoder>
    </appender>

    <appender name="INFO_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${USER_HOME}/spider_info.log</file>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_DIR}/spider_info%i.log</fileNamePattern>
            <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>20MB</maxFileSize>
            </TimeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder>
            <pattern>${LOG_MSG}</pattern>
        </encoder>
    </appender>
    <appender name="ORDERLIST_SPIDER_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${USER_HOME}/orderlist_spider.log</file>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_DIR}/orderlist_spider%i.log</fileNamePattern>
            <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>20MB</maxFileSize>
            </TimeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder>
            <pattern>${LOG_MSG}</pattern>
        </encoder>
    </appender>

    <appender name="ERROR_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${USER_HOME}/spider_error.log</file>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_DIR}/spider_error%i.log</fileNamePattern>
            <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>20MB</maxFileSize>
            </TimeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder>
            <pattern>${LOG_MSG}</pattern>
        </encoder>
    </appender>

    <!--<logger name="druid.sql.Statement" level="DEBUG" additivity="false">-->
    <!--<appender-ref ref="SQL_LOG"/>-->
    <!--</logger>-->
    <logger name="spiderLog" level="DEBUG" additivity="false">
        <appender-ref ref="ORDERLIST_SPIDER_LOG"/>
    </logger>
    <logger name="com.draft.processor.TcpjwPageProcessor" level="DEBUG" additivity="false">
        <appender-ref ref="ORDERLIST_SPIDER_LOG"/>
    </logger>
    <logger name="com.draft.task.SpiderTaskPage" level="DEBUG" additivity="false">
        <appender-ref ref="ORDERLIST_SPIDER_LOG"/>
    </logger>
    <logger name="com.draft.task.SpiderTask" level="DEBUG" additivity="false">
        <appender-ref ref="ORDERLIST_SPIDER_LOG"/>
    </logger>

    <root level="INFO">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="ERROR_LOG"/>
        <appender-ref ref="INFO_LOG"/>
        <!--<appender-ref ref="ORDERLIST_SPIDER_LOG"/>-->
    </root>
</configuration>

 

 

ref:Spring Boot系列——日志配置

 

点赞