我正在使用pax-logging-api和pax-logging-log4j2从我的OSGi捆绑包中进行日志记录。我想利用Log4J2的StructuredDataMessage(使用EventLogger)将一些消息写入数据库。但是,使用Pax日志记录时,我无法从附加程序读取我放入StructuredDataMessage的值。
直接使用Log4J2库的非OSGi项目中的以下works:
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
附加程序正确地从StructuredDataMessage中取消了sd
键的引用。
但是,在带有pax记录的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记录中起作用的技巧?我可以在适用时使用\$\\\{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()
是日志外观和日志后端之间的桥梁。
记住-使用pax记录,您可以说将Commons Logging facade与Log4J1 backend或Log4j2 facade(这就是您正在做的)一起使用,例如Logback 后端。
这就是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
。
然后仅调用已配置的附加程序-且具有提取结构化数据的布局的附加程序找不到它,因为结构化消息已转换为纯字符串。
这三行:
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(后端)进行crossing,这之间丢失了结构化信息。
我创建了https://ops4j1.jira.com/browse/PAXLOGGING-302,希望很快能对此有所帮助。
EDIT1
关键是在org.apache.logging.log4j.core.lookup.StructuredDataLookup#lookup()
中,此条件为真:
if (event == null || !(event.getMessage() instanceof StructuredDataMessage)) {
return null;
}