java – 无法实例化Log4j appender

前端之家收集整理的这篇文章主要介绍了java – 无法实例化Log4j appender前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在关注Web应用程序中Log4j日志记录的教程.

这是我的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.

不知道如何解决这个问题,以及为什么会出现这些错误,因为我按照教程开始了.

解决方法

您的文件中有两个错误.

> log4j.rootLogger = INFO,demoappender中有一个空的appender.此行的值被拆分,因此存在空元素.第一个元素是关卡.
> 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 – Short introduction to log4j中查看更多.

原文链接:https://www.f2er.com/java/125471.html

猜你在找的Java相关文章