Sunday, November 22, 2009

Log4j - Logging package

Log4j is a package which supports to do logging very easily. This logging outputs can be taken in many ways.

There are many more types can be used,
AsyncAppender, JDBCAppender, JMSAppender, LF5Appender, NTEventLogAppender,
NullAppender, NullAppender, SMTPAppender, SocketAppender, SocketHubAppender,
SyslogAppender, TelnetAppender, DailyRollingFileAppender, RollingFileAppender.
To begin with log4j, you should first download the log4j package. Here is a sample link to download it.

Log Levels

There are some log levels defined in log4j.
  1. all - All levels including custom levels
  2. trace (since log4j 1.2.12) - Developing only, can be used to follow the program execution.
  3. debug - Developing only, for debugging purpose
  4. info - Production optionally, Course grained (rarely written information), I use it to print that a configuration is initialized, a long running import job is starting and ending.
  5. warn - Production, simple application error or unexpected behaviour. Application can continue. I warn for example in case of bad logging attempts, unexpected data during import jobs
  6. error - Production, application error/exception but
    application can continue. Part of the application is probably not working
  7. fatal - Production, fatal application error/exception, application cannot continue, for example database is down.
  8. no - Do not log at all.

A simple example

First you better add downloaded jar package to the project using your IDE. Then you have to setup the log4j properties through property file or, xml file.... In this case, let's check with log4j.properties file. In your IDE, as other property files add your log4j properties file. For example create new file called "log4j.properties" It should be look like this.
log4j.rootLogger=debug, file
log4j.appender.file=org.apache.log4j.RollingFileAppender #logging Type
log4j.appender.file.maxFileSize=100KB #Max file size of a log file
log4j.appender.file.maxBackupIndex=5 #keeping backups of
log4j.appender.file.File=C://myprogram_logs/test.log #Location which saving log file
log4j.appender.file.threshold=debug #Log Level
log4j.appender.file.layout=org.apache.log4j.PatternLayout #Layout of the log file
log4j.appender.file.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n #Layout of the log file
I am using RollingFileAppender here. Max file size is 100KB, kind of all the properties related to Log4j is stated here.
At your java file, load the property file and put it into the PropertyConfigurator on log4j package. Example code snipped here,
Improte packages

import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.Logger;
import java.util.Properties;
On the relevant class, define the Logger type global variable(in my case "logger").
private static Logger logger = Logger.getLogger(MyProgram.class);
On main method,
   public static void main(String[] args){
Properties properties = new Properties();
InputStream inputStream = MyProgram.class.getClassLoader().getResourceAsStream("log4j.properties");
properties.load(inputStream);
PropertyConfigurator.configure(properties);
logger.info("My program Begins");
}
When you catch an exception, write it on log as follows
     try {
//Your code here
} catch (Exception e) {
logger.error("Found the exception here");
}

Now you will find logs on C://myprogram_logs/test.log location file.
When you need to use it in ubuntu, you just have to change the file location only from the properties file (i.e. /var/myprogram_logs/test.log).
See how easy logging you programming status with Log4j........
Here is a nice pdf to follow.
Cheers......

No comments:

Post a Comment