Spring Boot Logging 配置
Spring Boot 能够使用Logback, Log4J2 , java util logging 作为日志记录工具。Spring Boot 默认使用Logback作为日志记录工具。日志默认输出到控制台但也能输出到文件中。我们通过spring-boot-starter-logging 加入Logback依赖,其实只要我们加入任意的Spring Boot starter 都会默认引入spring-boot-starter-logging,因此 我们不需要分开加入他们。
在resource添加 logback-spring.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!--引入默认的一些设置-->
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<springProperty scope="context" name="LOG_FILE" source="spring.application.name" defaultValue="default" />
<!-- source:是application文件中配置的属性 -->
<springProperty scope="context" name="LOG_PATH" source="logging.file.path" defaultValue="logs" />
<!--web信息-->
<logger name="org.springframework.web" level="info"/>
<!--写入日志到控制台的appender,用默认的,但是要去掉charset,否则windows下tomcat下乱码-->
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>TRACE</level>
<level>DEBUG</level>
</filter>
</appender>
<appender name="dailyRollingFileAppender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<FileNamePattern>${LOG_PATH}/${LOG_FILE}.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<maxHistory>120</maxHistory>
<maxFileSize>50MB</maxFileSize>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>
</appender>
<appender name="errorFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<FileNamePattern>${LOG_PATH}/${LOG_FILE}-error.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<maxHistory>45</maxHistory>
<maxFileSize>50MB</maxFileSize>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>TRACE</level>
<level>INFO</level>
<level>WARN</level>
</filter>
</appender>
<!--生产环境:打印控制台和输出到文件-->
<springProfile name="pro">
<root level="info">
<appender-ref ref="consoleAppender" />
<appender-ref ref="errorFileAppender" />
<appender-ref ref="dailyRollingFileAppender"/>
</root>
</springProfile>
<!--开发环境:打印控制台-->
<springProfile name="dev">
<root level="info">
<appender-ref ref="consoleAppender" />
<appender-ref ref="errorFileAppender" />
<appender-ref ref="dailyRollingFileAppender"/>
</root>
</springProfile>
<!--本地环境:打印控制台-->
<springProfile name="local">
<root level="info">
<appender-ref ref="consoleAppender" />
</root>
</springProfile>
<!--测试环境:打印控制台-->
<springProfile name="test">
<root level="info">
<appender-ref ref="consoleAppender" />
<appender-ref ref="dailyRollingFileAppender"/>
</root>
</springProfile>
</configuration>
具体配置可以根据需求更改
使用Git管理根据
在.gitignore添加:
# Log file
<<<<<<< HEAD
../helmet/log/logs/helmet-print_log_warn.log
=======
*.log
>>>>>>> b3084d48ba2f135e5058603d582a2bcf46109d72
可以忽略生成的日志文件,不进行Git操作
文章地址: https://blog.csdn.net/Platinum_stars/article/details/108011867