Spring Boot 日誌系統

原文java

切換日誌級別爲 DEBUGspring

使用命令行,能夠在其後添加 --debugapache

application.properties 配置 debug=truespringboot

spring.output.ansi.enabled=DETECT 檢測終端是否支持ANSI輸出,支持則打印彩色日誌。默認爲 NEVER,不開啓。還能夠設在置爲 ALWAYS。app

文件輸出,在 application.properties 中設置 loggin.file 或 logging.path 屬性。框架

包級別控制,配置 logging.level.*=LEVEL。好比 logging.level.com.didispace=DEBUG:com.didispace包下全部class以DEBUG級別輸出。logging.level.root=WARN:root日誌以WARN級別輸出。maven

自定義日誌配置(不使用自帶的日誌系統)spring-boot

命名規則spa

Log4j:log4j-spring.properties, log4j-spring.xml, log4j.properties, log4j.xml命令行

Log4j2:log4j2-spring.xml, log4j2.xml

除此以外,還能夠 使用 logging.config=log4j2-spring.xml 指定文件的位置。

定義輸出格式

logging.pattern.console:定義輸出到控制檯的樣式

logging.pattern.file:定義輸出到文件的樣式

配置 log 顯示行號:

logging:
  pattern:
    console: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}.%M[%line] - %msg%n"
    file: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}.%M[%line] - %msg%n"

=======================================================================================

說明

關於採用 Logback

配置外化文件:在外化配置文件 application.properties 中配置

debug=true
logging.config=classpath:logger/logback-spring.xml
logging.file=target/logs/spring-boot-logging.log

自定義 logback-spring.xml 

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />
    <appender name="TIME_FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <file>${LOG_FILE}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i</fileNamePattern>
            <maxHistory>365</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
    </appender>
    <root level="INFO">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="TIME_FILE" />
    </root>
</configuration>

關於引入 Log4j2

加入依賴:在使用 maven建立spring boot 工程時,引入了 spring-boot-starter,其中包含了 spring-boot-starter-logging,採用的是 spring boot 默認的日誌框架 Logback。在使用 Log4j2 以前須要排除該包的依賴,並引入 log4j2 的依賴。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion> 
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

配置外化文件

logging.config=classpath:logger/log4j2-spring.xml

自定義 log4j2-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" strict="true" name="XMLConfigTest"
               packages="org.apache.logging.log4j.test">
    <Properties>
        <Property name="filename">target/logs/spring-boot-logging.log</Property>
    </Properties>
    <Filter type="ThresholdFilter" level="trace"/>

    <Appenders>
        <Appender type="Console" name="STDOUT">
            <Layout type="PatternLayout" pattern="%m MDC%X%n"/>
            <Filters>
                <Filter type="MarkerFilter" marker="FLOW" onMatch="DENY" onMismatch="NEUTRAL"/>
                <Filter type="MarkerFilter" marker="EXCEPTION" onMatch="DENY" onMismatch="ACCEPT"/>
            </Filters>
        </Appender>
        <Appender type="Console" name="FLOW">
            <Layout type="PatternLayout" pattern="%C{1}.%M %m %ex%n"/><!-- class and line number -->
            <Filters>
                <Filter type="MarkerFilter" marker="FLOW" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
                <Filter type="MarkerFilter" marker="EXCEPTION" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
        </Appender>
        <Appender type="File" name="File" fileName="${filename}">
            <Layout type="PatternLayout">
                <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
            </Layout>
        </Appender>
        <Appender type="List" name="List">
        </Appender>
    </Appenders>

    <Loggers>
        <Logger name="org.apache.logging.log4j.test1" level="debug" additivity="false">
            <Filter type="ThreadContextMapFilter">
                <KeyValuePair key="test" value="123"/>
            </Filter>
            <AppenderRef ref="STDOUT"/>
        </Logger>

        <Logger name="org.apache.logging.log4j.test2" level="debug" additivity="false">
            <AppenderRef ref="File"/>
        </Logger>

        <Root level="trace">
            <AppenderRef ref="List"/>
        </Root>
    </Loggers>

</Configuration>
相關文章
相關標籤/搜索