简体   繁体   中英

EventData message lost with log4j2 as SLF4J backend?

I want to use SLF4J-ext EventData and EventLogger to log important events in my business-logic:

public void onMessage(Message message) {
    EventData messageEvent = new EventData();
    messageEvent.setMessage("Message arrived");
    messageEvent.put("messageID", message.getJMSMessageID());
    EventLogger.logEvent(messageEvent);
}

I also want to use log4j2 as a backend. Currently, this leads to the following artifacts being part of my deployment: slf4j-api-1.7.12, slf4j-ext-1.7.12, log4j-api-2.3, log4j-core-2.3, log4j-slf4j-impl-2.3, log4j-web-2.3 - as well as their dependencies, of course.

My log4j2-configuration is intended to produce a formatted entry in regular files:

[...]
<Appenders>
    <RollingFile name="traceFileAppender"
        fileName="${logBase}/trace.log"
        filePattern="${logBase}/trace.log.%d{yyyy-MM-dd}">
        <TimeBasedTriggeringPolicy interval="1" modulate="true" />
        <DefaultRolloverStrategy max="7"/>
        <PatternLayout pattern="%date{ISO8601}[%thread] %level %marker %logger%nThread-Stack: %x%nThread-Map: %X%nMessage-Map: %K%nMessage: %m%n%rEx-- %n%n" />
        <ThresholdFilter level="TRACE" onMatch="ACCEPT" onMismatch="DENY" />
    </RollingFile>
    [...]
</Appenders>
<Loggers>
    [...]
    <Logger name="EventLogger" level="INFO" additivity="true" />
    [...]
    <Root level="TRACE">
        <AppenderRef ref="traceFileAppender" />
        [...]
    </Root>
</Loggers>
[...]

However, my log-statements look like this:

2015-07-31T08:13:55,589[MessageListenerThreadPool : 0] INFO EVENT EventLogger
Thread-Stack: []
Thread-Map: {}
Message-Map: {messageID=ID:c3e2d840d4d8d4f14040404040404040cf522d11eb57d22b}
Message: 
-- 

What am I missing?

The log4j-slf4j-impl module converts an slf4j EventData object to a log4j2 StructuredMessage. The output shows the message ID, proving that this conversion was successful.

However, the output does not show the message. Looks like this is due to the StructuredMessage.toString() implementation. At first glance it looks like it may not be possible to customize this by specifying a key for the message part with the %K{key} conversion specifier... :-(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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