Saturday 2 August 2014

Handler

A Handler object takes logger messages from a logger and export them to destinations like file, console, send them to a network logging service etc.,

ConsoleHandler, FileHandler, SocketHandler are some of the useful handlers available in java.util.logging package.


As you observe the above figure, logger sends the log messages to handler. Logger can add any number of handlers to it. In the below program, we add two handlers (file and console) to the logger. Logger creates a log record and send it to the registered handlers. Handler take these logger messages and export them to destinations like file, console etc.,

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

public class LoggingUsingHandler {
    static final Logger log = Logger.getLogger("MyLog");
    static ConsoleHandler conHandler = new ConsoleHandler();
    static FileHandler fileHanlder;
    
    static void processData(){
        log.info("Started Processing Data");
        /* Do Processing here */
        log.info("Finished processing data");
    }
    
    public static void main(String args[]) throws IOException{
        fileHanlder = new FileHandler("fileLog.out");
        
        log.addHandler(conHandler);
        log.addHandler(fileHanlder);
        
        processData();       
    }
}

Output
Jul 24, 2014 2:58:07 PM logger.LoggingUsingHandler processData
INFO: Started Processing Data
Jul 24, 2014 2:58:07 PM logger.LoggingUsingHandler processData
INFO: Started Processing Data
Jul 24, 2014 2:58:07 PM logger.LoggingUsingHandler processData
INFO: Finished processing data
Jul 24, 2014 2:58:07 PM logger.LoggingUsingHandler processData
INFO: Finished processing data

A file named ' fileLog.out' is created and stores all the logging messages.

static ConsoleHandler conHandler = new ConsoleHandler();
Above statement creates a console handler, which sent log messages to console.

fileHanlder = new FileHandler("fileLog.out");
Above statement creates a file name fileLog.out and send the log messages to this file.

The contents of 'fileLog.out' looks like below.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
<date>2014-07-24T14:55:32</date>
<millis>1406193932196</millis>
<sequence>0</sequence>
<logger>MyLog</logger>
<level>INFO</level>
<class>logger.LoggingUsingHandler</class>
<method>processData</method>
<thread>1</thread>
<message>Started Processing Data</message>
</record>
<record>
<date>2014-07-24T14:55:32</date>
<millis>1406193932230</millis>
<sequence>1</sequence>
<logger>MyLog</logger>
<level>INFO</level>
<class>logger.LoggingUsingHandler</class>
<method>processData</method>
<thread>1</thread>
<message>Finished processing data</message>
</record>
</log>

log.addHandler(conHandler);
log.addHandler(fileHanlder);

Above two statements add handlers to this particular logger.


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment