7

我正在关注有关 Log4j 登录 Web 应用程序的教程。

这是我的log4j.properties文件:

#log to console
log4j.rootLogger=INFO, , demoappender
log4j.appender.demoappender=org.apache.log4j.ConsoleAppender
log4j.appender.demoappender=System.out
log4j.appender.demoappender.layout=org.apache.log4j.PatternLayout
log4j.appender.demoappender.layout.ConversionPattern=%d{ABSOLUTE} %5p %c:%L - %m%n

我正在使用 init() 方法实例化记录器:

private Logger logger = null;

public void init(ServletConfig config) throws ServletException {
    logger = Logger.getRootLogger();
}

这是我尝试记录一些示例消息的地方:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    logger.info("logging INFO message");
    logger.error("logging ERROR message");
    logger.warn("loggging WARNING message");
    logger.fatal("logging FATAL message");


    response.setContentType("text/html");

当我在 Tomcat 上运行 servlet 时,控制台上出现此错误:

log4j:ERROR Could not find value for key log4j.appender.
log4j:ERROR Could not instantiate appender named "".
log4j:ERROR Could not instantiate class [System.out].
java.lang.ClassNotFoundException: System.out
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at org.apache.log4j.helpers.Loader.loadClass(Loader.java:198)
at org.apache.log4j.helpers.OptionConverter.instantiateByClassName(OptionConverter.java:327)
at org.apache.log4j.helpers.OptionConverter.instantiateByKey(OptionConverter.java:124)
at org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigurator.java:785)
at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:768)
at org.apache.log4j.PropertyConfigurator.configureRootCategory(PropertyConfigurator.java:648)
at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:514)
at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:580)
at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:526)
at org.apache.log4j.LogManager.<clinit>(LogManager.java:127)
at org.apache.log4j.Logger.getRootLogger(Logger.java:135)
at Log4jDemoServlet.init(Log4jDemoServlet.java:22)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1189)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1103)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:813)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
log4j:ERROR Could not instantiate appender named "demoappender".
log4j:WARN No appenders could be found for logger (root).
log4j:WARN Please initialize the log4j system properly.

不知道如何解决这个问题,以及为什么会出现这些错误,因为我按照教程进行了操作。

4

1 回答 1

15

您的文件中有两个错误。

  1. 中有一个的附加程序log4j.rootLogger=INFO, , demoappender。这条线的值被分割,所以有一个空元素。第一个元素是级别。

  2. appender 的类必须实现接口org.apache.log4j.Appender并且System.out不实现该接口。

您需要对文件进行几处更改。

# Set root logger level to INFO and its only appender to demoappender
log4j.rootLogger=INFO, demoappender

# demoappender is set to be a ConsoleAppender.
log4j.appender.demoappender=org.apache.log4j.ConsoleAppender
# log4j.appender.demoappender=System.out                    <-- Remove this line

# demoappender uses PatternLayout.
log4j.appender.demoappender.layout=org.apache.log4j.PatternLayout
log4j.appender.demoappender.layout.ConversionPattern=%d{ABSOLUTE} %5p %c:%L - %m%n

在Apache log4j 1.2 - log4j 简介中查看更多信息。

于 2013-08-16T23:38:04.143 回答