如何使用同一记录器将不同级别的日志记录到控制台+日志文件? [英] How to use the same logger to log different levels to console + logfile?
问题描述
我有一个log4j记录器,该记录器当前将日志写到控制台和文件中,效果很好.
I have a log4j logger that currently writes the log both to console and to a file, which works fine.
稍后,我想将其配置为将INFO + ERROR记录到日志文件中,但仅在控制台上显示ERROR.要实现此目标,我需要更改什么?
Later I'd like to configure it to log INFO + ERROR to the logfile, but only show ERROR on console. What would I have to change to achieve this?
log4j.rootLogger=INFO, console, MyFileAppender
log4j.logger.org.apache.cxf=INFO, console
log4j.logger.org.apache.cxf.interceptor.LoggingInInterceptor=INFO, console
log4j.logger.org.apache.cxf.interceptor.LoggingOutInterceptor=INFO, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.MyFileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.MyFileAppender.Append=true
log4j.appender.MyFileAppender.File=c:/logs.log
此外,我想阻止CXF XML请求记录在文件中.但是我希望它们仍然显示在控制台中.怎么样?
Further, I'd like to prevent the CXF XML requests to be logged in the file. But I want them still to be shown in the console. How?
推荐答案
基于Appender的配置
必须分别完成每个附加程序的日志级别的配置,除非它与根级别的配置相同.下面的示例log4.properties
文件配置为将INFO
以上的文件登录到控制台,但是仅ERROR
以上的文件登录到该文件.
Appender based configuration
Configuring the log levels per each appender has to be done separately unless it is same as the root level configuration. Below sample log4.properties
file is configured to log INFO
and above into the console, but only ERROR
and above into the file.
log4j.appender.[appender-name].Threshold=[Level]
查看以下示例的最后一行(来自"如何将log4j与Java项目集成).
Look at the last line of the below example (from "How to integrate log4j with your Java project").
# root level configurations
log4j.rootLogger=INFO,console,file
# configuration for console outputs
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
# configuration for file output (into a file named messages.log)
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=messages.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
# threshold for file output
log4j.appender.file.Threshold=ERROR
基于包的日志级别
以下任何一种方法都可以提供帮助.
Package based log levels
Any of the followings will help.
log4j.logger.[package]=[Level]
log4j.logger.[package]=[Level], [Appender]
例如:
log4j.logger.org.apache.cxf=INFO, console
这篇关于如何使用同一记录器将不同级别的日志记录到控制台+日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!