异步日志 [英] Asynchronous Logging

查看:119
本文介绍了异步日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我的应用程序,在某些时候我们会记录一些沉重的东西到日志文件中。

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

基本上只为我们创建可用数据的JSON,然后登录到日志记录files.This是企业要求JSON格式的日志数据。

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 .

现在从数据创建JSON可用,然后记录到文件需要大量的时间和影响的原始请求返回时间。
现在想法是提高sitation

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() 

在我们的code,然后提交任务给它做哪些数据转换成JSON和随后的记录。

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?

我想AP preciate如果有人可以分享更好的解决方案。
好歹使用Log4j这个。我试图用AsyncAppender但没有取得任何预期的结果。
我们正在使用EJB 3时,JBoss 5.0,Log4j的,的Java6。

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.

推荐答案

我相信你是在正确的轨道在使用记录一个单独的线程池的条款。在很多的产品,你会看到异步日志记录功能。日志被累积并推到记录用比请求线程一个单独的线程的文件。特别是在prodcution环境中,数以百万计的传入请求和您的响应时间的需要小于几秒钟。你买不起任何东西,如登录系统变慢。所以,使用的方法是在内存缓冲区添加日志,并在合理的大小的块异步地推他们。

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.

谨慎的用于记录一个词,而使用线程池
由于多个线程将在日志文件(S)和内存日志缓冲区一起工作,你需要小心的记录。您需要在FIFO缓冲区的类型添加日志,以确保日志中按时间戳排序的日志文件打印出来。另外,还要确保文件访问是同步并在日志文件全部倒置或搞砸了,你就不会遇到的情况。

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.

这篇关于异步日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆