Sunday, January 16, 2011

Log4J

In this tutorial you will learn about Log4J, which is one of the most used Logging API by the Java programmers. It is fast and easy to use and provides customizable log formats. Here we are providing many examples of the Log4J framework.

Log4J is an Open Source logging framework from the apache foundation. It's a Java based framework and can be used in any java program. It was originally developed by Ceki Gülcü and now its part of Apache Software project.

It can be used by the Java programmers to add logging functionality in the projects. These days most of the Java projects uses Apache Log4J for logging.

It provides powerful yet efficient and fast API for the Java programmers. Java programmers easily uses Log4J in their code to log the error and information messages into a log file.

Log4j simply inserts a log statement in the application code and manage them externally without going back to its application source code. Programmer's can control the logging message with the help of external configuration file e.g. log4.xml or log4j.properties file. The external properties file can be used to set the log format as well as the level of logging (DEBUG, INFO, FATAL etc..)

Three main component of Log4J

The architecture of Log4J framework is layered and consists of three main components. There components of the Log4J are:


  1. Logger
  2. Appender
  3. Layout

1. Logger

Logger is the most essential component of the logging process. It is responsible for capturing the logging information. There are 5 different log levels of the Logger. The level of Logger are as following:

There are 5 normal levels of logger:

  • DEBUG : Most useful to debug an application.
  • INFO : It provides informational messages.
  • WARN : It provides that application may have harmful events.
  • ERROR : It provides that application having error events but that might allow it to continue running.
  • FATAL : It denotes the severe error events which lead the application to abort.

In addition, there are two special levels also they are:

  • ALL : It is intended to turn on all logging
  • OFF : It is intended to turn off logging


2. Appenders in Log4J:

The Appender is responsible for publishing the log to a destination. It controls how the logging provides the output.

There are few list of appenders listed below:

  • ConsoleAppender
  • DailyRollingFileAppender
  • FileAppender
  • RollingFileAppender
  • WriterAppender
  • SMTPAppender
  • SocketAppender
  • SocketHubAppender
  • SyslogAppendersends
  • TelnetAppender


FileAppender appender = new FileAppender(new PatternLayout(),"filename");
 WriterAppender is used as:
appender = new WriterAppender(new PatternLayout(),new FileOutputStream("filename"));

3. Layouts in Log4J:

For each Appender it needs to have an associated Layout, which guides how to format the output. The Layout is responsible for formatting the log output in different layouts. User can control the output format by modifying the Log4J configuration file.

There are basically three types of Layout:

  1. HTMLLayout : It formats the output in the HTML table
  2. PatternLayout : It formats the output in the conversion pattern
  3. SimpleLayout : It formats the output in a simple manner, it prints the level then place a dash and then the user specified log message.

Configuring Log4J

For running application using Log4J you need to download the latest version log4j jar file and then add this to the classpath.

There are two ways to configure the Log4J one is by using properties file and other by using xml file. Using xml for Log4J is quite popular and it is recommended also.


Pattern Layout :

In our this section we are describing about most general layout PatternLayout. It is a flexible layout configurable with its pattern string. A conversion pattern consists of literal text and format control expressions. Here is the list of some of the conversion characters used for formatting:

Character Effect
cUsed to output the category of logging event
CUsed to output the fully qualified class name of the caller issuing the logging request
dUsed to output date of the logging event
mUsed to output the application supplied message associated with the logging event
nOutputs the platform dependent line separator character or characters

p

Used to output the priority of the logging event
rUsed to output the number of milliseconds elapsed from the construction of the layout until the creation of the logging event
tUsed to output name of thread of logging event
FUsed to output the file name


For More information:

http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html


Sample Log4.properties file

# Set root category priority to INFO and its appender to LOGFILE instead of the

# the default CONSOLE.

#log4j.rootCategory=INFO, CONSOLE

log4j.rootCategory=INFO, LOGFILE

# Set the enterprise logger category to FATAL and its appender to LOGILE.

#log4j.logger.org.apache.axis.enterprise=FATAL, LOGFILE

# Set root logger level to DEBUG and its only appender to A1.

#log4j.rootLogger=DEBUG, A1


# A1 is set to be a ConsoleAppender.

#log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.

#log4j.appender.A1.layout=org.apache.log4j.PatternLayout

#log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x (%F%l)- %m%n

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.


# This section is currently not being used. However due to some reasons, the

# the logging mechanism fails, then the console output will be based on the

# the pattern we define here.


#log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

#set back to WARN before shipping

#log4j.appender.CONSOLE.Threshold=WARN

#log4j.appender.CONSOLE.Threshold=INFO

#log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout

#log4j.appender.CONSOLE.layout.ConversionPattern=- %m%n


# LOGFILE is set to be a Rolling File appender using a PatternLayout.


#RollingFileAppender creates backup of old files if the current filesize increases

#beyond MaxFileSize (if property not defined, default is 10MB).

log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender


#The filename should be kept consistent with the executable name.

log4j.appender.LOGFILE.File=G:/example.log

# The logfile will always append and not start from beginning unless the size

# limit is reached.

log4j.appender.LOGFILE.Append=true


# The default level is set to INFO. This can be changed to DEBUG, INFO, FATAL,

# ERROR, WARN, TRACE

#set back to WARN before shipping

#log4j.appender.LOGFILE.Threshold=WARN

#log4j.appender.LOGFILE.Threshold=INFO


# Use the pattern layout for displaying date-time etc

log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout


# The actual pattern to be used.

#log4j.appender.LOGFILE.layout.ConversionPattern=%-4r %d [%t] %-5p %c %x - %m%n

log4j.appender.LOGFILE.layout.ConversionPattern=%-4r %d [%t] %-5p %c %x (%F%l)- %m%n

#log4j.appender.LOGFILE.layout.ConversionPattern= %d %-5p %c %x - %m%n

#log4j.appender.LOGFILE.layout.ConversionPattern=%p %t %c - %m%n


# The max file size of the axis log.

log4j.appender.LOGFILE.MaxFileSize=10MB


# The number of backup's to be created for the logfile before the first is

# cleared and reused.

# Currently setting to 1 as we require only one backup for the File.

log4j.appender.LOGFILE.MaxBackupIndex=1




No comments:

Post a Comment