简体   繁体   中英

log4j different levels of details

I am using log4j 1.2 and I want to achieve the following behaviour:

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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