Log4J SocketAppender

A SocketAppender can be used to relay log entries to another host. For more information, see logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/net/SocketAppender.html.

Important:Data loss can occur if there are network errors and you use a SocketAppender exclusively. Therefore, the use of Filebeat is generally preferred.

The following procedure describes how to relay entries to another host, using localhost for test purposes.

To relay entries to another host

1. Prepare a custom log4j configuration file, as described in Custom Log Configurations.
2. Add the SocketAppender to the configuration file, using code similar to the following:

<appender name="RELAY" class="org.apache.log4j.net.SocketAppender" >

<!-- Demo purposes: relay to localhost -->

<param name="Port" value="7080" />

<param name="RemoteHost" value="localhost" />

<param name="ReconnectionDelay" value="60000" />

<param name="Threshold" value="INFO" />

</appender>

3. Add the appender to the root logger, using code similar to the following:

<root>

<level value="INFO"/>

<appender-ref ref="ASYNC"/>

<appender-ref ref="RELAY"/>

</root>

Try it out

You can test the relay of log entries. log4j includes a simple server that can be used to receive the log messages. You can download log4j.jar from logging.apache.org. The simple server will receive log messages and write them locally. Start with a simple logging configuration (call it log4j-relay.properties) like the following:

log4j.rootLogger=INFO, 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 start log4j's simple server like this:

java -classpath log4j-1.2.17.jar org.apache.log4j.net.SimpleSocketServer 7080 log4j-relay.properties

Log messages from flexnetls will start appearing in logfile.log. You can confirm this using the tail -f logfile.log command (Linux).