Saturday, January 19, 2008

Session Summary - Log4J

Having being worked on Log4J attending Mr Vamshi's presentation on Log4J helped me in understanding Log4J utility

Log4J

An open source logging tool developed by Apache. A set of APIs that allows developers to write log statements in their code and configure them externally, using properties files.

Three Aspects of Log4j: Logger, Appender, Layout

He told us that each class in your application can have an individual logger or a common logger. log4j provides a root looger that all of your loggers will inherit from.

To create a logger we need to call a static method of the Logger class that will retrieve a logger for you by name.

When the question came from the audiences as to when what is an Appender?

He smartly answered saying : - Loggers need to know where to send your requests for logging. This is where the appenders come into picture. log4j supports writing to files (FileAppender), to console (ConsoleAppender), to databases (JDBCAppender), to NT event logs (NTEventLogAppender), to SMTP servers (SMTPAppender), to remote servers (SocketAppender), and others.

Loggers follow a parent-child relationship pattern.

There are five levels of logging in log4j:

  • DEBUG – fine grained informational events that are most useful to debug an application.
  • INFO - messages that highlight the progress of the application
  • WARN - potentially harmful situations
  • ERROR - error events that might still allow the application to continue running
  • FATAL - very severe error events

This became clear when he executed a program just to show how log4j works.

import org.apache.log4j.Logger;

import org.apache.log4j.BasicConfigurator;

public class HelloWorld {

static Logger logger = Logger.getLogger("HelloWorld");

static public void main(String[] args) {

BasicConfigurator.configure();

logger.debug("Hello World.");

}

}

In short log4j prints messages on the console. Very useful for a programmer like me – just to know how things are working on the server and also helps in troubleshooting the errors & silly mistakes :)

He concluded with the slide – Things not to be logged

  • Misleading information
  • Username & password
  • Logging for each request – will reduce the performance

Some important tips given by him:

> Never neglect warning logs

> Separate Request & Transaction log files

No comments: