log4j2无法创建INFO级别的日志



我的spring引导应用程序中有Log4j2,并希望将事件记录到2个日志文件中。UserService不能创建INFO级别的日志,但是可以创建WARN级别的日志。有人能给点建议吗?

Log4j2.xml

<Configuration>
<Appenders>
<RollingFile name="matchFile"
fileName="${log-path}/match.log"
immediateFlush="true">
<Filters>
<ThresholdFilter level="trace" onMAtch="deny" onMismatch="neutral" />
<ThresholdFilter level="debug" onMAtch="deny" onMismatch="neutral" />
<ThresholdFilter level="info" onMAtch="accept" onMismatch="deny" />
<ThresholdFilter level="warn" onMAtch="deny" onMismatch="neutral" />
<ThresholdFilter level="error" onMAtch="deny" onMismatch="neutral" />
<ThresholdFilter level="fatal" onMAtch="deny" onMismatch="neutral" />
</Filters>
</RollingFile>
<RollingFile name="confirmFile"
fileName="${log-path}/confirm.log"
immediateFlush="true">
<Filters>
<ThresholdFilter level="trace" onMAtch="deny" onMismatch="neutral" />
<ThresholdFilter level="debug" onMAtch="deny" onMismatch="neutral" />
<ThresholdFilter level="info" onMAtch="deny" onMismatch="neutral" />
<ThresholdFilter level="warn" onMAtch="accept" onMismatch="deny" />
<ThresholdFilter level="error" onMAtch="deny" onMismatch="neutral" />
<ThresholdFilter level="fatal" onMAtch="deny" onMismatch="neutral" />
</Filters>
</RollingFile>
</Appenders>
</Configuration>

UserService.java

public class UserService{
Logger matchLogger = LogManager.getLogger("matchFile");
Logger confirmLogger = LogManager.getLogger("confirmFile");

public void log(){
matchLogger.info("print info"); //not able to log this message
confirmLogger.warn("print warn"); //able to log this message
}
}

您缺少配置中的<Loggers>部分,您在其中声明要在代码中使用的日志记录器名称并将追加器附加到它们。此外,ThresholdFilter实际上声明了一个级别阈值,因此不需要为每个级别指定一个过滤器。

你的配置应该是这样的:

<Configuration>
<Appenders>
<RollingFile name="matchFile"
fileName="${log-pathmatchFile}/match.log"
immediateFlush="true">
<Filters>
<!-- deny warn, error and fatal messages -->
<ThresholdFilter level="warn" onMatch="deny" onMismatch="neutral" />
<!-- accept info, warn, error, fatal and deny debug, trace -->
<ThresholdFilter level="info" onMatch="accept" onMismatch="deny" />
</Filters>
</RollingFile>
<RollingFile name="confirmFile"
fileName="${log-path}/confirm.log"
immediateFlush="true">
<Filters>
<!-- deny error and fatal messages -->
<ThresholdFilter level="error" onMatch="deny" onMismatch="neutral" />
<!-- accept warn, error, fatal and deny info, debug, trace -->
<ThresholdFilter level="warn" onMatch="accept" onMismatch="deny" />
</Filters>
</RollingFile>
</Appenders>

<Loggers>
<!-- probably declare the root logger level and appenders:
<Root level="...">
<AppenderRef ref="..." />
</Root>
-->
<Logger name="matchFile" level="info">
<AppenderRef ref="matchFile" />
</Logger>
<Logger name="confirmFile" level="warn">
<AppenderRef ref="confirmFile" />
</Logger>
</Loggers>
</Configuration>

最新更新