0

Using JBoss LogManager, it is possible to format the logs. If i use for example the json log formatter, i get the following output, in which the mdc map contains my custom value:

{
    "timestamp": "2018-10-18T13:53:43.031-04:00",
    "sequence": 62,
    "loggerClassName": "org.jboss.as.server.logging.ServerLogger_$logger",
    "loggerName": "org.jboss.as",
    "level": "INFO",
    "message": "WFLYSRV0025: JBoss EAP 7.3.0.GA (WildFly Core 10.0.0.Final-redhat-20190924) started in 5672ms - Started 495 of 679 services (331 services are lazy, passive or on-demand)",
    "threadName": "Controller Boot Thread",
    "threadId": 22,
    "mdc": {
      "mycustommdcvalue": "123"
    },
    "ndc": "",
    "hostName": "localhost.localdomain",
    "processName": "jboss-modules.jar",
    "processId": 7461
}

But when i use a custom formatter, the MDC values are not present:

import java.util.logging.Formatter;
import java.util.logging.LogRecord;

public class MyFormatter extends Formatter
{

    @Override
    public String format(final LogRecord record) {
        return format(ExtLogRecord.wrap(record));
    }

    public String format(ExtLogRecord record) { 
       //return record.getMdcCopy();<-emptyMap       
       return record.getMdc("mycustommdcvalue");//<-empty
    }
}

I inspected the JSON-Formatter and StructuredFormatter, but can't see how they achieved to make MDC work.

In order to inspect the problem, i didn't use my custom formatter at all, but simply used the jar jboss-logmanager-2.1.14.Final-redhat-00001.jar in which the json log formatter is located with the following module.xml:

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="com.company.projectname.logging">
        <resources>
                <resource-root path="jboss-logmanager-2.1.14.Final-redhat-00001.jar"/>
        </resources>
    <dependencies>
        <!-- for java.beans -->
        <module name="java.desktop"/>
        <module name="java.logging"/>
        <module name="java.xml"/>
        <module name="javax.json.api"/>
        <module name="org.jboss.modules"/>
        <module name="org.wildfly.common"/>
    </dependencies>
</module>

Still, mdc is always empty.

peer
  • 257
  • 2
  • 11
  • It’s done right here https://github.com/jboss-logging/jboss-logmanager/blob/1de3c91785a54f0a9431e452a12fc39a02387e25/src/main/java/org/jboss/logmanager/formatters/StructuredFormatter.java#L223. You need to take the MDC map off the record. – James R. Perkins Oct 19 '22 at 04:08
  • @JamesR.Perkins Thanks, but i saw the call `record.getMdcCopy()`, but somehow it is still an empty map. – peer Oct 19 '22 at 07:37
  • I updated the question. In fact, MDC is an empty map when i don't use my custom formatter at all but simply use the jar `jboss-logmanager-2.1.14.Final-redhat-00001.jar` in which `JsonFormatter` is located. – peer Oct 19 '22 at 09:23
  • Are you adding things to the MDC in the same thread? – James R. Perkins Oct 19 '22 at 13:31

0 Answers0