简体   繁体   中英

Configuring logging for a web application

I have a Spring webapp packaged as a WAR file being deployed to Tomcat.

Catalina.out shows

log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
gen 29, 2013 11:37:04 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error listenerStart

That's probably a configuration error in either Spring or Hibernate. But in order to find the cause of listenerStart error I think it's necessary to enable log4j. Googling around I found that the log4j warning is caused by missing log4j configuration, and that's certain because I haven't configured it yet.

How do I configure log4j (where do I put the configuration file and what do I type in) in order to log errors to catalina.out, which is the most reasonable place for development logging?

Create a log4j.xml file like this one

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender class="org.apache.log4j.ConsoleAppender" name="CONSOLE">
        <param value="INFO" name="Threshold"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param value="%d{DATE}\t%m%n" name="ConversionPattern"/>
        </layout>
    </appender>
    <!-- Here you create additional logger for your application, ie:
    <logger name="package.xxx">
         <level value="info" />
    </logger>
    -->
    <logger name="rootLogger">
        <level value="info"/>
        <appender-ref ref="CONSOLE"/>
    </logger>
</log4j:configuration>

The basic explanation of this configuration file is

  1. appender : you can define multiple appender, the one I set up for you is called CONSOLE and is a ConsoleAppender , which means it writes on the standard output (in a tomcat environment, catalina.out ).
  2. layout is the definition of the layout of your log file, take a look to PatternLayout to understand the meaning of what I wrote and to modify to your needs
  3. logger , where you define one or more logger for your application. Here I simply define a rootLogger with default level info and which is going to use the CONSOLE appender.

Place this file wherever in your classpath and you will have your log4j configured. For a full tutorial, you can refer to http://logging.apache.org/log4j/1.2/manual.html

EDIT I read over your question again, I suggest you to add a second logger to log spring stuff like

<logger name="org.springframework">
    <level value="info"/>
</logger>

where you just set the level to the fine grained you desire for Spring message. The full tutorial will explain levels quite clearly, but in short the higher the level the less verbose is going to be your log.

Simplest approach ( source )

In src/main/webapp/WEB-INF/classes (considering a Maven layout) or WebContent/WEB-INF/classes (considering a classic Eclipse layout) create a logging.properties file with following content:

org.apache.catalina.core.ContainerBase.[Catalina].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].handlers = java.util.logging.ConsoleHandler

Then redeploy and get your desired BOOM on catalina.out

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM