我的log4j2
版本为2.8
我在log4j2.xml
中有以下src/main/resources
:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="120">
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH.mm.ss} %5p %c{1}:%L - %m%n</Pattern>
</PatternLayout>
</Console>
<RollingRandomAccessFile name="RollingRandomAccessFile" fileName="${env:CATALINA_HOME:-.}/logs/applicationlogs.log"
filePattern="${env:CATALINA_HOME:-.}/logs/$${date:yyyy-MM}/applicationlogs-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH.mm.ss} %5p %c{1}:%L - %m%n</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="50 MB"/>
</Policies>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<AsyncLogger name="com.mypackage" level="trace" includeLocation="true" additivity="false">
<AppenderRef ref="RollingRandomAccessFile"/>
</AsyncLogger>
<AsyncLogger name="org.springframework.jdbc" level="debug" includeLocation="true" additivity="false">
<AppenderRef ref="RollingRandomAccessFile"/>
</AsyncLogger>
<Root level="info" includeLocation="true">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="RollingRandomAccessFile"/>
</Root>
</Loggers>
</Configuration>
这会正确记录到文件中,但控制台上没有任何内容。
这里有什么问题?
答案 0 :(得分:1)
一旦事件到达记录器,其可加性设置为false 无论如何,事件都不会传递给任何父记录器 他们的可加性设置。
所以日志事件没有传递给根级别。 要登录到控制台,请添加
<AsyncLogger name="com.mypackage" level="info" includeLocation="true" additivity="false">
<AppenderRef ref="STDOUT"/>
</AsyncLogger>
答案 1 :(得分:1)
根据上面的回答,我这样做了:
<AsyncLogger name="com.mypackage" level="trace" includeLocation="true" additivity="false">
<AppenderRef ref="RollingRandomAccessFile"/>
</AsyncLogger>
<AsyncLogger name="org.springframework.jdbc" level="debug" includeLocation="true" additivity="false">
<AppenderRef ref="RollingRandomAccessFile"/>
</AsyncLogger>
<AsyncLogger name="com.mypackage" level="info" includeLocation="true" additivity="false">
<AppenderRef ref="STDOUT"/>
</AsyncLogger>
<AsyncLogger name="org.springframework.jdbc" level="info" includeLocation="true" additivity="false">
<AppenderRef ref="STDOUT"/>
</AsyncLogger>
但是现在,日志只会出现在控制台上。日志文件仍为空。
所以,我的最终解决方案是:
<AsyncLogger name="com.mypackage" level="trace" includeLocation="true" additivity="false">
<AppenderRef ref="RollingRandomAccessFile"/>
</AsyncLogger>
<AsyncLogger name="org.springframework.jdbc" level="debug" includeLocation="true" additivity="false">
<AppenderRef ref="RollingRandomAccessFile"/>
</AsyncLogger>
<AsyncLogger name="com.mypackage" level="info" includeLocation="true" additivity="false">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="RollingRandomAccessFile"/>
</AsyncLogger>
<AsyncLogger name="org.springframework.jdbc" level="info" includeLocation="true" additivity="false">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="RollingRandomAccessFile"/>
</AsyncLogger>
这按预期工作。