Log4j is a Reliable, Fast and Flexible Logging Framework (APIs) written in Java which is distributed under the Apache Software License. Log4j is highly configurable through external configuration files at runtime. It views the logging process in terms of levels of priorities and offers mechanisms to direct logging information to a great variety of destinations, such as a database, file, console, UNIX Syslog etc.
Log4j has three main components:
- loggers: Responsible for capturing logging information.
- appenders : Responsible for publishing logging information to various preferred destinations.
- layouts: Responsible to format logging information in different styles.
log4j Features:
- log4j is thread-safe.
- log4j supports multiple output appenders per logger.
- log4j supports internationalization.
- Logging behavior can be set at runtime using a configuration file.
- log4j is designed to handle Java Exceptions from the start.
- log4j uses multiple levels, namely ALL, TRACE, DEBUG, INFO, WARN, ERROR and FATAL.
- The format of the log output can be easily changed by extending the Layout class.
Logging Methods:
Once we obtain an instance of a named logger, we can use several methods of the logger to log messages. The Logger class has the following methods for printing the logging information.
public void debug(Object message)
This method prints messages with the level Level.DEBUG. |
public void error(Object message)
|
This method prints messages with the level Level.ERROR.
|
public void fatal(Object message);
|
This method prints messages with the level Level.FATAL.
|
public void info(Object message);
|
This method prints messages with the level Level.INFO.
|
public void warn(Object message);
|
This method prints messages with the level Level.WARN.
|
public void trace(Object message);
|
This method prints messages with the level Level.TRACE.
|
All the levels are defined in the org.apache.log4j.Level class and any of the above mentioned method can be called as follows:
import org.apache.log4j.Logger;
public class LogClass {
private static Logger log = Logger.getLogger(LogClass.class);
public static void main(String[] args) {
log.trace("Trace Message!");
log.debug("Debug Message!");
log.info("Info Message!");
log.warn("Warn Message!");
log.error("Error Message!");
log.fatal("Fatal Message!");
}
}
When you compile and run LogClass program it would generate following result:
Debug Message!
Info Message!
Warn Message!
Error Message!
Fatal Message!
log4j - Log Formatting:
Apache log4j provides various Layout objects, each of which can format logging data according to various layouts. It is also possible to create a Layout object that formats logging data in an application-specific way
The Layout Types:
The top-level class in the hierarchy is the abstract class org.apache.log4j.Layout. This is the base class for all other Layout classes in the log4j API.
The Layout class is defined as abstract within an application, we never use this class directly; instead, we work with its subclasses which are as follows:
- DateLayout
- HTMLLayout
- PatternLayout.
- SimpleLayout
- XMLLayout
log4j - Sample Program:
Following is a simple configuration file created for our example.It has the following information:
- The level of the root logger is defined as DEBUG and attaches appender named FILE to it.
- The appender FILE is defined as org.apache.log4j.FileAppender and writes to a file named "log.out" located in the log directory.
- The layout pattern defined is %m%n, which means the printed logging message will be followed by a newline character.
# Define the root logger with appender file
log = /usr/home/log4j
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
Using log4j in Java Program:
The following Java class is a very simple example that initializes, and then uses, the Log4J logging library for Java applications.
import org.apache.log4j.Logger;
import java.io.*;
import java.sql.SQLException;
import java.util.*;
public class log4jExample{
/* Get actual class name to be printed on */
static Logger log = Logger.getLogger(log4jExample.class.getName());
public static void main(String[] args) throws IOException,SQLException{
log.debug("Hello this is an debug message");
log.info("Hello this is an info message");
}
}
Enjoy Reading :)