如何在 log-back.xml 中包含 xml
how to include xml in log-back.xml
我正在编写一个自定义的 spring 启动启动项目用于日志记录。我的 logback-spring.xml 变大了,想把附加程序放在单独的文件中(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>
文件-appender.xml
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>testFile.log</file>
<append>true</append>
<!-- set immediateFlush to false for much higher logging throughput -->
<immediateFlush>true</immediateFlush>
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
但是当在我的 spring 引导应用程序中使用此启动程序时,它无法解析控制台和文件附加程序文件。
我试过放置以下路径:
<include file="/src/main/resources/console-appender.xml"/>
<include file="./console-appender.xml"/>
<include resource="console-appender.xml"/>
在这种情况下如何正确包含文件?
是的,这是可能的。 Logback 使用 Joran,因此您必须遵循 Joran 规则。
有关详细信息,请参阅 documentation。
无论如何,这是完成您正在寻找的事情的首选方式。
例如,我定义了两个文件,然后您可以根据需要对其进行调整。
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="console-appender.xml"/>
<root level="debug">
<appender-ref ref="CONSOLE"/>
</root>
</configuration>
控制台-appender.xml
<included>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{"yyyy-MM-dd HH:mm:ss.SSS", Europe/Rome} [%mdc{id}] [%-11thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
</included>
请注意 <included>
标签是 强制性的
我正在编写一个自定义的 spring 启动启动项目用于日志记录。我的 logback-spring.xml 变大了,想把附加程序放在单独的文件中(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>
文件-appender.xml
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>testFile.log</file>
<append>true</append>
<!-- set immediateFlush to false for much higher logging throughput -->
<immediateFlush>true</immediateFlush>
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
但是当在我的 spring 引导应用程序中使用此启动程序时,它无法解析控制台和文件附加程序文件。
我试过放置以下路径:
<include file="/src/main/resources/console-appender.xml"/>
<include file="./console-appender.xml"/>
<include resource="console-appender.xml"/>
在这种情况下如何正确包含文件?
是的,这是可能的。 Logback 使用 Joran,因此您必须遵循 Joran 规则。
有关详细信息,请参阅 documentation。
无论如何,这是完成您正在寻找的事情的首选方式。
例如,我定义了两个文件,然后您可以根据需要对其进行调整。
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="console-appender.xml"/>
<root level="debug">
<appender-ref ref="CONSOLE"/>
</root>
</configuration>
控制台-appender.xml
<included>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{"yyyy-MM-dd HH:mm:ss.SSS", Europe/Rome} [%mdc{id}] [%-11thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
</included>
请注意 <included>
标签是 强制性的