পৃষ্ঠাসমূহ

Search Your Article

CS

Pageviews

Showing posts with label log4j Tutorial. Show all posts
Showing posts with label log4j Tutorial. Show all posts

Sunday, January 22, 2017

log4j - Overview

log4j is a reliable, fast and flexible logging framework (APIs) written in Java, which is distributed under the Apache Software License.
log4j has been ported to the C, C++, C#, Perl, Python, Ruby, and Eiffel languages.

log4j - Installation

log4j API package is distributed under the Apache Software License, a full-fledged open source license certified by the open source initiative.
The latest log4j version, including full-source code, class files and documentation can be found at http://logging.apache.org/log4j/.

log4j - Architecture

log4j API follows a layered architecture where each layer provides different objects to perform different tasks. This layered architecture makes the design flexible and easy to extend in future.
There are two types of objects available with log4j framework.

log4j - Configuration

The previous chapter explained the core components of log4j. This chapter explains how you can configure the core components using a configuration file. Configuring log4j involves assigning the Level, defining Appender, and specifying Layout objects in a configuration file.

log4j - Sample Program

We have seen how to create a configuration file. This chapter describe how to generate debug messages and log them in a simple text file.
Following is a simple configuration file created for our example. Let us revise it once again:

log4j - Logging Methods

Logger class provides a variety of methods to handle logging activities. The Logger class does not allow us to instantiate a new Logger instance but it provides two static methods for obtaining a Logger object −
  • public static Logger getRootLogger();
  • public static Logger getLogger(String name);
The first of the two methods returns the application instance's root logger and it does not have a name.
Any other named Logger object instance is obtained through the second method by passing the name of the logger. The name of the logger can be any string you can pass, usually a class or a package name as we have used in the last chapter and it is mentioned below −
static Logger log = Logger.getLogger(log4jExample.class.getName());

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.
# Methods and Description
1 public void debug(Object message) It prints messages with the level Level.DEBUG.
2 public void error(Object message) It prints messages with the level Level.ERROR.
3 public void fatal(Object message) It prints messages with the level Level.FATAL.
4 public void info(Object message) It prints messages with the level Level.INFO.
5 public void warn(Object message) It prints messages with the level Level.WARN.
6 public void trace(Object message) It 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 methods can be called as follows −
import org.apache.log4j.Logger;

public class LogClass {
   private static org.apache.log4j.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 the following result −
Debug Message!
Info Message!
Warn Message!
Error Message!
Fatal Message!
All the debug messages make more sense when they are used in combination with levels. We will cover levels in the next chapter and then, you would have a good understanding of how to use these methods in combination with different levels of debugging.

log4j - Logging Levels

The org.apache.log4j.Level levels. You can also define your custom levels by sub-classing the Level class.
Level Description
ALL All levels including custom levels.

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.
All Layout objects receive a LoggingEvent object from the Appender objects.

log4j - Logging in Files

To write your logging information into a file, you would have to use org.apache.log4j.FileAppender.

FileAppender Configuration

FileAppender has the following configurable parameters:

log4j - Logging in Database

The log4j API provides the org.apache.log4j.jdbc.JDBCAppender object, which can put logging information in a specified database.

log4j Questions and Answers

log4j Questions and Answers has been designed with a special intention of helping students and professionals preparing for various Certification Exams and Job Interviews. This section provides a useful collection of sample Interview Questions and Multiple Choice Questions (MCQs) and their answers with appropriate explanations.

log4j - Quick Guide

log4j is a reliable, fast and flexible logging framework (APIs) written in Java, which is distributed under the Apache Software License.
log4j has been ported to the C, C++, C#, Perl, Python, Ruby, and Eiffel languages.

log4j - Useful Resources

The following resources contain additional information on log4j. Please use them to get more in-depth knowledge on this topic.

Discuss Log4J

log4j is a reliable, fast and flexible logging framework (APIs) written in Java, which is distributed under the Apache Software License. log4j is a popular logging package written in Java. log4j has been ported to the C, C++, C#, Perl, Python, Ruby, and Eiffel languages.