关闭log4j Logger以释放文件描述符 [英] Closing log4j Logger to release File Descriptor

查看:393
本文介绍了关闭log4j Logger以释放文件描述符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个服务器,该服务器接收RMI请求并执行一些操作.

I have written a Server that receives RMI requests and performs some operations.

对于每个请求,都会创建一个Log4J Logger实例.以下是用于创建Logger实例的代码段.

For each request a Log4J Logger instance is created. Following is the code snippet which is used for creating Logger instance.

Logger log = Logger.getLogger("log_" + requestID);
log.setAdditivity(false);
FileAppender appender = null;
try {
    PatternLayout layout = new PatternLayout("%-5p %C{1} %d{yyyy-MM-dd HH:mm:ss} : %m%n");
    appender = new FileAppender(layout, logFileName, false);
} catch(Exception e) {
    logger.error("Error initializing logger", e);
}

log.addAppender(appender);
log.setLevel(level);

在这里一切正常.问题是在收到大量请求后超时,此服务器进程的打开文件数量增加并且没有减少,因此崩溃并出现异常,提示打开文件太多.

Things work fine here. The problem is overtime after receiving a lot of requests, the numbe of Open Files for this server process increases and does not come down thus crashing the process with an Exception saying Too Many Open Files.

进一步检查发现,问题出在请求完成后,因为文件描述符未释放.我尝试浏览Log4J文档,但找不到关于如何关闭一个记录器而又不影响其他可能在不同线程中运行的记录器的任何信息.

Upon further inspection it was identified that the problem is because of the File Descriptors are not released after the the request is complete. I have tried looking through the Log4J documentation but could not find anything on how can I close just one logger without affecting the others that might be running in different threads.

这里有什么想法吗?

推荐答案

看看

Take a look at the FileAppender.close() documentation, maybe it will solve your issue.

但是我认为您应该重新考虑您的设计,并且只有一个Logger实例,不是吗?

But I think that you should reconsider your design, and only have one Logger instance, no ?

这篇关于关闭log4j Logger以释放文件描述符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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