While trying to execute the following lines only the last two statements are displayed(\"Here is some ERROR\" and \"Here is some FATAL\") and the first three statements are
I like to use a rolling file appender to write the logging info to a file. My log4j properties file typically looks something like this. I prefer this way since I like to make package specific logging in case I need varying degrees of logging for different packages. Only one package is mentioned in the example.
log4j.appender.RCS=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RCS.File.DateFormat='.'yyyy-ww
#define output location
log4j.appender.RCS.File=C:temp/logs/MyService.log
#define the file layout
log4j.appender.RCS.layout=org.apache.log4j.PatternLayout
log4j.appender.RCS.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm a} %5 %c{1}: Line#%L - %m%n
log4j.rootLogger=warn
#Define package specific logging
log4j.logger.MyService=debug, RCS
You probably have a log4j.properties file somewhere in the project. In that file you can configure which level of debug output you want. See this example:
log4j.rootLogger=info, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.logger.com.example=debug
The first line sets the log level for the root logger to "info", i.e. only info, warn, error and fatal will be printed to the console (which is the appender defined a little below that).
The last line sets the logger for com.example.* (if you get your loggers via LogFactory.getLogger(getClass())
) will be at debug level, i.e. debug will also be printed.
You need to set the logger level to the lowest you want to display. For example, if you want to display DEBUG messages, you need to set the logger level to DEBUG.
The Apache log4j manual has a section on Configuration.
Here's a quick one-line hack that I occasionally use to temporarily turn on log4j debug logging in a JUnit test:
Logger.getRootLogger().setLevel(Level.DEBUG);
or if you want to avoid adding imports:
org.apache.log4j.Logger.getRootLogger().setLevel(
org.apache.log4j.Level.DEBUG);
Note: this hack doesn't work in log4j2 because setLevel
has been removed from the API, and there doesn't appear to be equivalent functionality.
If you are coming here because you are using Apache commons logging with log4j and log4j isn't working as you expect then check that you actually have a log4j.jar in your run-time classpath. That one had me puzzled for a little while. I have now configured the runner in my dev environment to include -Dlog4j.debug in the Java command line so I can always see that Log4j is being initialized correctly
This is probably happening because your log4j configuration is set to ERROR
. Look for a log4j.properties file with contents like the following:
log4j.rootLogger=ERROR, CONSOLE
# console logging
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=DEBUG
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d %-5p %-20.20t %-24c{1}: %m%n
The rootLogger
is set to ERROR
level here using a CONSOLE
appender.
Note that some appenders like the console appender also have a Threshold
property that can be used to overrule the rootLoggers
level. You need to check both in this case.