Okay, you wrote a lot of Log4j debug statements all over your codebase but do not want them to appear in QA and/or Production environment. How to do that? You need to configure Log4j by modifying log4j.xml. You could also use log4j.properties in place of log4j.xml but some advanced settings can only be set using the latter. Might as well use something that offers greater benefits.
In log4j, there are levels of logging. Each level represents the severity or type of log is being made. From ascending order of level, these are TRACE, DEBUG, INFO, WARN, ERROR, and FATAL. FATAL has the highest level; whereas TRACE sits on the lowest level.
Software Environment
- Windows 7 Professional SP1
- Eclipse – Kepler Release
- Java 1.7 (1.7.0_67 – Windows x86)
- Apache Log4j
- Reference only log4j-1.2.16.jar
Your log4j.xml
This file should be included in the classpath.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?xml version="1.0" encoding="UTF-8" ?> <log4j:configuration debug="true" xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender class="org.apache.log4j.ConsoleAppender" name="console"> <layout class="org.apache.log4j.PatternLayout"><param name="ConversionPattern" value="%d{yyyy/MM/dd HH:mm:ss} %-5p %c{1}:%L - %m%n"> </layout> </appender> <root> <priority value="error"></priority> <!-- or <level value="error"></level> --> <appender-ref ref="console"> </appender-ref></root> </log4j:configuration> |
Notice particularly the level or priority tags, they allow you to specify the minimum level of the log. If INFO is specified, only INFO, WARN, ERROR, and FATAL logs are displayed. In this configuration, only ERROR and FATAL logs will be shown.
Sample Usage
The first example uses a logger.error() method to log an error. This logs the message in the ERROR level.
1 2 3 4 5 6 7 8 | import org.apache.log4j.Logger; public class LoggerTest { private static Logger logger = Logger.getLogger(LoggerTest.class.getName()); public static void main(String[] args) { logger.error("turreta.com"); } } } |
Sample output
1 2 3 4 5 6 7 8 9 | log4j: reset attribute= "false". log4j: Threshold ="null". log4j: Level value for root is [error]. log4j: root level set to ERROR log4j: Class name: [org.apache.log4j.ConsoleAppender] log4j: Parsing layout of class: "org.apache.log4j.PatternLayout" log4j: Setting property [conversionPattern] to [%d{yyyy/MM/dd HH:mm:ss} %-5p %c{1}:%L - %m%n]. log4j: Adding appender named [console] to category [root]. 2014/11/10 23:53:43 ERROR LoggerTest:5 - turreta.com |
Another example tries to log a WARN message but that message is not displayed.
1 2 3 4 5 6 7 8 | import org.apache.log4j.Logger; public class LoggerTest { private static Logger logger = Logger.getLogger(LoggerTest.class.getName()); public static void main(String[] args) { logger.debug("projecting knowledge"); logger.error("turreta.com"); } } |
Outputs:
1 2 3 4 5 6 7 8 9 | log4j: reset attribute= "false". log4j: Threshold ="null". log4j: Level value for root is [error]. log4j: root level set to ERROR log4j: Class name: [org.apache.log4j.ConsoleAppender] log4j: Parsing layout of class: "org.apache.log4j.PatternLayout" log4j: Setting property [conversionPattern] to [%d{yyyy/MM/dd HH:mm:ss} %-5p %c{1}:%L - %m%n]. log4j: Adding appender named [console] to category [root]. 2014/11/11 00:06:53 ERROR LoggerTest:6 - turreta.com |
Notice “ERROR LoggerTest:6”? This is on line 6. The message logged on line 5 is nowhere on the console.