Spring Boot中特定软件包的自定义日志文件 [英] Custom log file for specific packages in Spring boot
问题描述
我有一个带有专门操作的Java包.从某种意义上讲,它们很少使用,并且我不想让它们与常规日志记录混在一起.
I have a java package with specialized operations. Specialized in the sense that they are rarely used and i don't want to have them be mixed with normal logging.
我知道添加logging.file=myapplication.log
会将日志记录重定向到该文件,但是是否有办法仅指定从特定程序包到另一个文件的日志记录?像logging.file.my.package=special.log
吗?
I know that adding logging.file=myapplication.log
will redirect the logging to this file but is there a way to specify only logging from specific packages to another file? Like logging.file.my.package=special.log
?
推荐答案
Spring使用Logback作为默认记录器.根据官方文档,您可以自行设置 logback.xml ,以将特殊"行为添加到默认日志记录机制中:
Spring uses Logback as default logger. According to the official doc you can setup logback.xml yourself to add to the default logging mechanism your 'special' behavior:
<?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/file-appender.xml" />
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<property name="SPECIAL_FILE_NAME" value="special"/>
<appender name="SPECIAL_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} [%-10.10thread] %-5level %30.30logger{29}:%-4line %msg%n</pattern>
<charset>utf8</charset>
</encoder>
<file>logs/${SPECIAL_FILE_NAME}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>logs/${SPECIAL_FILE_NAME}-%i.log</fileNamePattern>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
<logger name="logging.file.my.package" level="debug" additivity="false">
<appender-ref ref="SPECIAL_FILE"/>
</logger>
<root level="INFO">
<appender-ref ref="FILE" />
<appender-ref ref="CONSOLE" />
</root>
</configuration>
在这里,我们使用Spring默认的FILE
和CONSOLE
附加程序照常记录应用程序信息(logging.file.my.package
除外),并使用SPECIAL_FILE
附加程序将信息从该程序包记录到文件log/special.log
.
Here we use Spring default FILE
and CONSOLE
appenders to log app info as usual (except logging.file.my.package
), and use SPECIAL_FILE
appender to log info from this package to file log/special.log
.
这篇关于Spring Boot中特定软件包的自定义日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!