Log4j

 
 
Log4j is a package to output log statements to a variety of output targets.

The log statements can remain in your shipped code without incurring a heavy performance cost. The logging behavior can be controlled by editing a configuration file (log4j.properties), without modifying the application.

One of the distinctive features of log4j is the notion of inheritance in loggers. By using a logger hierarchy it is possible to control which log statements are output at arbitrarily fine granularity. This helps reduce the volume of logged output and minimize the cost of logging.

The target of the log output can be a file, an OutputStream, a java.io.Writer, a remote log4j server, a remote Unix Syslog daemon, or even a NT Event logger among many other output targets.

Log4j has three main components (loggers, appenders and layouts) which works together to enable developers to log messages according to message type and level, and to control at runtime how these messages are formatted and where they are reported.

Log4j is distributed at no charge for commercial or non-commercial use. For more information read the LICENSE.txt file.

More information about log4j can be found at http://logging.apache.org/log4j/docs/index.html

The latest Log4j version can be downloaded from http://logging.apache.org/log4j/docs/download.html







Using loggers.



Information
Loggers define a hierarchy and give the programmer run-time control on which statements are printed or not. Loggers can be assigned to the following ORDERD levels:

ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF
  • ALL: Has the lowest possible rank and is intended to turn on all logging.
  • DEBUG: Print the debugging information and is helpful in the development stage.
  • INFO: Print informational messages that highlight the progress of the application.
  • ERROR: Print error messages that might still allow the application to continue running.
  • WARN: Print information related to some faulty and unexpected behavior of the system, which needs attention in near future or else can cause malfunctioning of the application.
  • FATAL: Print system critical information, which are causing the application to crash.
  • OFF: Has the highest possible rank and is intended to turn off all the logging.
Normally you do not need to set the level of a logger programmatically. This is usually done in configuration files. A log statement is printed depending on its level and its logger. Logging requests are made by invoking one of the printing methods of a logger instance. These printing methods are debug, info, warn, error and fatal.

When a particular orderd level is specified, messages from all other levels of higher significance will be reported as well. E.g., when orderd level WARN is specified, then messages with orderd levels of ERROR and FATAL will also be posted.

Operating system used
Windows XP Home Edition Version 5.1 SP 2

Software prerequisites
Log4j 1.2.9

Procedure
  1. Create the following directories:

    c:\demo
    c:\demo\com
    c:\demo\com\mobilefish


  2. Create the file c:\demo\com\mobilefish\Log4jDemo.java.

    package com.mobilefish;
    import org.apache.log4j.Logger;

    public class Log4jDemo {

          static Logger log = Logger.getLogger("com.mobilefish.Log4jDemo");

          public static void main(String args[]) {

                log.debug("This is my debug message.");
                log.info("This is my info message.");
                log.warn("This is my warn message.");
                log.error("This is my error message.");
                log.fatal("This is my fatal message.");
          }
    }


  3. Open a dos window and type:

    cd c:\demo

  4. Compile the java code, type:

    javac ./com/mobilefish/Log4jDemo.java

  5. Create file c:\demo\log4j.properties

    log4j.rootLogger=ERROR, stdout
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=[%5p] %d{mm:ss} (%F:%M:%L)%n%m%n%n


    Note: The log4j.properties file must be put in the directory where you issued the java command.

  6. Run your application:

    java com.mobilefish.Log4jDemo

    The following log messages will be displayed:

    [ERROR] 08:34 (Log4jDemo.java:main:13)
    This is my error message.

    [FATAL] 08:34 (Log4jDemo.java:main:14)
    This is my fatal message.




  7. Change the following line in the log4j.properties file:

    log4j.rootLogger=ALL, stdout

    or

    log4j.rootLogger=DEBUG, stdout

  8. Run your application:

    java com.mobilefish.Log4jDemo

    The following log messages will be displayed:

    [DEBUG] 27:42 (Log4jDemo.java:main:10)
    This is my debug message.

    [ INFO] 27:42 (Log4jDemo.java:main:11)
    This is my info message.

    [ WARN] 27:42 (Log4jDemo.java:main:12)
    This is my warn message.

    [ERROR] 27:42 (Log4jDemo.java:main:13)
    This is my error message.

    [FATAL] 27:42 (Log4jDemo.java:main:14)
    This is my fatal message.


  9. Change the following line in the log4j.properties file:

    log4j.rootLogger=OFF, stdout

  10. Run your application:

    java com.mobilefish.Log4jDemo

    There will be no log messages displayed.

  11. Change the following line in the log4j.properties file:

    log4j.rootLogger=FATAL, stdout

  12. Run your application:

    java com.mobilefish.Log4jDemo

    The following log messages will be displayed:

    [FATAL] 27:42 (Log4jDemo.java:main:14)
    This is my fatal message.