我正在编写一个用于日志记录的自定义 Spring Boot 启动项目。我的 logback-spring.xml 变大了,想把 appender 放在单独的文件(console-appender.xml 和 file-appender.xml)中,并想包含在 logback-spring.xml 文件中,如下所示。我将所有三个 xml 放在我的自定义启动项目的 src/main/resources 文件夹中。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProperty scope="context" name="JSONLOGSENABLED" source="spring.logs.json.enabled" defaultValue="true" />
<springProperty scope="context" name="JSONLOGSAPPENDER" source="spring.logs.json.appender" defaultValue="console" />
<if condition='property("JSONLOGSENABLED").equalsIgnoreCase("true")'>
<then>
<include file="{path to file}/console-appender.xml"/>
<include file="{path to file}/file-appender.xml"/>
<root level="INFO">
<if condition='property("JSONLOGSAPPENDER").equalsIgnoreCase("file")'>
<then>
<appender-ref ref="FILEJSON" />
</then>
<else>
<appender-ref ref="CONSOLEJSON" />
</else>
</if>
</root>
</then>
</if>
<logger
name="org.springframework.web.filter.CommonsRequestLoggingFilter">
<level value="DEBUG" />
</logger>
</configuration>
Run Code Online (Sandbox Code Playgroud)
文件appender.xml
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>testFile.log</file>
<append>true</append>
<!-- set immediateFlush to false for …Run Code Online (Sandbox Code Playgroud)