Java 如何让 log4j 也写入控制台
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3382985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to make log4j to write to the console as well
提问by fatnjazzy
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 将其日志写入文件和控制台吗?谢谢,有我的属性:
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
采纳答案by Steven Schlansker
Your root logger definition is a bit confused. See the log4j documentation.
您的根记录器定义有点混乱。请参阅log4j 文档。
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.
这是一个标准的 Java 属性文件,这意味着行被视为键=值对。您的第二log4j.rootLogger
行覆盖了第一行,这解释了为什么您在console
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 onelevel, so you need to change your configuration so that the appenders have appropriate levels.
您需要将两个rootLogger
定义合二为一。看起来您正在尝试将DEBUG
消息发送到控制台并将INFO
消息发送到文件。根记录器只能有一个级别,因此您需要更改配置以使附加程序具有适当的级别。
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.
请注意,您在大小写方面也有错误 - 您在一个地方使用控制台小写字母,而在另一个地方使用大写字母。
回答by ramit girdhar
Your log4j File should look something like below read comments.
您的 log4j 文件应该类似于下面的阅读评论。
# Define the types of logger and level of logging
log4j.rootLogger = DEBUG,console, FILE
# Define the File appender
log4j.appender.FILE=org.apache.log4j.FileAppender
# Define Console Appender
log4j.appender.console=org.apache.log4j.ConsoleAppender
# Define the layout for console appender. If you do not
# define it, you will get an error
log4j.appender.console.layout=org.apache.log4j.PatternLayout
# Set the name of the file
log4j.appender.FILE.File=log.out
# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true
# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug
# Set the append to false, overwrite
log4j.appender.FILE.Append=false
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
回答by Brijendra Verma
This works well for console in debug mode
这适用于调试模式下的控制台
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=DEBUG
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
回答by Pratap Kumar Panda
Write the root logger as below for logging on both console and FILE
编写如下的根记录器以同时登录控制台和文件
log4j.rootLogger=ERROR,console,FILE
log4j.rootLogger=错误,控制台,文件
And write the respective definitions like Target, Layout, and ConversionPattern (MaxFileSize for file etc).
并编写各自的定义,如 Target、Layout 和 ConversionPattern(文件的 MaxFileSize 等)。