[英]Why two Loggers do not split messages according to their levels in Log4j?
I want to log all logger levels to a file (FATAL-> ALL) and simultaneousy show ERROR level and higher in the console.我想将所有记录器级别记录到一个文件(FATAL-> ALL)并同时在控制台中显示 ERROR 级别和更高级别。 I'm using root logger to log all levels to a file and separate logger to log ERROR level to a console.
我正在使用根记录器将所有级别记录到文件中,并使用单独的记录器将错误级别记录到控制台。 My XML configuration file looks as follows:
我的 XML 配置文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="%d{HH:mm:ss.SSS} [%t] %highlight{%level}{FATAL=bg_red, ERROR=red, WARN=yellow, INFO=blue} %logger{36} - %msg%n" disableAnsi="false"/>
</Console>
<File name="File" fileName="output.log" bufferedIO="true" >
<PatternLayout
pattern="%d{HH:mm:ss.SSS} [%t] %highlight{%level}{FATAL=bg_red, ERROR=red, WARN=yellow, INFO=blue} %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="ALL">
<AppenderRef ref="File"/>
</Root>
<Logger name="com.danielk" level="ERROR">
<AppenderRef ref="Console"/>
</Logger>
</Loggers>
</Configuration>
I expected to have all levels of logging in the file and only "Error" and above level in the console but I have only "Error" levels on both (console and file):我希望文件中包含所有级别的日志记录,并且控制台中只有“错误”和更高级别,但我在两个(控制台和文件)上都只有“错误”级别:
23:19:54.422 [main] ERROR com.danielk.Example - error error error from Example 23:19:54.434 [main] ERROR com.danielk.Main - ERROR ERROR ERROR FROM MAIN
23:19:54.422 [main] ERROR com.danilk.Example - 示例 23:19:54.434 [main] ERROR com.danielk.Main - 来自 M 的错误错误错误
Messages themselves are correcty build because when I set both loggers (Root and additional) to "ALL" level I can see all kind of messages (not only "Error" level).消息本身是正确构建的,因为当我将两个记录器(根和附加)设置为“ALL”级别时,我可以看到所有类型的消息(不仅仅是“错误”级别)。
What should I do to split levels:我应该怎么做才能拆分级别:
You can use ThresholdFilter
on console appender to filter out log events that have ERROR
or more specific level ( https://logging.apache.org/log4j/2.x/manual/filters.html#ThresholdFilter ).您可以在控制台附加程序上使用
ThresholdFilter
过滤掉具有ERROR
或更特定级别( https://logging.apache.org/log4j/2.x/manual/filters.html#ThresholdFilter )的日志事件。
Entire configuration eg:整个配置例如:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="%d{HH:mm:ss.SSS} [%t] %highlight{%level}{FATAL=bg_red, ERROR=red, WARN=yellow, INFO=blue} %logger{36} - %msg%n"
disableAnsi="false"/>
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
</Console>
<File name="File" fileName="output.log" bufferedIO="true" append="false">
<PatternLayout
pattern="%d{HH:mm:ss.SSS} [%t] %highlight{%level}{FATAL=bg_red, ERROR=red, WARN=yellow, INFO=blue} %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="File"/>
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
PS When you specify: PS当你指定:
<Logger name="com.danielk" level="ERROR">
<AppenderRef ref="Console"/>
</Logger>
then log level is set to ERROR
for all sub-packages and classes inside com.danielk
(unless you configure more specific logger, say com.danielk.db.MyClass
with a different level).然后将
com.danielk
中的所有子包和类的日志级别设置为ERROR
(除非您配置更具体的记录器,例如com.danielk.db.MyClass
具有不同的级别)。 That's why only errors appeared in the log file.这就是日志文件中只出现错误的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.