[an error occurred while processing this directive]

A simple example of log4j

                 

This Example shows you how to create a log in a program.

Description of the code:
  1. Logger.getLogger(): Logger class is used for handling the majority of log operations and getLogger method is used for return a logger according to the value of the parameter. If the logger already exists, then the existing instance will be returned. If the logger is not already exist, then create a new instance.
                
  2. Logger.debug(): This method is used to check that the specified category is DEBUG enabled or not, if yes then it converts the massage passed as a string argument to a string by using appropriate object renderer of class ObjectRenderer.
            
  3. Logger.info(): This method is used to check that the specified category is INFO enabled or not, if yes then it converts the massage passed as a string argument to a string by using appropriate object renderer of class ObjectRenderer.
              
  4. Logger.warn(): This method is used to check that the specified category is WARN enabled or not, if yes then it converts the massage passed as a string argument to a string by using appropriate object renderer of class ObjectRenderer.
                
  5. Logger.error(): This method is used to check that the specified category is ERROR enabled or not, if yes then it converts the massage passed as a string argument to a string by using appropriate object renderer of class ObjectRenderer.
              
  6. Logger.fatal(): This method is used to check that the specified category is FATAL enabled or not, if yes then it converts the massage passed as a string argument to a string by using appropriate object renderer of class ObjectRenderer.

LogExample.java


import org.apache.log4j.Logger;

public class LogExample {

    public LogExample() {
    }
    static Logger log = Logger.getLogger(LogExample.class);

    public static void main(String argsp[]) {

        log.debug("Here is some DEBUG");
        log.info("Here is some INFO");
        log.warn("Here is some WARN");
        log.error("Here is some ERROR");
        log.fatal("Here is some FATAL");
    }
}

log4j.properties


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


Output:
[DEBUG55:17 (LogExample.java:main:11)
Here is some DEBUG

INFO55:17 (LogExample.java:main:12)
Here is some INFO

WARN55:17 (LogExample.java:main:13)
Here is some WARN

[ERROR55:17 (LogExample.java:main:14)
Here is some ERROR

[FATAL55:17 (LogExample.java:main:15)
Here is some FATAL

Download code

                 

[an error occurred while processing this directive]