如何使log4j仅按需创建日志文件? [英] How do I make log4j create log files on demand only?

查看:66
本文介绍了如何使log4j仅按需创建日志文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个模块化应用程序,其中模块具有自己的log4j日志(即通信日志和错误日志).这些附加程序和类别均在核心log4j XML中配置,但并非总是安装所有模块. DailyRollingFileAppender会创建文件而不管其使用情况如何,并且会公开完整的模块集(尽管不存在),并且由于其中某些模块是针对客户的,因此我们希望隐藏未使用的日志. 有没有办法让DailyRollingFileAppender在首次使用时创建其文件,而不是在启动时自动创建?

We have a modular application where modules have their own log4j logs (i.e. communication log and error log). The appenders and categories for these are all configured in the core log4j XML, but not all modules are always installed. The DailyRollingFileAppender creates its file regardless of use and that exposes the full set of modules although not present and as some of them are customer specific we'd like to hide logs not in use. Is there a way to make DailyRollingFileAppender create its file on first use instead of automatically at startup?

推荐答案

文件追加程序没有选项来延迟创建日志文件-setFile方法自动创建文件(如果尚不存在):ostream = new FileOutputStream(fileName, append);

The file appenders have no option to lazily create the log files - the setFile method automatically creates the file if it doesn't already exist: ostream = new FileOutputStream(fileName, append);

您必须扩展附加程序并自己覆盖文件初始化代码,以获取所需的行为.

You'll have to extend the appender and overwrite the file initialisation code yourself to get the behaviour you're after.

这篇关于如何使log4j仅按需创建日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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