如何根据邮件的日志记录级别设置log4j以使用两个单独的模式?
例如,现在我使用适用于所有级别的以下内容:
log4j.appender.stdout.layout.ConversionPattern=%-5p %d{HH\:mm\:ss} - %m%n
我想要做的是对 WARN及以上的级别使用不同的模式。如:
%-5p %d{HH\:mm\:ss} - [%L:%C] %m%n
输出发生警告或错误的行号和类。在出现警告或错误的情况下,提取呼叫者信息的额外费用是值得的。
这可行吗?
这是我的完整log4j.properties文件:
log4j.rootLogger=WARN, stdout1, stdout2
log4j.category.my.package=DEBUG
log4j.appender.stdout1=org.apache.log4j.ConsoleAppender
log4j.appender.stdout1.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout1.layout.ConversionPattern=%-5p %d{HH\:mm\:ss} - %m%n
log4j.appender.stdout2=org.apache.log4j.ConsoleAppender
log4j.appender.stdout2.threshold=WARN
log4j.appender.stdout2.target=System.err
log4j.appender.stdout2.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout2.layout.ConversionPattern=%-5p %d{HH\:mm\:ss} - [%t] [%L:%C] %m%n
修改
threshold
看起来是我需要记录WARN
及更高等级的所有级别。是否有类似的属性会将另一个appender配置为记录INFO
和更低的所有级别?
答案 0 :(得分:1)
这里我使用的属性:
log4j.rootLogger=WARN, CONSOLE, FICLOG
####### CONSOLE is set to be a ConsoleAppender.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
####### CONSOLE uses PatternLayout.
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-5r %-5p[%C{1}.%M]:%m%n
####### FICLOG is set to be a FileAppender à la racine du projet
log4j.appender.FICLOG=org.apache.log4j.RollingFileAppender
####### FIC uses PatternLayout.
####### ex:log4j.appender.dest2.File=${java.home}/log4j.log
log4j.appender.FICLOG.File=Appli_Info.log
log4j.appender.FICLOG.threshold=WARN
log4j.appender.FICLOG.MaxFileSize=5MB
log4j.appender.FICLOG.MaxBackupIndex=8
log4j.appender.FICLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.FICLOG.layout.ConversionPattern=%d %-8r [%t] %-5p [%C{1}.%M] %x - %m%n
####### log4j.appender.FICLOG.layout.ConversionPattern=%d{HH:mm:ss} %-8r [%t] %-5p [%C{1}.%M] %x - %m%n
HTH
答案 1 :(得分:0)
在这里找到答案:Logging error to stderr and debug, info to stdout with log4j
我最终不得不使用xml文件而不是属性文件来获取我想要的结果。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
<!-- STDERR - WARN and above -->
<appender name="stderr" class="org.apache.log4j.ConsoleAppender">
<param name="threshold" value="warn" />
<param name="target" value="System.err"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{HH\:mm\:ss} - [%t][%L:%C] %m%n" />
</layout>
</appender>
<!-- STDINFO - DEBUG & INFO -->
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<param name="threshold" value="debug" />
<param name="target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{HH\:mm\:ss} - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="debug" />
<param name="LevelMax" value="info" />
</filter>
</appender>
<category name="my.project.package">
<!-- Display DEBUG and above for all log messages in My Project -->
<param name="level" value="debug" />
</category>
<root>
<!-- Global Setting - Display only WARN messages
This surpresses other libraries from display DEBUG & INFO messages
But the Category setting above for your project will override this
and display DEBUG & INFO messages (using STDOUT) as well as
WARN and above (using STDERR) -->
<priority value="warn"></priority>
<appender-ref ref="stderr" />
<appender-ref ref="stdout" />
</root>
</log4j:configuration>