根据发生率,在运行时更改日志级别的方法是什么
我想知道是否有常规方式(可能是一些第三方库)可以帮助我解决以下问题。我希望有能力根据发生率更改日志级别。例如,如果我发现一些例外,并且发生一次,我想将其记录下来。但是,如果速率较高,那么我想切换到误差级别。这种行为的目的是不要因罕见的零星例外而分心,这些例外通常是不可避免的,除非有足够的数量,否则不应引起开发人员的注意。当我打开Sentry摘要页面时,我只想看到真正重要的内容并需要关注的内容,而不是不可避免地会发生的罕见请求失败。但是与此同时,我不能仅仅使用警告级别,因为如果有很多这样的失败,那么显然会发生一些不好的事情,并且需要注意。
为了更好地说明我的意思是我认为如何使用假想的第三方库来模仿(或成为包装纸)SLF4J/Commons-Logging:
LOG.warn("Failed to send request to ...", e).onRate(Rates.hourlyMoreThan(5)).switchTo(LogLevel.ERROR);
替代方案是Sentry的选择,而不是显示事件(符合某些标准,例如例外类别),但据我所知,这是不可能的。
I would like to know whether there are conventional ways (probably some third-party libraries) to help me with the following problem. I would like to have an ability to change log level depending on occurrence rate. For example, if I catch some exception and it happens say once an hour I would like to log it with the WARN level. But if the rate becomes higher then I would like to switch to the ERROR level. The purpose of this behavior is to not get distracted by rare sporadic exceptions that are inevitable in general but shouldn't be brought to developers' attention unless there is a sufficient amount of them. When I open Sentry summary page I would like to see only what's really important and requires attention, not some rare request failures that will inevitably happen from time to time. But at the same time I can't just use WARN level because if there are many such failures then there's clearly something bad going on and it requires attention.
To better illustrate what I mean here's how in my opinion that could be done with an imaginary third-party library that would imitate (or be a wrapper over) slf4j/commons-logging:
LOG.warn("Failed to send request to ...", e).onRate(Rates.hourlyMoreThan(5)).switchTo(LogLevel.ERROR);
An alternative would be an option in Sentry not to show events (matching some criteria like a class of the exception) with a low occurrence rate but as far as I know that's not possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您想使用log4j执行此操作(其他记录框架应该具有相似的结构),则可以使用自定义的应用程序来完成。如果该Appender看到一条消息太多次,它可以尝试更改类别的(或记录器)级别。
这是您可以创建自己的appender的方式:
https://logging.apache.opache.org/log/log4j/2.x/手册/延伸
。
https://logging.apache.apache.org/log/log/log4j/2.x/手册/customconfig.html
Assuming you want to do this with Log4j (other logging frameworks should have similar structures), this can probably be done with a custom appender. If that appender sees one message too many times, it could try to change the category's (or the logger's) level.
Here is how you could create your own Appender:
https://logging.apache.org/log4j/2.x/manual/extending.html
Here is what the appender would have to do to reconfigure the logging:
https://logging.apache.org/log4j/2.x/manual/customconfig.html
不确定是否可以在sentry UI中过滤,但是您肯定会根据 sentryappender 它将仅在达到某些阈值时衡量事件并发射事件。
Not sure if it is possible to filter in Sentry UI, but you an definitely create a custom Sentry appender based on SentryAppender which will measure the occurrence and emit event to Sentry only when it reaches certain threshold.