Home  >  Core Java

Example of SocketHandler in Java

By Arvind Rai, April 13, 2013
SocketHandler is created by passing arguments host and port. SocketHandler has the publish method which publishes the LogRecord. If we do not provide host to SocketHandler, localhost will be used by default.
SocketHandlerTest.java
package com.concretepage;
import java.io.IOException;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SocketHandler;
import java.util.logging.XMLFormatter;
public class SocketHandlerTest {
    public static void main(String[] args) throws SecurityException, IOException {

        Logger logger = Logger.getLogger("concrete.log");

        Handler handler = new SocketHandler("localhost", 8080);
        
        LogRecord logRec = new LogRecord(Level.INFO, "Log recorded");
        
        handler.publish(logRec);

        handler.setFormatter(new XMLFormatter());

        logger.addHandler(handler);

        logger.info("socket handler info message");

    }
}
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us