javaweb-serviceslog4jaxis

Please initialize the log4j system properly. While running web service


Maybe it looks silly to ask this but I am confused. I referred to Configuring Log4j property but it doesn't seem to help.

I have written a simple web service HelloWorld. And while running it I am getting the error something like this :

log4j:WARN No appenders could be found for logger (org.apache.axis.transport.http.AxisServlet). log4j:WARN Please initialize the log4j system properly.

I am not sure why its happening.

I am generating the web-service using Eclipse and deployed in Tomcat 6.0. I check on Axis Developer's Guide and according to them

log4j.configuration=log4j.properties Use this system property to specify the name of a Log4J configuration file. If not specified, the default configuration file is log4j.properties. A log4j.properties file is provided in axis.jar.

I didn't find log4j.properties in the axis.jar.

Any help on this?


Solution

  • Those messages are something tricky, enough so that people created this bug report to make it clearer.

    What's tricky about them is that the warnings are written if Log4j can't find its log4j.properties (or log4j.xml) file, but also if the file is fine and dandy but its contents are not complete from a configuration point of view.

    The following paragraph is taken from the log4j's troubleshooting guide:

    Logging output is written to a target by using an appender. If no appenders are attached to a category nor to any of its ancestors, you will get the following message when trying to log:

    log4j: No appenders could be found for category (some.category.name).
    log4j: Please initialize the log4j system properly.
    

    Log4j does not have a default logging target. It is the user's responsibility to ensure that all categories can inherit an appender. This can be easily achieved by attaching an appender to the root category.

    You can find info on how to configure the root logger (log4j.rootLogger) in the log4j documentation; basically, by adding something as simple as this at the beginning of the file:

    log4j.rootLogger=debug, stdout
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
    

    This should clear those WARN messages you get on startup. (Make sure you don't already have an appender named stdout. Also be careful of what level you give the root logger; debug will be very verbose and every library in your app will start writing stuff to the console.)

    As for the log4j.properties/log4j.xml file, I suggest you place this file in /WEB-INF/classes, as it is important to have it exposed for different tweaks (activating/deactivating logs, changing log levels, etc). You can have it inside a JAR in the classpath also (as you specified in your comment), but it will be enclosed in the archive (hopefully in the right place inside the archive) and won't be as easy to handle as if it were in /WEB-INF/classes.