tags:

views:

723

answers:

4

I'm trying to permit debug logging per a particular class using Log4j, and I've got the following:

log4j.rootLogger=stdout, daily

log4j.logger.com.mycompany.myapplication.mymodule=DEBUG

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{h:mm:ssa} %5p (%F:%L) - %m%n
log4j.appender.stdout.threshold=warn

log4j.appender.daily=org.apache.log4j.DailyRollingFileAppender
log4j.appender.daily.layout=org.apache.log4j.PatternLayout
log4j.appender.daily.layout.ConversionPattern=%d{h:mm:ssa} %5p (%F:%L) - %m%n
log4j.appender.daily.File=/some/file/path/stuff
log4j.appender.daily.DatePattern=MMdd'.log'
log4j.appender.daily.threshold=info

If this is the WEB-INF/classes/log4j.properties file as part of tomcat should debug messages from mycompany.myapplication.mymodule be seen or do the entries later in the file override it? (or am I changing the logging level per class completely wrong?)

Secondly, if a log4j.properties file is included in a jar file, do my settings in Tomcat override those?

A: 

Yes, you seem to be configuring your individual loggers correctly.

No, they will not be seen because you are declaring thresholds on both of your appenders which are above DEBUG:

log4j.appender.stdout.threshold=warn
log4j.appender.daily.threshold=info

As for "Secondly, if a log4j.properties file is included in a jar file, do my settings in Tomcat override those?", I'm not sure what you mean by this. log4j attempts to load a log4j.xml or log4j.properties from the classpath, and will use whichever it finds first. IIRC, entries in the shared lib folder of Tomcat supercede the classpath of each application. If you have a log4j configuration in both a jar in WEB-INF/lib and in a plaintext file in WEB-INF/classes, then you are essentially flipping a coin at runtime as to which will be used.

matt b
+1  A: 

For your first question, I would say that log4j.appender.daily.threshold=info is a problem. I might not pick any DEBUG message.

Normally, we don't give a threshold to an appender, levels are configured for loggers.


I suggest you use the log4j.xml instead of the log4j.properties. In addition to the useful validation, it adds some interesting features or default values. I don't recall exactly which though...

KLE
A: 
  1. I think you have to give mymodule an appender, as in

    log4j.logger.com.mycompany.myapplication.mymodule=DEBUG, Daily log4j.additivity.com.mycompany.myapplication.mymodule=false

    Also, if you set the additivity to false, you make sure messages get logged only by the appender you specify.

  2. The 'root' of a jar is normally not included in the CLASSPATH, so a log4j.properties within a jar should be invisible to the JVM.

wallenborn
+2  A: 

You tell your class to log at level DEBUG but tell the appenders to ignore anything below WARN and INFO, so you won't see the log messages.

As for the order in which the log4j.properties will be discovered:

  1. WEB-INF/classes
  2. Any JAR in WEB-INF/lib
  3. common/classes (in the tomcat directory)
  4. Any JAR that you put into common/endorsed
  5. Any JAR that you put into common/lib
  6. shared/classes
  7. Any JAR that you put into shared/lib

The discovery will stop with the first file found.

Aaron Digulla