log4j 附加程序之间的依赖关系
我正在编写一个自定义 log4j 附加程序,并且我想依赖另一个配置的附加程序作为后备,以防我的(数据库)附加程序失败。
如何保证附加器的构建顺序?我的附加程序的 activateOptions()
方法尝试访问另一个附加程序并失败,因为它尚未构造/注册。
I'm writing a custom log4j appender, and I want to rely on another configured appender as a fallback, in case my (Database) appender fails.
How can I guarantee order of construction of the appenders? My appender's activateOptions()
method tries to access another appender and fails because it's not constructed/registered yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议将第二个附加程序的配置选项移动/复制到自定义附加程序的配置中,然后在自定义附加程序中自己创建第二个附加程序。
I suggest to move/copy the config options for the second appender into the config of your custom appender and then create the second appender yourself inside of your custom appender.
如果您使用 XML 格式的配置文件,那么您可以利用 XML 文件中附加程序声明的顺序很重要这一事实。首先声明的appender 将首先被配置。如果您使用 .properties 格式的配置文件,那么它们的配置顺序取决于记录器(也称为类别)引用它们的顺序。首先被引用的appender会被首先配置。
您还可以查看 logback,它是 log4j 的后继者,它有很好的文档记录。
If you are using a configration file in XML, then you can take advantage of the fact that the order of declaration of appenders in an XML file matters. The appender which is declared first will be configured first. If you are using a configuration file in .properties format, then their order of configuration depends on the order in which they are referenced by loggers a.k.a. categories. The appender which is references first will be configured first.
You could also have a look at logback, log4j's successor which is quite well documented.
如果事实证明没有办法执行您想要的操作,您可以从“主”附加程序内部创建和配置“后备”附加程序,而不是将其留给 log4j 来配置。这不是一个很好的解决方案,但我不知道如何在附加程序之间添加依赖关系。
If it turns out that there isn't a way of doing what you want, you could create and configure the "fallback" appender from inside your "primary" appender, rather than leaving it to log4j to configure. Not a very nice solution, but I'm not aware of a way of adding dependencies between appenders otherwise.