spingboot用logback-spring.xml替換logback.xml
阿新 • • 發佈:2019-07-12
logback.xml載入早於application.properties,所以如果你在logback.xml使用了變數時,而恰好這個變數是寫在application.properties時,那麼就會獲取不到,只要改成logback-spring.xml就可以解決。
pom檔案
只繼承了spring-boot-starter-parent原因請看下圖maven繼承樹
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.test</groupId> <artifactId>Quartz</artifactId> <version>0.0.1-SNAPSHOT</version> <name>Quartz</name> <description>Quartz project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
resources下新增logback-spring.xml
以下是我的配置以供參考
<configuration>
<contextName>spring-boot-demo</contextName>
<!--各專案日誌儲存統一父目錄->路徑自己設定絕對路徑和相對路徑都可以-->
<property name="LOG_DIR" value="C:/TEMP" />
<!--最大儲存天數-->
<property name="KEEP_MAX_DAY" value="180" />
<!--總最大儲存檔案大小-->
<property name="KEEP_TOTAL_MAX_SIZE" value="5GB" />
<!--單檔案最大儲存檔案大小-->
<property name="KEEP_FILE_MAX_SIZE" value="100MB" />
<!--輸出到控制檯-->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<!--輸出到ERROR事件記錄檔案-->
<appender name="errorFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${LOG_DIR}/${CONTEXT_NAME}/error/${CONTEXT_NAME}-error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_DIR}/${CONTEXT_NAME}/error/${CONTEXT_NAME}-error-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<maxFileSize>${KEEP_FILE_MAX_SIZE}</maxFileSize>
<maxHistory>${KEEP_MAX_DAY}</maxHistory>
<totalSizeCap>${KEEP_TOTAL_MAX_SIZE}</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<!--輸出到WARN事件記錄檔案-->
<appender name="warnFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>WARN</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${LOG_DIR}/${CONTEXT_NAME}/warn/${CONTEXT_NAME}-warn.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_DIR}/${CONTEXT_NAME}/warn/${CONTEXT_NAME}-warn-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<maxFileSize>${KEEP_FILE_MAX_SIZE}</maxFileSize>
<maxHistory>${KEEP_MAX_DAY}</maxHistory>
<totalSizeCap>${KEEP_TOTAL_MAX_SIZE}</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<!--輸出到INFO事件記錄檔案-->
<appender name="infoFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${LOG_DIR}/${CONTEXT_NAME}/info/${CONTEXT_NAME}-info.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_DIR}/${CONTEXT_NAME}/info/${CONTEXT_NAME}-info-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<maxFileSize>${KEEP_FILE_MAX_SIZE}</maxFileSize>
<maxHistory>${KEEP_MAX_DAY}</maxHistory>
<totalSizeCap>${KEEP_TOTAL_MAX_SIZE}</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.springframework.jdbc.core" level="debug" additivity="false">
<appender-ref ref="console" />
</logger>
<logger name="com.springboot.web.dynamic_debug" level="debug" additivity="false">
<appender-ref ref="console" />
</logger>
<root level="info">
<appender-ref ref="console" />
<appender-ref ref="errorFile" />
<appender-ref ref="warnFile" />
<appender-ref ref="infoFile" />
</root>
</config