HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Log4j / Log4j SocketAppender and Socket Server Example

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=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=application-error.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
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
log4j.appender.server=org.apache.log4j.net.SocketAppender

#Port where socket server will be listening for the log events
log4j.appender.server.Port=4712

#Host name or IP address of socket server
log4j.appender.server.RemoteHost=localhost

#Define any connection delay before attempting to reconnect
log4j.appender.server.ReconnectionDelay=10000

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
		PropertyConfigurator.configure("log4j.properties");

		//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?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. sachin

    July 25, 2018

    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. sachin

    June 21, 2018

    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. Jeetendra Edke

    February 28, 2018

    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. Siva

    July 13, 2017

    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=/10.0.1.80,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?

    Regards,
    Siva Chandra

    • Lokesh Gupta

      July 14, 2017

      Are you able to ping to server ip?

Comments are closed on this article!

Search Tutorials

Log4j2 Tutorial

  • Log4j2 – Introduction
  • Log4j2 – JSON Config
  • Log4j2 – Properties Config
  • Log4j2 – XML Config
  • Log4j2 – RollingFileAppender
  • Log4j2 – Multiple appenders
  • Log4j2 – LevelRangeFilter
  • Log4j2 – HTMLLayout
  • Log4j2 – Fish Tagging
  • Log4j2 – Conversion Patterns
  • Log4j2 – JUnit

Log4j Tutorial

  • Log4j – Introduction
  • Log4j – Properties Config
  • Log4j – XML Config
  • Log4j – Maven Config
  • Log4j – Logging Levels
  • Log4j – ConsoleAppender
  • Log4j – RollingFileAppender
  • Log4j – SocketAppender
  • Log4j – JDBCAppender
  • Log4j – XMLLayout
  • Log4j – HTMLLayout
  • Log4j – Runtime Reload
  • Log4j vs. SLF4j
  • Log4j – RESTEasy + Tomcat 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces