简体   繁体   English

Spring Boot中触发log4j2的logger.error()时触发自定义方法

[英]Trigger custom method when log4j2's logger.error() is triggered in Spring Boot

I am using Spring Boot with log4j2 - and I'd like to trigger a custom method in a '@Service' class when logger.error(...) is triggered.我正在使用带有 log4j2 的 Spring Boot - 当logger.error(...)被触发时,我想在“@Service”类中触发一个自定义方法。

For example,例如,

@Service
public class Foo {
    private final Logger logger = LogManager.getLogger(getClass());
    ...
    public void doSomething() {
        try {
            ...
        }
        catch (Exception e) {
            logger.error("Error!", e); // When `error` is triggered...
        }
    }
}

// Other class
@Service
public class Bar {
    @Autowired private NotificationService notificationService;

    public void triggeredOnError() { // I'd like to trigger this method
        this.notificationService.notifySomething();
    }
}

I'd like to know this is possible in log4j2 with Spring Boot.我想知道这在带有 Spring Boot 的 log4j2 中是可能的。 The thing is, I just want to trigger the default method logger.error(...) since I don't want to change the default behavior of log4j2 .问题是,我只想触发默认方法logger.error(...)因为我不想更改 log4j2 的默认行为 I researched a bit - and filter or adapter might be the solution here, but I am not really sure how to achieve this.我研究了一下 - filteradapter可能是这里的解决方案,但我不确定如何实现这一点。 Please help me out!请帮帮我!

While an appender would work as Mark suggests, I would implement a Filter.虽然 appender 会像 Mark 建议的那样工作,但我会实现一个过滤器。 A Filter can be placed in four different locations in Log4j 2 and has the option of forcing the log event to be logged, forcing it to not be logged or just continue on with the normal evaluation of whether it should be logged.过滤器可以放置在 Log4j 2 中的四个不同位置,并且可以选择强制记录日志事件、强制不记录它或继续正常评估是否应该记录它。 But a filter can always be configured with onMatch=NEUTRAL and onMismatch=NEUTRAL so that it really has no effect on whether the log event is processed but allows some other processing to take place.但是过滤器总是可以用 onMatch=NEUTRAL 和 onMismatch=NEUTRAL 来配置,这样它对是否处理日志事件没有影响,但允许其他一些处理发生。 In addition, Filters are much easier to write than an Appender.此外,过滤器比 Appender 更容易编写。

You can find a sample Filter at http://logging.apache.org/log4j/2.x/manual/extending.html#Filters您可以在http://logging.apache.org/log4j/2.x/manual/extending.html#Filters找到示例过滤器

What you should not do in a Filter though, is use it as a way to write the log event to some destination.但是,您不应该在过滤器中做的是将其用作将日志事件写入某个目的地的一种方式。 That is exactly what Appenders are for.这正是 Appenders 的用途。

IMO the easiest way to achieve that is to create a special appender and in Log4j2 configuration associate it with a logger of your choice (or maybe with all the loggers if you want a “global” configuration). IMO 实现这一目标的最简单方法是创建一个特殊的 appender,并在 Log4j2 配置中将其与您选择的记录器相关联(或者,如果您想要“全局”配置,则可能与所有记录器相关联)。

Then you could use an “appender filter” to make an appender called only if its an error message.然后,您可以使用“appender 过滤器”来调用仅在出现错误消息时才调用的 appender。

The only potential issue is contacting the spring bean from log4j2 appender.唯一的潜在问题是从 log4j2 appender 联系 spring bean。 Read this SO thread to understand how technically you can achieve that.阅读此 SO 线程以了解您如何在技术上实现这一目标。

The benefit of this method is that you don't change the framework but instead leverage the configuration options that it already provides.这种方法的好处是您无需更改框架,而是利用它已经提供的配置选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM