Java Log4J 输出到控制台和文件产生警告/错误 log4j:WARN File 选项未为 appender 设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18197187/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:12:41  来源:igfitidea点击:

Log4J out to console and file produces warnings/errors log4j:WARN File option not set for appender

javaeclipselog4j

提问by caffreyd

I'd like to integrate Log4J logging into my Java application, writing information out to a log file as well as displaying the info at the console. Unfortunately, I've had some trouble getting this off the ground. I'm working with Eclipse Juno on Win 7.

我想将 Log4J 日志集成到我的 Java 应用程序中,将信息写入日志文件并在控制台上显示信息。不幸的是,我在实现这一点时遇到了一些麻烦。我正在 Win 7 上使用 Eclipse Juno。

My log4j.properties file:

我的 log4j.properties 文件:

#configure logfile
log4j.appender.logfile = org.apache.log4j.RollingFileAppender
log4j.appedner.logfile.File = MyAppLog.log
log4j.appender.logfile.Append = true
log4j.appender.logfile.layout = org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern = %-5p %d [%t][%F:%L] : %m%n

#configure stdout
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %-5p %d [%t][%F:%L] : %m%n

log4j.rootLogger = debug, logfile, stdout

I belive the above follows the pattern of some other accepted answers on this topic.

我相信以上内容遵循了有关此主题的其他一些公认答案的模式。

I've specifed the location of this log4j.properties file wtih a VM argument:

我已经使用 VM 参数指定了这个 log4j.properties 文件的位置:

-Dlog4j.configuration=file:///${workspace_loc}\path\to\file\log4j.properties

I instantiate the logger in my java class:

我在我的 java 类中实例化记录器:

static Logger logger = Logger.getLogger(MyApp.class.getName());

log a string:

记录一个字符串:

logger.info("barfoo");

This configuration results in the following warnings/errors on launch:

此配置会在启动时导致以下警告/错误:

log4j:WARN File option not set for appender [logfile].
log4j:WARN Are you using FileAppender instead of ConsoleAppender?
log4j:ERROR No output stream or file set for the appender named [logfile]

It does not produce the log file. The console logging seems to be working just fine, however.

它不生成日志文件。然而,控制台日志似乎工作得很好。

采纳答案by Xynariz

This will sound stupid - you have a typo. You have log4j.appedner.logfile.Fileinstead of log4j.appender.logfile.File(note the 'n' and 'd' are switched). This is why it's complaining that you haven't set the File.

这听起来很愚蠢 - 你有一个错字。你有log4j.appedner.logfile.File而不是log4j.appender.logfile.File(注意'n'和'd'被切换了)。这就是为什么它抱怨你没有设置文件。