Results 1 to 4 of 4

Thread: Tomcat 7: Setting logging level for org.springframework libraries

  1. #1
    Join Date
    Aug 2011
    Posts
    3

    Question Tomcat 7: Setting logging level for org.springframework libraries

    On Tomcat 7, I am trying to get logging output from the Spring libraries I am using.

    In Tomcat's logging.properties I have:

    Code:
    .handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler
    
    java.util.logging.ConsoleHandler.level = ALL
    1catalina.org.apache.juli.FileHandler.level = ALL
    
    #this does not change the logging level...
    org.springframework.level = ALL
    The above code does not cause logging messages from Spring to be logged.

    However, if I also add:

    Code:
    # this does change the logging level...
    javax.faces.level = ALL
    com.sun.faces.level = ALL
    then I do get logging messages from JSF to be logged.

    Why does the logging not work with Spring and what should I do to make it work?

    UPDATE:

    I have noticed that when I remove log4j.jar from the classpath, I do get logging from Spring. But when I add log4j.jar to the classpath, the logging from Spring is gone. Can you explain why?

    BTW I do need log4j in my classpath, since one of the other jars I am using, refers to log4j explicitly in its code.
    Last edited by rapt; Apr 8th, 2012 at 03:35 PM.

  2. #2
    Join Date
    Mar 2012
    Location
    Gurgaon, India
    Posts
    49

    Default Include a custom log4j.xml or log4j.properties file with your application

    It will be better if you include a custom log4j.xml or log4j.properties file with your application rather than changing the Tomcat log configuration file. You can set the appropriate log levels in your config file and redirect the logs to a custom log file (or other log targets).

  3. #3
    Join Date
    Oct 2008
    Location
    Poland, Wrocław
    Posts
    429

    Default

    Hello

    See https://github.com/grgrzybek/tomcat-slf4j-logback for better alternative to using log4j and commons-logging in Tomcat (downloads: https://github.com/grgrzybek/tomcat-...back/downloads).

    Tomcat uses repackaged version of commons-logging which (when you've not added tomcat-juli-adapters.jar explicitely to TOMCAT/lib) uses "standard" java.util.Logging (may it rest in peace forever). To get log4j functionality you can add it to TOMCAT/lib (then you get global configuration) or to every WEB-INF/lib (then every WAR must contain their own log4j.xml).

    I encourage you to look at slf4j (as logging facade) and logback (as logging implementation). API is clean and implementation is not dealing with classloaders (like commons-logging and log4j).

    regards
    Grzegorz Grzybek

  4. #4
    Join Date
    Oct 2008
    Location
    Poland, Wrocław
    Posts
    429

    Default

    Hello again

    My first answer was not exactly about your problem, but if you use slf4j and logback, you can include in your WEB-INF/lib:
    • slf4j-api-1.6.4.jar
    • logback-core-1.0.1.jar
    • logback-classic-1.0.1.jar
    • jcl-over-slf4j-1.6.4.jar
    • log4j-over-slf4j-1.6.4.jar


    and proper logback.xml in your WEB-INF classes, for example:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
    
    	<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    		<encoder>
    			<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level (%F:%L\) [%logger{20}] : %msg%n</pattern>
    		</encoder>
    	</appender>
    
    	<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    		<file>${catalina.base}/logs/myapplication.log</file>
    		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    			<fileNamePattern>${catalina.base}/logs/myapplication-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
    			<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
    				<maxFileSize>100MB</maxFileSize>
    			</timeBasedFileNamingAndTriggeringPolicy>
    		</rollingPolicy>
    		<encoder>
    			<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{user}:%X{ctx}] %-5level {%thread} [%logger{30}] : %msg%n</pattern>
    		</encoder>
    	</appender>
    
    	<logger name="org.springframework" level="INFO" />
    	<logger name="com.example" level="TRACE" />
    
    	<root level="INFO">
    		<appender-ref ref="CONSOLE" />
    		<appender-ref ref="FILE" />
    	</root>
    
    </configuration>
    and you'll get nice logging configuration.

    regards
    Grzegorz Grzybek

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •