I'm following a tutorial on Log4j logging in web applications.
This is my log4j.properties
file:
#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
I am instantiating the logger using an init() method:
private Logger logger = null;
public void init(ServletConfig config) throws ServletException {
logger = Logger.getRootLogger();
}
This is where I try to log some sample messages:
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");
When I run the servlet on Tomcat, I get this error on the console:
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.
Not sure how to resolve this, and why these errors are coming up, since I followed the tutorial to a tee.
Apache log4j provides Appender objects which are primarily responsible for printing logging messages to different destinations such as consoles, files, sockets, NT event logs, etc. Each Appender object has different properties associated with it, and these properties indicate the behavior of that object.
The rootlogger is always the logger configured in the log4j. properties file, so every child logger used in the application inherits the configuration of the rootlogger . The logging levels are (from smaller to greater) : ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF .
There are two errors in your file.
There is an empty appender in log4j.rootLogger=INFO, , demoappender
. The value of this line is split by ,
so there is an empty element. The first element is the level.
The class of the appender must implement the interface org.apache.log4j.Appender
and System.out
does not implement that interface.
You need to make several changes to your file.
# 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
See more in Apache log4j 1.2 - Short introduction to log4j.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With