我想将日志文件发送到我的应用程序 我为此尝试了Appach log4j。在Java代码中,我说:
Logger log = Logger.getLogger(uploadfile.class);
log.debug("Start");
我用:
创建文件src\log4j.properties
# ***** Set root logger level to DEBUG and its only appender to A.
log4j.rootLogger=DEBUG, R, A
log4j.rootLogger=INFO, A
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.MaxFileSize=100mb
log4j.appender.R.MaxBackupIndex=1000
log4j.appender.R.File=c:/uploaded_files/server.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
# ***** A is set to be a ConsoleAppender.
log4j.appender.A=org.apache.log4j.ConsoleAppender
# ***** A uses PatternLayout.
log4j.appender.A.Threshold=INFO
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.logger.org.hibernate.type.BasicTypeRegistry=INFO
log4j.logger.com.opensymphony.xwork2.config.providers.XmlConfigurationProvider=INFO
我复制了这个there。我得到错误:
13.02.2013 9:03:34 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [uploadfile] in context with path [/gis-mrsk-portlet] threw exception [Servlet execution threw an exception] with root cause
java.lang.NoClassDefFoundError: Could not initialize class org.apache.log4j.LogManager
at org.apache.log4j.Logger.getLogger(Logger.java:117)
at test.uploadfile.doPost(uploadfile.java:35)
我做错了什么?以及如何为简单配置log4j?
更新
如果我使用此配置,我会获取日志文件,但它是空的:
Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=INFO, CONSOLE, LOGFILE
Set the enterprise logger priority to FATAL
log4j.logger.org.apache.axis2.enterprise=FATAL
log4j.logger.de.hunsicker.jalopy.io=FATAL
log4j.logger.httpclient.wire.header=FATAL
log4j.logger.org.apache.commons.httpclient=FATAL
CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%p] %m%n
LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=C:/uploaded_files/LogFile.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
答案 0 :(得分:1)
看起来没错..你在路径中正确设置了log4j Lib吗?
答案 1 :(得分:1)
问题不在于你的配置,问题是 cat4j对catalina的不存在的支持它并不像它应该那样容易一起工作。要使log4j与catalina一起使用,请遵循http://tomcat.apache.org/tomcat-6.0-doc/logging.html#Using_Log4j提供的文档。
编辑
总结我给出的链接:
[...]将log4j.jar和log4j.properties放入Web应用程序的WEB-INF / lib和WEB-INF / classes中。
下载或构建tomcat-juli.jar和tomcat-juli-adapters.jar,它们可用作Tomcat的“extras”组件。有关详细信息,请参阅其他组件http://tomcat.apache.org/tomcat-6.0-doc/extras.html
按照这些步骤后,您的示例应该已经有效。
/ EDIT 的
如果您希望在日志记录中使用简单部分,我建议使用对基于tomcat的应用程序的本机servlet支持附带的logback。如果您想进一步记录日志:clusterlog.net(完全披露我拥有该公司的一部分)
答案 2 :(得分:0)
java.lang.NoClassDefFoundError
看起来你的Apache不知道org.apache.log4j.LogManager
的定义。你有没有把所需的jar放在Apache classpath
?