What is the significance of log4j.rootLogger
property in log4j.properties
file? What happens if I don't use this property?
Example:
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
What happens if I set this property to ERROR
mode.
log4j.rootLogger property sets the Level (DEBUG here) and Appender (A1 here) for root Logger. This is not mandatory. Root logger does not have a default appender attached and it can exist without an appender. So, your log4j properties file can be without this property being set.
Root logger is the highest logger in the log4j hierarchy similar to Object class in Java.
To answer
If you don't set the rootLogger to a level and an appender, you will get a warning.
For example, if you omit or comment out the line
log4j.rootLogger=DEBUG, stdout
, i.e. say your log4j.properties file contains only the rootlogger and no additional loggers, here the root logger being commented out:You will get something like the following output:
Samudra Gupta explains in his book1:
If you have the following configuration:
This is how the logger hierarchy could end up looking:2
Samudra Gupta continues to explain:
NOTES
1 Samudra Gupta, Pro Apache Log4j, Second Edition (Berkeley, CA: Apress, 2005), 24-25, ISBN13: 978-1-59059-499-5
2 Dominic Mitchell, Logging in Java, http://happygiraffe.net/blog/2008/09/03/logging-in-java/, Retrieved 26 May 2014.