带有 pax-logging 的 log4j2:不能使用来自 StructuredDataMessage 的值

log4j2 with pax-logging: can't use values from StructuredDataMessage

我正在使用 pax-logging-api 和 pax-logging-log4j2 从我的 OSGi 包进行日志记录。我想利用 Log4J2 的 StructuredDataMessage(使用 EventLogger)将一些消息写入数据库。但是,在使用 Pax Logging 时,我无法从附加程序读取我放入 StructuredDataMessage 中的值。

以下在直接使用 Log4J2 库的非 OSGi 项目中有效

log4j2.properties:

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %m%n

appender.event.type = Console
appender.event.name = event
appender.event.layout.type = PatternLayout
appender.event.layout.pattern = %marker ${sd:id} ${sd:testKey} %n    %m%n

rootLogger.level = debug
rootLogger.appenderRef.console.ref = STDOUT

logger.event.name = EventLogger
logger.event.level = debug
logger.event.appenderRef.console.ref = event
logger.event.additivity = false

Test.java:

public class Test {

    private static final Logger LOGGER = LogManager.getLogger(Test.class);

    public static void main(String[] args) {
        StructuredDataMessage msg = new StructuredDataMessage("1", "message", "event");
        msg.put("testKey", "testValue");

        LOGGER.info(msg);
        EventLogger.logEvent(msg);
    }
}

输出:

 1 testValue event [1 testKey="testValue"] message
EVENT 1 testValue 
    event [1 testKey="testValue"] message

请注意,event appender 正确地从 StructuredDataMessage 中取消引用 sd 键。

但是,以下 在带有 pax-logging 的 OSGi 中不起作用

org.ops4j.pax.logging.cfg:

log4j2.appender.console.type = Console
log4j2.appender.console.name = STDOUT
log4j2.appender.console.layout.type = PatternLayout
log4j2.appender.console.layout.pattern = %m%n

log4j2.appender.event.type = Console
log4j2.appender.event.name = event
log4j2.appender.event.layout.type = PatternLayout
log4j2.appender.event.layout.pattern = %marker $\\{sd:id\} $\\{sd:testKey\} %n    %m%n

log4j2.rootLogger.level = debug
log4j2.rootLogger.appenderRef.console.ref = STDOUT

log4j2.logger.event.name = EventLogger
log4j2.logger.event.level = debug
log4j2.logger.event.appenderRef.console.ref = event
log4j2.logger.event.additivity = false

Test.java:

public class Test implements BundleActivator {

    private static final Logger LOGGER = LogManager.getLogger(Test.class);

    @Override
    public void start(BundleContext context) throws Exception {
        StructuredDataMessage msg = new StructuredDataMessage("1", "message", "event");
        msg.put("testKey", "testValue");

        LOGGER.info(msg);
        EventLogger.logEvent(msg, Level.INFO);
    }

    @Override
    public void stop(BundleContext context) throws Exception {
    }
}

输出:

event [1 testKey="testValue"] message
EVENT ${sd:id} ${sd:testKey}
    event [1 testKey="testValue"] message

是否有技巧可以让它在 pax-logging 中工作?我可以在适用时使用 $\\{ctx:key\} 从 MDC 访问值,因此我假设语法相似。我也尝试过在 RoutingAppender、FileAppender 等模式中使用查找,但无济于事。

提前致谢!

编辑: 我正在使用最新版本的 pax-logging-api 和 pax-logging-log4j2 (1.11.3)

好吧,这还不是一个明确的答案——评论太短,无法描述发生的事情。

您调用的堆栈跟踪是:

"pipe-restart 238@10666" prio=5 tid=0xc3 nid=NA runnable
  java.lang.Thread.State: RUNNABLE
      at org.ops4j.pax.logging.log4j2.internal.PaxLoggerImpl.doLog0(PaxLoggerImpl.java:354)
      at org.ops4j.pax.logging.log4j2.internal.PaxLoggerImpl.doLog(PaxLoggerImpl.java:337)
      at org.ops4j.pax.logging.log4j2.internal.PaxLoggerImpl.inform(PaxLoggerImpl.java:233)
      at org.ops4j.pax.logging.internal.TrackingLogger.inform(TrackingLogger.java:209)
      at org.ops4j.pax.logging.log4jv2.Log4jv2Logger.logMessage(Log4jv2Logger.java:162)
      at org.apache.logging.log4j.spi.AbstractLogger.log(AbstractLogger.java:2102)
      at org.apache.logging.log4j.spi.AbstractLogger.tryLogMessage(AbstractLogger.java:2190)
      at org.apache.logging.log4j.spi.AbstractLogger.logMessageTrackRecursion(AbstractLogger.java:2144)
      at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2127)
      at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1828)
      at org.apache.logging.log4j.EventLogger.logEvent(EventLogger.java:56)
      at grgr.test.ActivatorLogging.start(ActivatorLogging.java:39)
...

org.ops4j.pax.logging.log4jv2.Log4jv2Logger.logMessage()logging facadelogging backend.

之间的桥梁

记住 - 使用 pax-logging,你可以说,将 Commons Logging facade 与 Log4J1 backend 或 Log4j2 [=56] 一起使用=]facade(这就是你正在做的)例如 Logback backend.

这就是 org.ops4j.pax.logging.log4jv2.Log4jv2Logger.logMessage() 这样做的原因:

} else if (level.intLevel() >= Level.INFO.intLevel()) {
    m_delegate.inform(paxMarker, message.getFormattedMessage(), t, fqcn);

并且您的结构化消息已更改为字符串 event [1 testKey="testValue"] message

只有这样配置的 appender 才会被调用 - 具有提取结构化数据的布局的 appender 找不到它,因为结构化消息已经转换为纯字符串。

这 3 行:

  at org.ops4j.pax.logging.log4j2.internal.PaxLoggerImpl.inform(PaxLoggerImpl.java:233)
  at org.ops4j.pax.logging.internal.TrackingLogger.inform(TrackingLogger.java:209)
  at org.ops4j.pax.logging.log4jv2.Log4jv2Logger.logMessage(Log4jv2Logger.java:162)

从 pax-logging-api(门面)到 TrackingLogger(桥)到 pax-logging-log4j2(后端)进行 交叉 丢失之间的结构化信息。

我已经创建了 https://ops4j1.jira.com/browse/PAXLOGGING-302 并希望尽快对此做点什么。

编辑1

关键是在org.apache.logging.log4j.core.lookup.StructuredDataLookup#lookup()中,这个条件为真:

if (event == null || !(event.getMessage() instanceof StructuredDataMessage)) {
    return null;
}

编辑2

我刚刚修复了 https://ops4j1.jira.com/browse/PAXLOGGING-302,这个测试证明它有效:

Logger logger = LogManager.getLogger("my.logger");

logger.info(new StructuredDataMessage("1", "hello!", "typeX").with("key1", "sd1"));
logger.info(new StringMapMessage().with("key1", "map1"));

List<String> lines = readLines();
assertTrue(lines.contains("my.logger/org.ops4j.pax.logging.it.Log4J2MessagesIntegrationTest typeX/sd1 [INFO] typeX [1 key1=\"sd1\"] hello!"));
assertTrue(lines.contains("my.logger/org.ops4j.pax.logging.it.Log4J2MessagesIntegrationTest ${sd:type}/map1 [INFO] key1=\"map1\""));

配置为:

log4j2.appender.console.type = Console
log4j2.appender.console.name = console
log4j2.appender.console.layout.type = PatternLayout
log4j2.appender.console.layout.pattern = %c/%C ${sd:type}/${map:key1} [%p] %m%n
log4j2.rootLogger.level = info
log4j2.rootLogger.appenderRef.file.ref = console

(如果通过 Karaf 中的 etc/org.ops4j.pax.logging.cfg 配置,则必须使用您使用过的转义序列)。