20

I've got an answer about how to use SocketAppender (I need it to gather logs from distributed system), but I am new to log4j and I have no idea how to use that sample code.

Probably I should have log4j-server.properties like that:

log4j.appender.SERVER=org.apache.log4j.net.SocketAppender
log4j.appender.SA.Port=4712
log4j.appender.SA.RemoteHost=loghost
log4j.appender.SA.ReconnectionDelay=10000

But I still don't know how to start the server (how to use this line)

org.apache.log4j.net.SimpleSocketServer 4712 log4j-server.properties

And what is the most important: Where\How can I see my logs?

Community
  • 1
  • 1
alicjasalamon
  • 4,171
  • 15
  • 41
  • 65
  • Those are called command line arguments. – Woot4Moo Aug 01 '12 at 12:48
  • Please note, that you should not use that feature with some log4j versions ("This affects Log4j versions up to 1.2 up to 1.2.17."): https://nvd.nist.gov/vuln/detail/CVE-2019-17571 – metters Jan 16 '20 at 07:29

2 Answers2

46

You can run the server using

java -classpath log4j.jar org.apache.log4j.net.SimpleSocketServer 4712 log4j-server.properties

The SimpleSocketServer receives logging events sent to the specified port number by the remote SocketAppender, and logs them as if they were generated locally, according to the configuration you supply in log4j-server.properties. It's up to you to configure the relevant console/file/rolling file appenders and attach them to the relevant loggers just as you would if you were doing the logging directly in the original process rather than piping the log events over a network socket. I.e. if you're currently creating local log files with something like:

log4j.rootLogger=DEBUG, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logfile.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

then you would change it so that the sending side log4j.properties simply says

log4j.rootLogger=DEBUG, server
log4j.appender.server=org.apache.log4j.net.SocketAppender
log4j.appender.server.Port=4712
log4j.appender.server.RemoteHost=loghost
log4j.appender.server.ReconnectionDelay=10000

and the server-side log4j-server.properties contains the definitions that were previously on the sending side:

log4j.rootLogger=DEBUG, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logfile.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

In particular, note that there's no point specifying a layout on the SocketAppender on the sending side - what goes over the network is the whole logging event object, it's the receiving side that is responsible for doing the layout.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • Thanks a lot, it works :) but I still have one question. Now I get extra logs like `[2012-08-01 15:20:39,950] [main] [Connected to client at /127.0.0.1] [2012-08-01 15:20:39,950] [main] [Starting new socket node.] [2012-08-01 15:20:39,950] [main] [Waiting to accept a new client.]` What should I do to avoid logging this? – alicjasalamon Aug 01 '12 at 13:23
  • 1
    Those look like messages generated by the socket server itself. You'll have to refine your logging configuration a bit. Depending on the level of these extra messages it may be enough to set your server-side root logger to ERROR rather than DEBUG and then add `log4j.logger.com.example=DEBUG` or whatever is appropriate to the loggers your sending side is logging to. – Ian Roberts Aug 01 '12 at 13:31
  • what about a support for Log4J 2 ? – Donatello Jan 23 '18 at 15:08
  • Can the server identify the client, e.g., different client to different file, or add client ip for each log event? – Leon Aug 05 '18 at 04:46
  • @Leon not as standard, but `SimpleSocketServer` is very simple code so it wouldn't be hard to modify it to add the client's IP address to the MDC before logging the event locally, then you could refer to that MDC key in your appender pattern – Ian Roberts Aug 15 '18 at 17:13
0

To start the server simple type below command in 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.

madhu
  • 1,083
  • 3
  • 12
  • 31