Java – asynchronous logging

Now in my application, at some point we are recording something heavy in the log file

Basically just for recording, we are creating JSON of available data and logging in to the log file This is the business requirement to record data in JSON format

Now it takes a lot of time to create JSON from the available data and record it to file, which affects the original request return time The idea now is to improve resettlement

One of the things we're talking about is creating a pool that uses threads

Executors.newSingleThreadExecutor()

In our code, the task is then submitted to the task of converting data into JSON and subsequent logging

Is this a good way? Are there any problems when we manage the thread pool itself?

If someone can share a better solution, a little use log4j I tried to use asyncappender but failed to achieve any desired results We use EJB 3, JBoss 5.0, log4j and JAVA6.0

Solution

I believe you are right in using a separate thread pool for logging In many products, you will see asynchronous logging Use a different thread from the requesting thread to accumulate logs and push them to the log file Especially in production environments, millions of incoming requests and responses take less than a few seconds You can't afford anything, such as logging in to slow down the system Therefore, the method used is to add logs to the memory buffer and push them asynchronously in blocks of considerable size

Be careful when using thread pool for logging. Since multiple threads will run on log files and memory log buffers, you need to pay attention to logging You need to add logs in FIFO type buffer to ensure that the logs are printed in the log file sorted by timestamp Also make sure that file access is synchronized and that you don't encounter complete reversal or confusion of log files

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>