Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j: How does a Socket Appender work?

Tags:

java

log4j

I'm not sure how Socket Appender works. I know that logging events are sent to particular port. Then we can print logs on a console or put into a file.

My question is more about the way logs are sent. Is there e.g. one queue? Is it synchronous or asynchronous? Can using it slow down my program?

I've found some info here, but it isn't clear for me.

like image 783
alicjasalamon Avatar asked Aug 06 '12 09:08

alicjasalamon


People also ask

How does log4j Appender work?

In the log4j2 architecture, an appender is basically responsible for sending log messages to a certain output destination. Here are some of the most useful types of appenders that the library provides: ConsoleAppender – logs messages to the System console. FileAppender – writes log messages to a file.

What is JMS Appender in log4j?

Log4j JMS appender can be used to send your log messages to JMS broker. To use ActiveMQ as a destination of your messages, you need to configure JMS appender properly.

What is Appender logging?

The appender is the part of a logging system that's responsible for sending the log messages to some destination or medium. It answers the question "where do you want to store this stuff?"

What is Appender name in log4j?

In log4j speak, an output destination is called an appender. Currently, appenders exist for the console, files, GUI components, remote socket servers, JMS, NT Event Loggers, and remote UNIX Syslog daemons. It is also possible to log asynchronously. More than one appender can be attached to a logger.


2 Answers

From the SocketAppender documentation

Logging events are automatically buffered by the native TCP implementation. This means that if the link to server is slow but still faster than the rate of (log) event production by the client, the client will not be affected by the slow network connection. However, if the network connection is slower then the rate of event production, then the client can only progress at the network rate. In particular, if the network link to the the server is down, the client will be blocked.

On the other hand, if the network link is up, but the server is down, the client will not be blocked when making log requests but the log events will be lost due to server unavailability.

Since the appender uses the TCP protocol, I would say the log events are "sort of synchronous".

Basically, the appender uses TCP to send the first log event to the server. However, if the network latency is so high that the message has still not been sent by the time a second event is generated, then the second log event will have to wait (and thus block), until the first event is consumed. So yes, it would slow down your application, if the app generates log events faster than the network can pass them on.

As mentioned by @Akhil and @Nikita, JMSAppender or AsyncAppender would be better options if you don't want the performance of your application to be impacted by the network latency.

like image 179
Rajesh J Advani Avatar answered Oct 14 '22 22:10

Rajesh J Advani


Socket Appender sends the logs as a serialized Obect to a SocketNode or log server. In the appender the Connector Thread with a configured reconnectionDelay will check for the connection integrity and will dump all the logs if the connection is not initialized or disconnected.Hence no blocking on the application flow. If you need better JMS features in sending log info across JVM try JMSAppender.

  1. Log4j JMS appender can be used to send your log messages to JMS broker.The events are serialized and transmitted as JMS message type ObjectMessage.

You can get a sample program HERE.

like image 37
Akhi Avatar answered Oct 14 '22 22:10

Akhi