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 categories.



Information
By using categories the developer can decide where the log messages should be written too (e.g. console, file etc.) and which log message level (e.g. DEBUG, INFO, WARN, ERROR, FATAL) should be logged.

Log message belongs to a particular category, in a hierarchy of categories. Category names are using a dot notation just like Java packages. There will always be a root category, which is the start of the hierarchy of categories.

Categories inherit settings from its parents. For example, the category named "com.mobilefish.bean" is a child of the category named "com.mobilefish" and inherits all of its properties.

You can use any category name but it is HIGHLY recommended to use the package name ending with its class name.

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
    c:\demo\com\mobilefish\bean


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

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

    public class Message {

          Logger log = Logger.getLogger("com.mobilefish.bean.Message");

          private String msg;

          public void setMessage(String msg) {
                this.msg = msg;

                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.");
          }

          public String getMessage() {

                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.");

                return msg;
          }
    }


  3. Create the file c:\demo\com\mobilefish\Log4jDemo3.java.

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

    public class Log4jDemo3 {

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

          public static void main(String args[]) {

                Message m = new Message();
                m.setMessage("Hello World");
                System.out.println(m.getMessage());
                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.");
          }
    }


  4. Open a dos window and type:

    cd c:\demo

  5. Compile the java code, type:

    javac ./com/mobilefish/bean/Message.java
    javac ./com/mobilefish/Log4jDemo3.java


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

    log4j.rootLogger=DEBUG, stdout

    # Global Threshold - overridden by any Categories below.
    log4j.appender.stdout.Threshold=WARN

    # Categories
    log4j.category.com.mobilefish=FATAL
    #log4j.category.com.mobilefish.bean=INFO

    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%5p [%c{1}] %M - %m%n


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



  7. Run your application:

    java com.mobilefish.Log4jDemo3

    The following log messages will be displayed:

    FATAL [Message] setMessage - This is my fatal message.
    FATAL [Message] getMessage - This is my fatal message.
    Hello World
    FATAL [Log4jDemo3] main - This is my fatal message.


    The category com.mobilefish in line "log4j.category.com.mobilefish=FATAL" is the parent of category com.mobilefish.bean. Only FATAL messages are logged.

  8. Uncomment the following line in the log4j.properties file:

    log4j.category.com.mobilefish.bean=INFO

  9. Run your application:

    java com.mobilefish.Log4jDemo3

    The following log messages will be displayed:

     WARN [Message] setMessage - This is my warn message.
    ERROR [Message] setMessage - This is my error message.
    FATAL [Message] setMessage - This is my fatal message.
     WARN [Message] getMessage - This is my warn message.
    ERROR [Message] getMessage - This is my error message.
    FATAL [Message] getMessage - This is my fatal message.
    Hello World
    FATAL [Log4jDemo3] main - This is my fatal message.


    INFO messages in category com.mobilefish.bean are NOT logged. This is because of line "log4j.appender.stdout.Threshold=WARN"

    This appender will not log any messages with priority lower than WARN even if the category's priority is set lower (INFO).

  10. Comment out the following lines in the log4j.properties file:

    #log4j.category.com.mobilefish=FATAL
    #log4j.category.com.mobilefish.bean=INFO


  11. Run your application:

    java com.mobilefish.Log4jDemo3

    The following log messages will be displayed:

     WARN [Message] setMessage - This is my warn message.
    ERROR [Message] setMessage - This is my error message.
    FATAL [Message] setMessage - This is my fatal message.
     WARN [Message] getMessage - This is my warn message.
    ERROR [Message] getMessage - This is my error message.
    FATAL [Message] getMessage - This is my fatal message.
    Hello World
    FATAL [Log4jDemo3] main - This is my fatal message.


    All log messages should be displayed due to line "log4j.rootLogger=DEBUG, stdout".
    However because of line "log4j.appender.stdout.Threshold=WARN" only messages with priority WARN or higher are logged.

  12. Comment out the following lines in the log4j.properties file:

    #log4j.appender.stdout.Threshold=WARN
    #log4j.category.com.mobilefish=FATAL
    #log4j.category.com.mobilefish.bean=INFO


  13. Run your application:

    java com.mobilefish.Log4jDemo3

    The following log messages will be displayed:

    DEBUG [Message] setMessage - This is my debug message.
     INFO [Message] setMessage - This is my info message.
     WARN [Message] setMessage - This is my warn message.
    ERROR [Message] setMessage - This is my error message.
    FATAL [Message] setMessage - This is my fatal message.
    DEBUG [Message] getMessage - This is my debug message.
     INFO [Message] getMessage - This is my info message.
     WARN [Message] getMessage - This is my warn message.
    ERROR [Message] getMessage - This is my error message.
    FATAL [Message] getMessage - This is my fatal message.
    Hello World
    DEBUG [Log4jDemo3] main - This is my debug message.
     INFO [Log4jDemo3] main - This is my info message.
     WARN [Log4jDemo3] main - This is my warn message.
    ERROR [Log4jDemo3] main - This is my error message.
    FATAL [Log4jDemo3] main - This is my fatal message.