Skip to content

Instantly share code, notes, and snippets.

@zenglian
Last active August 19, 2018 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zenglian/0d60700baeac42c0b36f8871b084fe14 to your computer and use it in GitHub Desktop.
Save zenglian/0d60700baeac42c0b36f8871b084fe14 to your computer and use it in GitHub Desktop.
log4j socket server: save log events from different clients to separate files

log4j socket server: save log events from each client to a separate file

Issue

An application can run on multiple hosts and if we want to collect the logs from all clients we can use log4j's socket appender.

Log4j has implemented SimpleSocketServer which calls SocketNode which collects all events from all clients to a single log file so it's difficult to identify them.

Goal

Out goal is to collect events from each client to a separate log file with file name xxx-{client}.log where {client} is the client address.

Implementation

This is implemented in https://github.com/zenglian/log4j-socket-server
The mainl logic is to add an appender for each SocketNode.
This will result in log files xxx-{client}.log, where {client} is the hostname or ip or the client.

Test result

-rw-r--r-- 1 leon dba 489884 May 26  2012 log4j-1.2.17.jar
-rw-r--r-- 1 leon dba746 Aug  5 20:33 log4j-server.properties
-rw-r--r-- 1 leon dba   5690 Aug 17 17:05 monitor-den01zur.example.com.log
-rw-r--r-- 1 leon dba  30741 Aug 17 17:05 monitor.log
-rw-r--r-- 1 leon dba   5946 Aug 17 17:04 monitor-slc10zhw.example.com.log
-rw-r--r-- 1 leon dba   5690 Aug 17 17:04 monitor-slc16ijn.example.com.log
-rw-r--r-- 1 leon dba   2838 Aug 17 17:05 monitor-slc16jeo.example.com.log
-rw-r--r-- 1 leon dba   8777 Aug 17 17:04 monitor-slc16ldq.example.com.log
-rwxr-xr-x 1 leon dba   5458 Aug 17 16:54 mylog4j.jar
-rwxr-xr-x 1 leon dba224 Aug 17 15:49 restart-log4j-server.sh
-rwxr-xr-x 1 leon dba 20 Aug 17 15:00 truncate-all.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment