简体   繁体   English

有没有一种方法可以在Log4j2中使用RoutingAppender来基于Marker路由日志

[英]Is there a way to Route logs based on Marker with the RoutingAppender in Log4j2

It is possible to filter messages using markers, such as : 可以使用标记来过滤消息,例如:

      <MarkerFilter marker="FLOW" onMatch="ACCEPT" onMismatch="DENY"/>

However I'm trying to route a message based on the marker using the RoutingAppender . 但是,我正在尝试使用RoutingAppender基于标记路由消息。 I don't want to filter the same arguments multiple times in multiple Appenders. 我不想在多个Appender中多次过滤相同的参数。 Here's my configuration sample (yaml): 这是我的配置示例(yaml):

Routing:
  name: ROUTING_APPENDER
  Routes:
    pattern: "$${ctx:marker}" #<-- How to use Marker here?
    Route:
      - key: MyRoutingKey
        ref: MyCustomAppender

The documentation stipulates : 该文件规定:

The pattern is evaluated against all the registered Lookups and the result is used to select a Route 针对所有已注册的查找评估模式,结果用于选择路线

However there seems to be no Lookup for Markers, same for LogLevel. 但是,似乎没有对标记的查找 ,对LogLevel也是如此。 It would be possible to add a custom MarkerValue or LogLevelValue in the ThreadContextMap but I don't find the solution really efficient, it duplicates known information. 可以在ThreadContextMap中添加自定义MarkerValueLogLevelValue ,但是我发现该解决方案并不十分有效,它会复制已知信息。

Is it not documented or just impossible? 它没有记载或只是不可能? Should there be a built-in way to have access to those values in Lookup? 是否应该有一种内置的方法来访问Lookup中的那些值?

The documentation for RoutingAppender shows the ThreadContext lookup, but routing can also work with other lookups. RoutingAppender的文档显示了ThreadContext查找,但是路由也可以与其他查找一起使用。 One idea is to create a custom lookup. 一种想法是创建自定义查找。

A custom lookup is implemented as a log4j2 plugin. 自定义查找被实现为log4j2插件。 To help log4j2 find your plugin you can enable packages="yourCustomPackage" in your configuration file. 为了帮助log4j2找到您的插件,您可以在配置文件中启用packages =“ yourCustomPackage” Your plugin class needs to be on the classpath so log4j can find it. 您的插件类必须位于类路径上,以便log4j可以找到它。 Here's the plugin code for the custom lookup: 这是自定义查找的插件代码:

import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.lookup.StrLookup;

@Plugin(name = "marker", category = "Lookup")
public class MarkerLookup implements StrLookup {

    public String lookup(String key) {
        return null
    }

    public String lookup(LogEvent event, String key) {
        final Marker marker = event.getMarker();
        return marker == null ? null : marker.getName();
    }
}

And in the configuration file : 并在配置文件中:

Routing:
  name: ROUTING_APPENDER
  Routes:
    pattern: "$${marker:}"
    Route:
    - key: PERFORMANCE
      ref: PERFORMANCE_APPENDER
    - key: PAYLOAD
      ref: PAYLOAD_APPENDER
    - key: FATAL
      ref: FATAL_APPENDER
    - ref: APPLICATION_APPENDER #Default route

Credits to the Log4j2 developers ( https://issues.apache.org/jira/browse/LOG4J2-1015 ). 感谢Log4j2开发人员( https://issues.apache.org/jira/browse/LOG4J2-1015 )。

UPDATE : According to them, it should be built-in in the next version (2.4). 更新 :据他们说,它应该内置在下一版本(2.4)中。 So no needs to write custom plugin after that. 因此,此后无需编写自定义插件。

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

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