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 not displayed.I had just started to learn this topic, can anyone tell why is this happening?
logger.debug("Here is some DEBUG");
logger.info("Here is some INFO");
logger.warn("Here is some WARN");
logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL");
the log4j.property has
log4j.rootLogger=debug,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%5p] %d{mm:ss}(%F:%M:%L)%n%m%n%n
Here's a quick one-line hack that I occasionally use to temporarily turn on debug logging in a JUnit test:
Put a file named
log4j.xml
into your classpath. Contents are e.g.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:
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.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
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.
This is probably happening because your log4j configuration is set to
ERROR
. Look for a log4j.properties file with contents like the following:The
rootLogger
is set toERROR
level here using aCONSOLE
appender.Note that some appenders like the console appender also have a
Threshold
property that can be used to overrule therootLoggers
level. You need to check both in this case.