自适应 log4j 附加程序忽略负载下的日志指令

发布于 2024-12-31 22:30:52 字数 173 浏览 2 评论 0原文

我有一个特定的日志类别 - 在“高负载”的情况下,我想选择不记录。

日志数据写入文件系统,因此在这种情况下,我将“高负载”定义为文件随着时间的推移以高于特定阈值的速率增长。

是否有一个现有的附加程序能够识别文件增长异常快,然后忽略日志指令?

如果不是,这是否属于自定义附加程序的范围?

I have a specific log category which - in the event of "high load", I'd like to choose not to log.

The log data is written to the file system, so in this scenario, I'd define 'high load' as the file growing at a rate above a specific threshold over time.

Is there an existing appender which is able to recognize that the file is growing abnormally fast, and then ignore log directives?

If not, is this something that is within the scope of a custom appender?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

抠脚大汉 2025-01-07 22:30:52

遗憾的是,没有现有的 log4j 附加程序可以做这样的事情。

然而,可以开发自定义附加器(例如,作为现有附加器的子类)。它必须跟踪其吞吐量,并相应地丢弃消息。

自定义附加程序方法有两个注意事项:

  1. 在较大的系统中,附加程序将在 Log4J 日志服务器中运行,而不是在您的应用程序中运行。应用程序将消息发送到日志服务器,日志服务器随后调用附加器将其存储(在文件、数据库等中)。现在,这里的主要工作是将消息获取到日志服务器,因此丢弃日志服务器上的消息将是对资源的巨大浪费。相反,您必须在客户端(即您的应用程序中)丢弃它。

  2. 构建消息有时很昂贵(创建对象、字符串、参数化消息、收集统计数据、测量时间等)。同样,如果附加程序要丢弃结果,那么这样做会浪费资源。

尽管如此,它还是可以做到的。

首先,您的吞吐量感知附加程序必须是 SocketAppender 的子类,这样网络开销就不会白白浪费。
其次,附加程序必须为应用程序提供一种高效的方法来查明消息当前是否被丢弃。然后,在您的应用程序中,您可以编写高效的代码,例如:

if (MyLogHelper.isActuallyLogging(LOGGER, Level.DEBUG)) {
    LOGGER.debug(constructExpensiveMessage());
}

Sadly, there is no existing log4j appender that can do such a thing.

However, a custom appender could be developed (e.g. as a subclass of an existing appender). It would have to keep track of its throughput, and discard messages accordingly.

The custom appender approach has two caveats:

  1. In a larger system, the appender would run in the Log4J log server, not in your application. The application would send messages to the log server, which subsequently calls the appender to store it (in a file, database, whatever). Now, the main effort here is to get the message to the log server, so discarding the message on the log server would be a huge waste of resources. Instead, you would have to discard it on the client-side (that is in your application).

  2. Constructing the message is sometimes expensive (creating objects, strings, parameterized messages, gather statistics, measure time etc.). Again, it's a waste of resources to do this if the appender were to discard the results.

Still, it can be done.

Firstly, your throughput aware appender would have to be a subclass of SocketAppender, so that the network overhead is not expended for nothing.
Secondly, the appender would have to offer a highly efficient way for the application to find out if messages are currently being discarded. Then, in your application, you can write efficient code like:

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