简体   繁体   中英

Log4j doesn't log INFO Level

I have the following log4j.properties file, for an application deployed in WebSphere Portal:

log4j.rootLogger=DEBUG, InfoAppender, DebugAppender

log4j.appender.InfoAppender=org.apache.log4j.RollingFileAppender
log4j.appender.InfoAppender.Threshold=INFO
log4j.appender.InfoAppender.File=C:/info.log
log4j.appender.InfoAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.InfoAppender.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.DebugAppender=org.apache.log4j.RollingFileAppender
log4j.appender.DebugAppender.Threshold=DEBUG
log4j.appender.DebugAppender.File=C:/debug.log
log4j.appender.DebugAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.DebugAppender.layout.ConversionPattern=%d %p [%c] - %m%n

When I code, I define the logger at class level:

private static Logger logger = Logger.getLogger(IWannaLogThis.class);

And I log INFO messages with this:

logger.info(theObjectToLog);

When I deploy my application, the debug.log file gets everything I log with logger.debug() but ignores everything I write with logger.info() . On the other side, the info.log file keeps empty.

The weirdest thing is that in debug.log and info.log appears some INFO and DEBUG messages made by some JARS (like Hibernate Validator) I had in the classpath, but just ignores everything I try to log in my code.

Any ideas?

This is most likely a classloading-related problem. WebSphere Portal uses Log4J internally, so I'm guessing that you end up using WebSphere Portal's provided Log4J JAR file as well as its own Log4J properties.

You can verify that by adding the following to the JVM arguments of the server instance:

-Dlog4j.debug=true

And then inspect the SystemOut.log file. Log4J will spit out lots of tracing information about the configuration file(s) it reads.

The best way to avoid this is to do the following:

  1. Bundle the Log4J JAR file with your application.
  2. Associate a Shared Library with the server. In that Shared Library, place your Log4J configuration file.

As an alternative to step 2, you can bundle your Log4J configuration file with the application itself, however that would carry its own drawbacks (for example, having to repackage your application whenever you perform a Log4J configuration change).

Another common problem is that the JARs you have in your classpath also use log4j and also have their own appenders set. So depending on the settings that they use, and the packages that your classes reside in, this may lead to the problem you describe.

So:

  • Make sure that your package names are unique and not used by any of the third party libraries.
  • Check the log4j settings in all libraries in your classpath. They should not contain general settings which override yours.
  • Make sure your loggers use your log4j.properties (you can be sure if changes you make in your file affect your loggers as expected).
  • If you can, make sure that your log4j stuff loads last, in case any of the third party libs reset the configuration. They shouldn't, but who can stop them.

Normally, it should be one of these things. Post more explicit example if it doesn't work.

Good luck!

What I have done in the past is set specific logs for the classes I want to log. It sounds like you can try setting your root logger to INFO and see if that gets you the messages you want. Here's a little bit of my log4j property file. I set a logger for each class and assign it to my "data" appender, which defines the log layout. In the loggers I specify specific classes I want to log and set their Log level individually. Any class that logs that is not defined in the Loggers I have use the default log level for the rootCategory.

log4j.rootCategory=INFO, rollingFile, stdout

#GetData Loggers
log4j.logger.com.myapp.data=INFO, data
log4j.logger.com.myapp.data.SybaseConnection=DEBUG, data
log4j.logger.com.myapp.data.GetData=ERROR, data


# data appender
log4j.appender.data=org.apache.log4j.RollingFileAppender
log4j.appender.data.layout=org.apache.log4j.PatternLayout
log4j.appender.data.File=c\:\\Program Files\\MyApp\\logs\\MyApp-data.log
log4j.appender.data.Append=true
log4j.appender.data.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n

you root logger opens the log properties in the debug mode,

use INFO instead of DEbug in the first line of your properties file.

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