Log4j Configuration With properties file
Log4j configuration can be done either using an XML or a properties file externally. This makes your code independent of logging related steps to be followed in your code, so no ongoing source code modifications.
All you require to achieve that are logger, appender and layout. The logger object is used to do logging, appender decides where you want to append logging messages, to a console or to a file and finally layout decides on format, the way you want logging messages to be logged.
In the previous post, where BasicConfigurator.configure() is used, the default appender ConsoleAppender and layout PatternLayout are in use.
First of all the contents of log4j.properties file looks like:
And now the ExampleLog4j.java file:
To make things simpler for you, here is a snapshot from my Eclipse Galileo project:
The log4j levels follow the following order.
DEBUG
INFO
WARN
ERROR
FATAL
As the code level mentioned in .properties file is ERROR, it displays ERROR and FATAL messages only in result displayed of the code. If the code level would have been DEBUG then, all the messages would have been displayed on the console.
The result of the code looks like as given below:
All you require to achieve that are logger, appender and layout. The logger object is used to do logging, appender decides where you want to append logging messages, to a console or to a file and finally layout decides on format, the way you want logging messages to be logged.
In the previous post, where BasicConfigurator.configure() is used, the default appender ConsoleAppender and layout PatternLayout are in use.
First of all the contents of log4j.properties file looks like:
log4j.rootLogger=ERROR, CA log4j.appender.CA=org.apache.log4j.ConsoleAppender log4j.appender.CA.layout=org.apache.log4j.PatternLayout log4j.appender.CA.layout.ConversionPattern=%-1r [%t] %-5p %c %x - %m%n
And now the ExampleLog4j.java file:
package com.iqjava.log4j; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; public class ExampleLog4j { static final Logger logger = Logger.getLogger(ExampleLog4j.class); public static void main(String[] args) { PropertyConfigurator.configure("log4j.properties"); logger.debug("Here goes the debug message"); logger.error("Here goes the error message"); logger.fatal("Here goes the fatal message"); logger.info("Here goes the info message"); logger.warn("Here goes the warn message"); } }
To make things simpler for you, here is a snapshot from my Eclipse Galileo project:
The log4j levels follow the following order.
DEBUG
INFO
WARN
ERROR
FATAL
As the code level mentioned in .properties file is ERROR, it displays ERROR and FATAL messages only in result displayed of the code. If the code level would have been DEBUG then, all the messages would have been displayed on the console.
The result of the code looks like as given below:
0 [main] ERROR com.iqjava.log4j.ExampleLog4j - Here goes the error message 2 [main] FATAL com.iqjava.log4j.ExampleLog4j - Here goes the fatal message