Logback:在ERROR级别日志上执行自定义操作

huangapple go评论70阅读模式
英文:

Logback: perform custom action on ERROR level log

问题

使用logback,我正在寻找一种方法,在发生一定数量的错误日志时执行自定义操作。过去,使用log4j,我可以通过子类化某些日志记录器基类或实现某些日志记录器接口(我不记得确切的方式)来实现这一点。在我的logback搜索中,我还没有找到相同的功能。

有没有人知道一种方法(我有一个Spring Boot应用程序)可以做到这一点?基本上,我想捕获所有错误日志的字符串消息,搜索特定文本,然后对它们进行计数,当达到阈值时,发布一个自定义的Prometheus指标。

只是寻找要子类化/实现的内容,不是完整的解决方案。

谢谢

英文:

With logback, I am looking for a way to perform a custom action every time a certain number of ERROR logs occurs. In the past, with log4j, I was able to do this by either subclassing some logger base class or implementing some logger interface (I can't remember exactly). I haven't been able to find the same functionality with my searches for logback.

Does anyone know a way (I have a spring boot application) to do this? Basically I want to capture the string message of all ERROR logs, search for specific text, and then count them and when a threshold is reached, publish a custom Prometheus metric.

Just looking for the thing to subclass/implement here, not a full solution.

Thank you

答案1

得分: 0

Logback中可能有很多选项,但扩展一个appender效果很好。类似于ch.qos.logback.classic.AsyncAppender,它实现了ch.qos.logback.core.spi.AppenderAttachable

我做了这个,不打算贴出整个解决方案,因为你不需要,但你可以看看AsyncAppender,我基本上做了同样的事情:

class MyOwnAppender extends UnsynchronizedAppenderBase<ILoggingEvent> implements AppenderAttachable<ILoggingEvent>

英文:

Probably many options in Logback, but extending an appender works nice. Something like ch.qos.logback.classic.AsyncAppender that implements ch.qos.logback.core.spi.AppenderAttachable.

I did this, not gonna post the whole solution since you don't want that, but you can look at the AsyncAppender, I basically did the same thing:

class MyOwnAppender extends UnsynchronizedAppenderBase<ILoggingEvent> implements AppenderAttachable<ILoggingEvent>

huangapple
  • 本文由 发表于 2020年8月4日 23:12:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63249863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定