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.