Log4j SocketAppender and Socket Server Example

Log4j is a simple and flexible logging framework. Logging equips the developer with detailed context for application failures. With log4j it is possible to enable logging at runtime without modifying the application binary.

Log4j comes with multiple options to format log files created by framework. It can create simple log files, html log files or xml log files also.

This this post, I am showing the example code for configuring log4j to record logs events in a simple socket server (packaged inside log4j.jar itself) over the network location.

Step 1) Create a maven java project and update log4j dependencies

Follow the steps given in this post related to configuring log4j with maven.

Step 2) Configure socket server logging configuration in log4j-server.properties file

Very few know this capability of log4j that it is bundled with a fully functional socket server which can be used to listen on network connections and record log events sent to server from various network nodes and locations.

To configure socket server, create a log4j-server.properties file in your project root folder as given. This file configures that how and where the log events received will be logged.

#Define a narrow log category. A category like debug will produce some extra logs also from server itself
log4j.rootLogger=ERROR, file

#Define how the socket server should store the log events
log4j.appender.file.layout.ConversionPattern=[%d] [%t] [%m]%n

Step 3) Configure log4j.properties to use SocketAppender

In this step, lets specify the remote socket server’s hostname [i.e. IP address] and port where it is listening for log events. We also will specify the log appender as SocketAppender which is capable to sending the log events to a socket connected on network.

#Define the log4j configuration for local application
log4j.rootLogger=ERROR, server

#We will use socket appender

#Port where socket server will be listening for the log events

#Host name or IP address of socket server

#Define any connection delay before attempting to reconnect

Step 4) Start the simple socket server

To start the server simple type below command to command prompt and server will be up and running:

java -classpath c:Users.m2repositorylog4jlog4j1.2.17log4j-1.2.17.jar org.apache.log4j.net.SimpleSocketServer
4712 log4j-server.properties

Please do not forget to specify the correct path of log4j.jar in your system.

Step 5) Test the application

Write a test class which configure log4j.properties and send a log event. I have written such a test class as below:

package com.howtodoinjava;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Log4jSocketAppenderExample
	static Logger logger = Logger.getLogger(Log4jSocketAppenderExample.class);
	public static void main(String[] args)
		//PropertiesConfigurator is used to configure logger from properties file

		//These logs will be sent to socket server as configured in log4j.xml
		logger.error("Log4j socket appender test run successfully!!");

Above code will create a log entry in application.log file as given:

[2013-04-09 09:00:34,044] [main] [Log4j socket appender test run successfully!!]

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

5 thoughts on “Log4j SocketAppender and Socket Server Example”

  1. executing the below is hanging the terminal in RHEL

    java -classpath /opt/logSocketServer/log4j-1.2.16.jar org.jboss.logging.Log4jSocketServer 14712 log4j-server.properties

    pressing ctrl+C is stopping the logSocketServer altogether. Please guide me how to proceed

  2. Hi I have started the server by below command
    java -classpath c:Users.m2repositorylog4jlog4j1.2.17log4j-1.2.17.jar org.apache.log4j.net.SimpleSocketServer
    4712 log4j-server.properties

    the port is listening as well.

    but i have changed the
    log file name and log4j.appender.file.layout.ConversionPatter in
    log4j-server.properties file, and the changes are not not reflected int the log file

    please let me know how to stop the server

  3. hi – I want to create multiple log files using SocketAppender on the same server. But even though using multiple FileAppenders in “log4j-server.properties”, it is not working and writing logs to only one file. Here, it writes logs on the file name mentioned in the last appender. It ignores all appenders apart from last one and write log only to single file instead of multiple files.
    Do we have any solution for the same?

  4. Hello,

    I did the following and got following exception:

    1. I have created the “log4j-server.properties” file and shared it to my friend (server) and asked him to execute the following command from command prompt:

    java -classpath c:Users.m2repositorylog4jlog4j1.2.17log4j-1.2.17.jar org.apache.log4j.net.SimpleSocketServer
    4712 log4j-server.properties.

    2. I have created the log4j.properties file, Log4jSocketAppenderExample.java file in my studio (client) and have executed the java class.

    3. Found the following exception in “application-error.log” (in my friends system – server)

    [2017-07-13 11:54:15,491] [main] [Could not open ObjectInputStream to Socket[addr=/,port=63872,localport=4712]]
    java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:209)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
    at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2338)
    at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2351)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2822)
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804)
    at java.io.ObjectInputStream.(ObjectInputStream.java:301)
    at org.apache.log4j.net.SocketNode.(SocketNode.java:56)
    at org.apache.log4j.net.SimpleSocketServer.main(SimpleSocketServer.java:67)

    Could you please help me on this?

    Siva Chandra


Leave a Comment


A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.