如何调节日志指令随时间产生的打印输出量?

发布于 2024-07-24 22:00:00 字数 454 浏览 3 评论 0原文

如何将程序日志打印输出限制为 Y 秒内最多 X 个打印输出?

使用 java.util.logging 进行服务器端编程,我的代码有很多信息、警告和错误语句,例如:

s_logger.logp(Level.WARNING, myClassName, myMethodName, "msg.code.in.properties.file");

一方面,我确实希望看到上面的警告消息打印到 STDOUT,因为它表明出了问题,特别是在调查生产中的问题时,但另一方面,在短时间内打印大量讲述同一个故事的行,没有任何附加价值,反而会影响性能并引入滚动失明。

我正在寻找一种机制/API,可以将打印输出调节为在 y 秒内每个消息代码不超过 x 条消息。 例如,在一分钟内,我不想生成并向日志写入超过 10 条类型为“事务超时”的消息。

我很乐意提供 API 或库参考,有人吗?

How can I limit a program log printouts to a maximum of X printouts within Y seconds?

Programming server side with java.util.logging, my code has a lot of info, warning, and error statements like:

s_logger.logp(Level.WARNING, myClassName, myMethodName, "msg.code.in.properties.file");

On the one hand, I do want to see the warning message above printed to the STDOUT, since it serves an indication that something went wrong, especially when investigating problems in production, but on the other hand, printing numerous lines that tells the same story over a short period of time, has no added value but instead hit performance and introduces scrolling blindness.

What I'm looking for is a mechanism/API that could regulate the printouts to no more than x messages, per message code, during y seconds. For example, over the course of a minute, I don't want to generate and write to the log more than 10 messages of the type: "transaction timed out".

I'd be happy for a API or a library reference, Anyone?

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

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

发布评论

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

评论(4

桃气十足 2024-07-31 22:00:00

如果您想使用标准日志记录库/机制,您可以在标准记录器周围生成自己的包装器,它将检查您是否记录的条件,并仅在满足条件时将信息传递给日志记录机制。

或者您可以编写自己的(简单的,也可能不是)记录器:)

if you want to use standard logging library/mechanism, you may produce your own wrapper around the standard logger which will check for your conditions whether to log or not, and pass the info down to the logging mechanism only if conditions are satisfied.

or you can write your own (simple, or maybe not) logger :)

翻了热茶 2024-07-31 22:00:00

谢谢。 我已经考虑过这一点。
我希望有一些已经存在的东西,因为我相信这个问题在服务器端开发项目中很常见。

Thanks. I already considered that.
I was hoping for something that's already out there, since I believe this problem to be common on server side development projects.

一生独一 2024-07-31 22:00:00

吉利,我不确定这样的东西是否存在。 像 Logback 这样的良好日志记录实现将允许您为代码的每个部分定义记录器,然后配置日志记录级别每个 Logger 的信息(例如,默认情况下只有 WARN 及以上消息,但对于控制您正在主动调试的区域的 Logger,则为 INFO 甚至 FINE)。

日志记录实现通常还可以管理多个日志文件,将当前日志滚动到压缩存档版本中,并定期或在日志文件变得太大时启动新的日志文件。

如果您可以控制代码,则可以修改日志记录语句以不生成如此多的冗余警告。 例如,如果您遇到大量事务超时,也许您可​​以在事务启动尝试之间等待更长的时间,然后再完全放弃。 (这样做的优点是可以降低实现事务的服务器上的负载,给它们时间恢复。)

zappan 提到的另一种方法是创建您自己的日志过滤器。 在 java.util.logging 中,您可以将自己的过滤器附加到您的 过滤器来限制它们打印的内容。

更精细的过滤方法是将日志记录实现配置为通过多播地址而不是文件发送所有日志消息。 您可以编写一个过滤程序来侦听多播并有选择地将您想要的内容写入磁盘(例如,如果您有分布式系统,则可以在日志聚合服务器上运行)。 其他过滤器可以提取最重要的消息并更新系统运行状况的图形显示。

Gili, I'm not sure anything like this exists right out of the box. A good logging implementation like Logback will let you define Loggers for each portion of your code and then configure the logging level of each Logger (eg, only WARN and above messages by default, but INFO or even FINE for the Logger controlling an area you're actively debugging).

A logging implementation also typically can manage multiple log files, rolling over the current log into a compressed archive version and starting a fresh one either at regular intervals or whenever a log file grows too large.

If you are in control of the code, you could modify your logging statements to not generate so many redundant warnings. For example if you are getting a lot of transaction timeouts, maybe you could wait for successively longer periods between transaction start attempts before giving up altogether. (This has the advantage of ratcheting down the load on the servers implementing the transactions, giving them time to recover.)

Another approach alluded to by zappan is to create your own log filters. In java.util.logging you can attach your own Filters to your Filters to restrict what they print out.

A more elaborate filtering approach would be to configure your logging implementation to send all log messages out on a multicast address instead of a file. You'd write a filtering program that listens to the multicast and selectively writes the ones you want out to disk (eg, this could run on a log aggregation server if you have a distributed system). Other filters could pull out the most important messages and update a graphical display of your system's health.

橪书 2024-07-31 22:00:00

如何将程序日志打印输出限制为 Y 秒内最多 X 个打印输出?

如果您有权访问 JavaMail 1.5.5 或logging-mailhandler 1.5.5,您可以使用 < a href="https://javaee.github.io/javamail/docs/api/com/sun/mail/util/logging/DurationFilter.html" rel="nofollow noreferrer">com.sun.mail.util.logging .DurationFilter 限制特定的java.util.logging.Loggerjava.util.logging.Handler

我正在寻找一种机制/API,可以在 y 秒内将每个消息代码的打印输出调整为不超过 x 条消息。 例如,在一分钟内,我不想生成并向日志写入超过 10 条此类消息:“事务超时”。

如果超时消息仅在一个记录器本地,则与上述相同。 否则,您必须创建自定义过滤器。 正如其他答案中所指出的,将过滤器与其他过滤器组合起来并不难。

另一种选择是创建代理处理程序,在将消息发布到其他下游处理程序之前缓冲并删除给定时间窗口内的重复消息。

How can I limit a program log printouts to a maximum of X printouts within Y seconds?

If you have access to JavaMail 1.5.5 or logging-mailhandler 1.5.5 you can use the com.sun.mail.util.logging.DurationFilter to limit the specific java.util.logging.Logger or java.util.logging.Handler.

What I'm looking for is a mechanism/API that could regulate the printouts to no more than x messages, per message code, during y seconds. For example, over the course of a minute, I don't want to generate and write to the log more than 10 messages of the type: "transaction timed out".

If the timeout message is only local to one logger then it is the same as above. Otherwise, you have to create a custom filter. As pointed out in other answers it is not to hard to compose filters with other filters.

Another option would be to create proxy handler that buffers and removes repeated messages within a given time window before publishing them to some other downstream handler.

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