javajava.util.logging

Simple SocketHandler for java8 logging(java.util.logging) throws ConnectionException


The intention is to stream the log during runtime on a specific host:port, so that the logs are accessible to users outside the running system, from browser.

As you can see, i have created a simple SocketHandler for java8 logging(java.util.logging), is there something that i have missed?

import java.net.MalformedURLException;
import java.net.URL;

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 Main {
    public static void main(String[] args) throws Exception {

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

        SocketHandler handler = new SocketHandler("HOSTNAME", 19004);

        LogRecord logRec = new LogRecord(Level.INFO, "Log recorded");

        handler.publish(logRec);

        handler.setFormatter(new XMLFormatter());

        logger.addHandler(handler);

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

When i run the code, i see the following exception, i have tried checking the system firewall settings on both local(mac/windows) and remote(Linux) and seen that the settings do not block 19004 port

Exception in thread "main" java.net.ConnectException: Connection refused (Connection refused)
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:476)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:218)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:200)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:394)
        at java.net.Socket.connect(Socket.java:606)
        at java.net.Socket.connect(Socket.java:555)
        at java.net.Socket.<init>(Socket.java:451)
        at java.net.Socket.<init>(Socket.java:228)
        at java.util.logging.SocketHandler.connect(SocketHandler.java:167)
        at java.util.logging.SocketHandler.<init>(SocketHandler.java:154)
        at Main.main(Main.java:16)

UPDATE

As suggested by bosowski

When i create Socket to listen to a specific port, the log messages are getting printed on the console of the host. However, am unable to access hostname:port for the log to be streamed from the browser. Is there anything specific that needs to be performed after this step? Please let me know

import java.io.*;
import java.net.*;

public class MyServer {

    public static void main(String[] args)
    {

        try {

            ServerSocket ss = new ServerSocket(19004);
            Socket soc = ss.accept();
            DataInputStream dis
                    = new DataInputStream(soc.getInputStream());

            String str = (String)dis.readUTF();

            System.out.println("message= " + str);

            ss.close();
        }
        catch (Exception e) {

            System.out.println(e);
        }
    }
}



Solution

  • SocketHandler does not open up a port to connect to, if that's what you're assuming. It tries to connect to the specified host and port, so you need to have a port that is listening on the host that you are trying to connect to.

    https://docs.oracle.com/javase/8/docs/api/java/util/logging/SocketHandler.html#SocketHandler-java.lang.String-int-

    <handler-name>.host specifies the target host name to connect to (no default).

    <handler-name>.port specifies the target TCP port to use (no default).

    If you do indeed have a listening TCP port on the hostname that you're trying to connect to, you can try running sudo nmap -F hostname to check if the port is indeed accessible from your machine.