如何让 log4j 也写入控制台 [英] how to make log4j to write to the console as well
问题描述
有什么方法可以告诉 log4j 将其日志写入文件和控制台吗?谢谢有我的属性:
Is there any way to tell to log4j to write its log to the file and to the console? thanks there are my properties:
log4j.rootLogger=DEBUG,console,R
log4j.rootLogger=INFO, FILE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p - %m%n
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=log4j.log
log4j.appender.FILE.MaxFileSize=512KB
log4j.appender.FILE.MaxBackupIndex=3
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p - %m%n
推荐答案
您的根记录器定义有点混乱.请参阅 log4j 文档.
Your root logger definition is a bit confused. See the log4j documentation.
这是一个标准的 Java 属性文件,这意味着行被视为键=值对.您的第二个 log4j.rootLogger
行覆盖了第一行,这解释了为什么您在 console
附加程序上看不到任何内容.
This is a standard Java properties file, which means that lines are treated as key=value pairs. Your second log4j.rootLogger
line is overwriting the first, which explains why you aren't seeing anything on the console
appender.
您需要将两个 rootLogger
定义合并为一个.看起来您正在尝试将 DEBUG
消息发送到控制台,并将 INFO
消息发送到文件.根记录器只能有 一个 级别,因此您需要更改配置以使 appender 具有适当的级别.
You need to merge your two rootLogger
definitions into one. It looks like you're trying to have DEBUG
messages go to the console and INFO
messages to the file. The root logger can only have one level, so you need to change your configuration so that the appenders have appropriate levels.
虽然我还没有验证这是否正确,但我猜它看起来像这样:
While I haven't verified that this is correct, I'd guess it'll look something like this:
log4j.rootLogger=DEBUG,console,file
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.file=org.apache.log4j.RollingFileAppender
请注意,您在大小写方面也有错误 - 您在一个地方使用控制台小写字母,而在另一个地方使用大写字母.
Note that you also have an error in casing - you have console lowercase in one place and in CAPS in another.
这篇关于如何让 log4j 也写入控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!