java logback:每当写入新日志时我可以附加自定义函数吗
我有一个使用 java 语言的 red5 应用程序,我使用 logback 进行日志记录。
每当使用 logback 写入日志时,我想将其发送给用户的消息。有没有办法以某种方式附加到记录器类,以便能够在发送日志时执行另一个命令?
I have a red5 application using java language and I use logback for logging.
whenever a log is written using logback i want to send it to the message to the user. is there a way to somehow attach to the logger class to be able to do another command when a log is being sent ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当 Logback(但这个想法也适用于 Log4J)决定记录给定的语句(基于日志记录级别和过滤器)时,他将
ILoggingEvent
发送到每个附加的所谓的 appender给定的记录器及其父母。你想要的是实现你自己的appender(实现 Appender 或最好扩展 AppenderBase) 并将其添加到您的
logback.xml
(简化):如您所见,它非常简单,但是在编写自己的附加程序之前,请确保类似的附加程序尚未存在。
When Logback (but the idea applies to Log4J as well) decides to log given statement (based on logging levels and filters), he sends the
ILoggingEvent
to every so called appender attached to a given logger and its parents.What you want is to implement your own appender (class implementing Appender or preferably extending AppenderBase) and add it to your
logback.xml
(simplified):As you can see it is very simple, but before you write your own appender make sure that a similar appender doesn't already exist.
您必须创建自定义附加程序: http://logback.qos.ch/manual/appenders.html
只需根据需要实现其
doAppend()
即可。例如向用户发送消息。如果您想运行“另一个命令”,您应该使用类似的解决方案。
You have to create your custom appender: http://logback.qos.ch/manual/appenders.html
Just implement its
doAppend()
as you want. For example send message to user.If you want to run "another command" you should use similar solution.