简体   繁体   English

log4j不同级别的详细信息

[英]log4j different levels of details

I am using log4j 1.2 and I want to achieve the following behaviour: 我正在使用log4j 1.2,并且想要实现以下行为:

I want to be able to log from all classes in the package. 我希望能够从包中的所有类中进行登录。 The log should be written in a detailed version (including info and debug messages) to a file, but I want to receive a short version (errors and fatals) as String. 日志应以详细的版本(包括信息和调试消息)写入文件,但我希望以String的形式收到简短的版本(错误和致命信息)。

From the documentation I guessed that I probably have to define two different logger instances which are ancestors or each other and assign appenders to them. 从文档中,我猜想我可能必须定义两个不同的记录器实例,它们是祖先或彼此祖先,并为其分配附加器。 But I neither know the details nor do I know if this is the intended way to achieve this behaviour. 但是我既不知道细节,也不知道这是否是实现这种行为的预期方式。

Can somebody please give me a hint? 有人可以给我提示吗?

您应该能够使用相同的记录器,并将过滤器应用于处理程序,即将控制台处理程序的日志级别(我假设您用“ as String”表示)设置为ERROR

Define only one logger which you affect two file appenders: one with level error and the other with the debug level. 仅定义一个会影响两个文件追加器的记录器:一个具有级别错误,另一个具有调试级别。 The level filtering will be done in the appender. 级别过滤将在附加程序中完成。

<appender name="DebugAppender" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="logs/debug.log" />
    <param name="MaxBackupIndex" value="5" />
    <param name="MaxFileSize" value="5MB" />
    <param name="threshold" value="debug" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d %t %-5p %c - %m%n" />
    </layout>
</appender>
<appender name="ErrorAppender" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="logs/error.log" />
    <param name="MaxBackupIndex" value="5" />
    <param name="MaxFileSize" value="5MB" />
    <param name="threshold" value="error" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d %t %-5p %c - %m%n" />
    </layout>
</appender>
<logger name="mylogger">
    <level value="debug" />
    <appender-ref ref="DebugAppender" />
    <appender-ref ref="ErrorAppender" />
</logger>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM