log4j 附加程序之间的依赖关系

发布于 2024-08-02 05:44:21 字数 155 浏览 4 评论 0原文

我正在编写一个自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑饮青盏花 2024-08-09 05:44:21

我建议将第二个附加程序的配置选项移动/复制到自定义附加程序的配置中,然后在自定义附加程序中自己创建第二个附加程序。

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.

洒一地阳光 2024-08-09 05:44:21

如果您使用 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.

壹場煙雨 2024-08-09 05:44:21

如果事实证明没有办法执行您想要的操作,您可以从“主”附加程序内部创建和配置“后备”附加程序,而不是将其留给 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文