Spring-Boot日志配置

Spring-Boot日志配置默认文件名llogback-spring.xml,放在src/main/resouces目录下

<?xml version="1.0" encoding="UTF-8"?>

<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />

<!--<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">-->
<!--<encoder>-->
<!--<pattern>${CONSOLE_LOG_PATTERN}</pattern>-->
<!--<charset>UTF-8</charset>-->
<!--</encoder>-->
<!--</appender>-->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--<file>/Users/pengsir/Downloads/logs/api/api.log</file>-->
<!--<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">-->
<!--<fileNamePattern>/Users/pengsir/Downloads/logs/api/api.%i.log.zip</fileNamePattern>-->
<!--<minIndex>1</minIndex>-->
<!--<maxIndex>50</maxIndex>-->
<!--</rollingPolicy>-->
<!--<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">-->
<!--<maxFileSize>30MB</maxFileSize>-->
<!--</triggeringPolicy>-->
<!--按时间分割日志-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<FileNamePattern>../api.%d{yyyy-MM-dd}.log</FileNamePattern>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>

<springProfile name="dev">
<logger name="org.springframework" level="DEBUG" />
<logger name="com.jiuhuar" level="DEBUG"/>
<root level="DEBUG">
<appender-ref ref=“CONSOLE"/>
</root>
</springProfile>

<springProfile name=“preview,prod">
<logger name="org.springframework" level="INFO" />
<logger name="com.jiuhuar" level="INFO"/>
<root level="INFO">
<appender-ref ref="FILE"/>
</root>
</springProfile>
</configuration>

解释

<springProfile name="dev"> 表示dev 环境的日志配置
<logger name="org.springframework" level="INFO" /> 表示 包org.springframework下的日志级别为INFO

<root level="DEBUG">  // 采用DEBUG模式
<appender-ref ref=“CONSOLE"/> // 控制台输出日志 ref=“FILE” 输出到日志文件中
</root>

日志文件配置 可以采用按日期分割/按日志大小分割

推荐配置

<?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" />
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />

<springProfile name="dev">
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
</springProfile>

<springProfile name="preview, prod">
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</springProfile>
</configuration>

在配置文件application.yml中配置日志路径 不同环境配置不同,可自己调整

logging:
level:
org.springboot.web: INFO
file: /data/log/admin/admin.log
坚持原创技术分享,您的支持将鼓励我继续创作!