Saturday 2 August 2014

Logging Levels

Set of standard logging levels are provided by Java, to control logging output. Enabling logging at given level also enables logging at higher levels.

Below are the logging levels offered by logging package in descending order.

SEVERE (highest value)
WARNING
INFO
CONFIG
FINE
FINER
FINEST (lowest value)

If you enable logging at level 'INFO', then it enables logging for higher levels (WARNING, SEVERE) by default. We are allowed to set logging levels to both handlers and loggers.

import java.io.IOException;
import java.util.logging.*;

public class LoggingLevels {
    static final Logger log = Logger.getLogger("MyLog");
    static FileHandler fileHanlder1, fileHanlder2, fileHanlder3;
    static FileHandler fileHanlder4, fileHanlder5, fileHanlder6;
    
    static void doLogging(){
        log.severe("I am SEVERE Message");
        log.warning("I am WARNING Message");
        log.info("I am INFO Message");
        log.fine("I am FINE Message");
        log.finer("I am finer Message");
        log.finest("I am finest Message");
    }
    public static void main(String args[]) throws IOException{
        fileHanlder1 = new FileHandler("handle1.out");
        fileHanlder2 = new FileHandler("handle2.out");
        fileHanlder3 = new FileHandler("handle3.out");
        fileHanlder4 = new FileHandler("handle4.out");
        fileHanlder5 = new FileHandler("handle5.out");
        fileHanlder6 = new FileHandler("handle6.out");
        
        log.addHandler(fileHanlder1);
        log.addHandler(fileHanlder2);
        log.addHandler(fileHanlder3);
        log.addHandler(fileHanlder4);
        log.addHandler(fileHanlder5);
        log.addHandler(fileHanlder6);
        
        fileHanlder1.setLevel(Level.SEVERE);
        fileHanlder2.setLevel(Level.WARNING);
        fileHanlder3.setLevel(Level.INFO);
        fileHanlder4.setLevel(Level.FINE);
        fileHanlder5.setLevel(Level.FINER);
        fileHanlder6.setLevel(Level.FINEST);
        
        log.setLevel(Level.FINE);
        
        doLogging();
        
        fileHanlder1.close();
        fileHanlder2.close();
        fileHanlder3.close();
        fileHanlder4.close();
        fileHanlder5.close();
        fileHanlder6.close();
        
    }
}

Run the above program, you will see the below messages in console.

Jul 24, 2014 3:59:38 PM logger.LoggingLevels doLogging
SEVERE: I am SEVERE Message
Jul 24, 2014 3:59:38 PM logger.LoggingLevels doLogging
WARNING: I am WARNING Message
Jul 24, 2014 3:59:38 PM logger.LoggingLevels doLogging
INFO: I am INFO Message

Below files are created.
handle1.out : Contains only SEVERE Messages, since Level is set to SEVERE.
handle2.out : Contains Only SEVERE and WARNING Messages since Level is set to WARNING.
handle3.out : Contains Only SEVERE, WARNING and INFO Messages since Level is set to INFO.
handle4.out : Contains only SEVERE, WARNING, INFO and FINE messages. Since Level is set to FINE.
handle5.out and handle6.out contains only SEVERE, WARNING, INFO and FINE messages, even though their Level supports FINER and FINEST respectively. It is because above program set log level to FINE.

log.setLevel(Level.FINE)
Above statement sets the log level to FINE. So all the messages higher than or equal to FINE are processed by this logger. Message levels lower than this value will be discarded

Note:
In addition to the above logging levels, two more also supported.
a. OFF : used to turn off logging
b. ALL : used to enable logging of all messages.





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment