Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous Logging

Right now in my application,at certain points we are logging some heavy stuff into the log files.

Basically only for logging we are creating JSON of the data available and then logging into Log files.This is business requirement to log data in JSON format .

Now creating JSON from the data available and then logging to FILE takes lot of time and impacts the original request return time. Now idea is to improve the sitation .

One of the things that we have discussed is to create a thread pool using

Executors.newSingleThreadExecutor() 

in our code and then submitting the task to it which does the conversion of data into JSON and subsequent logging.

Is it a good approach to do this ?As we are managing the thread pool itself ,is it going to create some issues?

I would appreciate if someone can share better solutions. Someway to use Log4j for this .I tried to use AsyncAppender but didnt achieve any desired result. We are using EJB 3,Jboss 5.0,Log4j,java6.

like image 418
Rips Avatar asked Jun 10 '13 07:06

Rips


3 Answers

I believe you are on right track in terms of using a separate thread pool for logging. In lot of products you will see the asynchronous logging feature. Logs are accumulated and pushed to log files using a separate thread than the request thread. Especially in prodcution environments, where are millions of incoming request and your response time need to be less than few seconds. You cannot afford anything such as logging to slow down the system. So the approach used is to add logs in a memory buffer and push them asynchronously in reasonably sized chunks.

A word of caution while using thread pool for logging As multiple threads will be working on the log file(s) and on a memory log buffer, you need to be careful about the logging. You need to add logs in a FIFO kind of a buffer to be sure that logs are printed in the log files sorted by time stamp. Also make sure the file access is synchronized and you don't run into situation where log file is all upside down or messed up.

like image 183
Juned Ahsan Avatar answered Oct 12 '22 02:10

Juned Ahsan


Have a look at Logback,AsyncAppender it already provide separate threadpool, queue etc and is easily configurable, it almost do the same as you are doing, but saves you from re-inventing the wheel.

like image 7
Alankar Srivastava Avatar answered Oct 12 '22 03:10

Alankar Srivastava


Is using MongoDB for logging considered?

  1. MongoDB inserts can be done asynchronously. One wouldn’t want a user’s experience to grind to a halt if logging were slow, stalled or down. MongoDB provides the ability to fire off an insert into a log collection and not wait for a response code. (If one wants a response, one calls getLastError() — we would skip that here.)
  2. Old log data automatically LRU’s out. By using capped collections, we preallocate space for logs, and once it is full, the log wraps and reuses the space specified. No risk of filling up a disk with excessive log information, and no need to write log archival / deletion scripts.
  3. It’s fast enough for the problem. First, MongoDB is very fast in general, fast enough for problems like this. Second, when using a capped collection, insertion order is automatically preserved: we don’t need to create an index on timestamp. This makes things even faster, and is important given that the logging use case has a very high number of writes compared to reads (opposite of most database problems).
  4. Document-oriented / JSON is a great format for log information. Very flexible and “schemaless” in the sense we can throw in an extra field any time we want.
like image 3
rajesh Avatar answered Oct 12 '22 04:10

rajesh