log4j配置文件错误检测
我目前正在使用 log4j 编写一个记录器。一旦我加载了 log4j.properties 或 log4j.xml 文件,我想知道是否有一种方法可以检测记录器配置文件是否有效。如果它无效,我希望改为加载默认设置(位于另一个文件中)。
谢谢
I'm currently writing a logger utilizing log4j. Once I load in a log4j.properties or a log4j.xml file, I was wondering if there was a way to detect if the logger configuration file is valid. If it isn't valid, I was hoping to load a default setting (that's located in another file) instead.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们通过在加载配置之前重定向 System.err 并检查错误是否记录到流中来解决这个问题:
We solved this problem by redirecting System.err before loading the configuration and checking if errors were logged to the stream:
您只能尝试手动检查记录器是否存在。
http ://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/LogManager.java?view=markup
就像:LogManager.exists(“你的记录器名称”);
此外,您还可以通过根据 DTD 验证您的 xml 是否有效。
但是“有效”日志文件的定义是什么?如果仅基于语法,请使用 DTD 验证并检查属性文件的格式是否正确。
如果文件有效并且具有特定星座,请使用上面的手动方法。
希望有帮助,
基督教
You can only try to check manually for a logger being existent or not.
http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/LogManager.java?view=markup
Like: LogManager.exists("your logger name");
Additionally you can check if your xml is valid or not with validating it against the DTD.
But what is the definition for a "valid" log file? If its only syntax based use DTD validation and check if the property file is in a good format.
If a file is valid if it has a specific constellation use the manual approach above.
Hope that helps,
Christian