MDC in custom log formatter using JBoss LogManager

563 Views Asked by At

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.

0

There are 0 best solutions below