java log4j appender 问题 - 无法打印调试,错误级别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28022232/
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
log4j appender issue - unable to print debug, error levels
提问by kingsmasher1
I am using a log4j for logging.
我正在使用 log4j 进行日志记录。
This is how my log4j.properties looks like
这是我的 log4j.properties 的样子
# Root logger option
log4j.rootLogger=info, debug, error file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
#Redirect to Tomcat logs folder
#log4j.appender.file.File=${catalina.home}/logs/logging.log
log4j.appender.file.File=C:\Users\raj_sanpui\Desktop\Automation\test.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Each of my Java files constructor has this call:
我的每个 Java 文件构造函数都有这个调用:
public class IMGOperations {
private org.apache.log4j.Logger log;
private String hostname;
private String sysid;
private String dicomfilepath;
public IMGOperations(String hostname, String sysid, String dicomfilepath)
{
this.hostname=hostname;
this.sysid=sysid;
this.dicomfilepath=dicomfilepath;
PropertyConfigurator.configure(mainConfig.LOG4JCONFPATH);
log = Logger.getLogger(mainConfig.class);
}
I am getting this error on running my Java program:
我在运行 Java 程序时遇到此错误:
log4j:ERROR Could not find value for key log4j.appender.debug
log4j:ERROR Could not instantiate appender named "debug".
log4j:ERROR Could not find value for key log4j.appender.error file
log4j:ERROR Could not instantiate appender named "error file".
log4j:ERROR Could not find value for key log4j.appender.debug
I am basically a C/C++ folk, who knows Core Java, and pretty much a noob in this stuff. So please pardon me, if you find it too basic.
我基本上是一个 C/C++ 人,了解 Core Java,而且在这方面几乎是个菜鸟。所以请原谅我,如果你觉得它太基础了。
回答by ConMan
The problem is in your second line, it should be:
问题出在你的第二行,应该是:
# Root logger option
log4j.rootLogger=INFO, stdout, file
This means you will log at INFO level, and instantiate log4j appenders for stdout and files. Needless to say, INFO can be switched for any other logging level (TRACE, DEBUG, INFO, WARN, ERROR, FATAL).
这意味着您将在 INFO 级别登录,并为标准输出和文件实例化 log4j 附加程序。不用说,INFO 可以切换为任何其他日志级别(TRACE、DEBUG、INFO、WARN、ERROR、FATAL)。
EDIT:
编辑:
The log4j.rootLogger takes 2 or more arguments. The first argument, is the logging level, and any following are the names of log4j appenders. You are seeing the error you have presented in your question because there are no appenders by the names of "debug" or "error file", as described by the line:
log4j.rootLogger 需要 2 个或更多参数。第一个参数是日志级别,后面的任何参数都是 log4j 附加程序的名称。您看到您在问题中提出的错误,因为没有名称为“调试”或“错误文件”的附加程序,如以下行所述:
log4j:ERROR Could not find value for key log4j.appender.debug
log4j:ERROR Could not find value for key log4j.appender.error file
EDIT 2:
编辑2:
Given the below config file:
鉴于以下配置文件:
# Root logger option
log4j.rootLogger=info, debug, error file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
#Redirect to Tomcat logs folder
#log4j.appender.file.File=${catalina.home}/logs/logging.log
log4j.appender.file.File=C:\Users\raj_sanpui\Desktop\Automation\test.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Log4j expects appenders to be defined for "debug" and "error file", however, you have only defined an appender for "file". You have defined your appender like so:
Log4j 期望为“调试”和“错误文件”定义附加程序,但是,您只为“文件”定义了附加程序。你已经像这样定义了你的 appender:
log4j.rootLogger=info, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
To use an appender named "debug", you would need another similar line:
要使用名为“debug”的 appender,您需要另一行类似的行:
log4j.rootLogger=info, debug
log4j.appender.debug=org.apache.log4j.RollingFileAppender
There should be a one to one mapping between the appenders you declare in the log4j.rootLogger property, and your appender definitions.
您在 log4j.rootLogger 属性中声明的 appender 和您的 appender 定义之间应该存在一对一的映射。
It might help if you think of each of the properties like this:
如果您像这样考虑每个属性,它可能会有所帮助:
log4j.appender.file => create new file appender object
log4j.appender.file.File => set the file property of the file appender
log4j.appender.file.MaxFileSize => set the max file size of the file appender
log4j.appender.file.MaxBackupIndex => set the max backup index property of the file appender
log4j.appender.file.layout => set the layout of the file appender
etc...
回答by Sivakumar
You having problem in the rootLogger
, you should have only one logging level e.g DEBUG, INFO, ERROR etc..
in your case you having multiple logging levels and second thing you didn't declare file
appender in your rootLogger
but you used it in rest of the snippets.
您在 . 中遇到问题rootLogger
,您应该只有一个日志记录级别,例如,DEBUG, INFO, ERROR etc..
在您的情况下,您有多个日志记录级别,第二件事是您没有file
在您的文件中声明appender,rootLogger
但您在其余代码段中使用了它。
# Root logger option
log4j.rootLogger=info, debug, error file
# Root logger option
log4j.rootLogger=info, debug, error file
The above snippet should be like this log4j.rootLogger= INFO, file
上面的代码片段应该是这样的 log4j.rootLogger= INFO, file
Have a look at this link
看看这个链接