2

So i have this task to log activities to a file, but it has to be done remotely on the server side, Remote logging.

NOTE : Remote Logging has to be in latest version of Log4j2(2.10)

My task was simple

  1. Send logging info to a port.
  2. Log info from port to a file.

My Discoveries

  1. Socket Appender exist which help send info to a port. This is it, you dont need to create a client side code or anything.

Socket appender configuration in log4j2.properties

appender.socket.type = Socket
appender.socket.name= Socket_Appender
appender.socket.host = "IP address"
appender.socket.port = 8101
appender.socket.layout.type = SerializedLayout
appender.socket.connectTimeoutMillis = 2000
appender.socket.reconnectionDelayMillis = 1000
appender.socket.protocol = TCP

Adapting from here. But this is also log4j 1.x adaptation.

  1. I found out that before log4j 2.6 to listen to a port we used TcpSocketServer which started a server using LogEventBridgeThis helped reach that conclusion. This class was in core.net.server which is no longer available.Assuming it is not used anymore and the only similar/closest class, TcpSocketManager.Other links that helped. How to use SocketAppend?
  2. Then i tried this

    public static final Logger LOG=LogManager.getLogger(myapp.class.getName());
    
    main(){
    LOG.debug("DEBUG LEVEL");
    }
    

and got the following error

main ERROR TcpSocketManager (TCP:IPAddress:8111) caught exception and will continue: java.net.SocketTimeoutException: connect timed out

I know this work because i made it read to a socket but there was no one listening, but somehow i messed up big time and there was a code change.

I need help how to go ahead. Thank You in advance

ABHINAV RANA
  • 67
  • 1
  • 10

1 Answers1

4

The socket server to remotely receive log events has been moved to a separate repository: https://github.com/apache/logging-log4j-tools

This still needs to be released.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
  • As i specified i am doing remote logging. This is what is did. SocketAppender and its logger will automatically send logs across using the port number. Next i fired up the code and got error "_Socket Not Available Error_". So i created a socket make it listen to that port number, using ServerSocket. All good now its sending logs across. >But now how do i output that event logs received via socketappender to console or file using via the same properties file on the receiving end? – ABHINAV RANA Dec 19 '17 at 08:52
  • You can check out the above logging-log4j-tools project and build it with `mvn clean install`. This will generate a binary (jar) containing a socket server that can output the received log events into a log file. – Remko Popma Dec 19 '17 at 09:01
  • @RemkoPopma Nexus is not able to get this dependency. Do we need to build this project locally and push it to the nexus? – Gaurav Apr 26 '19 at 07:26
  • Yes please. Also please let the log4j dev community know with a jira ticket or email to logging-users. – Remko Popma Apr 26 '19 at 08:36