log4j appenders – power of logging
Better replacement of System.out.println() is using log4j with console appender (You must have seen its example in How to use Log4j – An efficient and sufficient guide with examples).
ConsoleAppender might be useful for small applications or when you want to track application flow live. But if want to know historical flow/fault of your system you need to write logs to a file. As per need, log4j provides different set of appenders.
| ConsoleAppender | Logs to console |
| FileAppender | Logs to a file |
| SMTPAppender | Logs by email |
*You would have to set file name for FileAppender.
Sample log4j.properties
log4j.rootLogger=DEBUG, CA, FA, mail #Console Appender log4j.appender.CA=org.apache.log4j.ConsoleAppender log4j.appender.CA.layout=org.apache.log4j.PatternLayout log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n #File Appender log4j.appender.FA=org.apache.log4j.FileAppender log4j.appender.FA.File=sample.log log4j.appender.FA.layout=org.apache.log4j.PatternLayout log4j.appender.FA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n # Set the logger level of File Appender to WARN log4j.appender.FA.Threshold = WARN
Description
CA appender will show all logs on console for all levels ie debug, warn, error, fatal and info. While FA appender will write logs to sample.log file for warn, error and fatal levels. Last line of above property file, will set logger level for FA appender.
Each Appender object has different properties associated with it
| Property | Description |
| layout | Appender uses the Layout objects and the conversion pattern associated with them to format the logging information. |
| target | The target may be a console, a file, or another item depending on the appender. For example;
System.out in case of ConsoleAppender Sample.log in case of FileAppender I hadn’t tried file name or file stream in case of ConsoleAppender |
| level | The level is required to control the filteration of the log messages. |
| threshold | Appender can have a threshold level associated with it independent of the logger level. The Appender ignores any logging messages that have a level lower than the threshold level. |
| filter | The Filter objects can analyze logging information beyond level matching and decide whether logging requests should be handled by a particular Appender or ignored. |
*Every appender has some more properties inside.
Few days ago, when I faced some issues with NetBeans IDE, I switched to Eclips. Now the problem was my work. Placing java files from one place to another seems easy. But preparing configuration file is really time-consuming. And as usual I avoid manual work. SO let’s see what I did to import NetBeans workspace into Eclipse without wasting time









