Custom Converter with log4j2 and Mule not working

555 Views Asked by At

I'm trying to create a custom converter for log4j in a mule application. I want to put an '%foo' into a pattern layout and define that output in a java class.

My converter class looks like this:

package my.package 

@Plugin(name = "MyConverter", category = "Converter")
@ConverterKeys(["foo"])
public final class MyConverter extends LogEventPatternConverter {

    protected MyConverter(String name, String style) {
        super(name, style)
    }

    public static MyConverter newInstance(final String[] options) {
      return new MyConverter("foo", "foo");
    }

    @Override
    public void format(LogEvent arg0, StringBuilder arg1) {
        arg1.append("hello")
    }
}

My log4j2.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration verbose="true" packages="my.package">

    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%-5p [%t] %c: %m%n"/>
        </Console>
        <File name="mylog" fileName="${env:MULE_HOME}/logs/mylog.log">
            <PatternLayout pattern="%-4p %d [%t] [%foo] %c: %m%n"/>
        </File>
    </Appenders>

...

All i get, whenever i start my mule application is this in the mule.log file

ERROR Unrecognized format specifier [foo]
1

There are 1 best solutions below

1
On

You could instead use the MDC class (Mapped Diagnostic Context) of log4j. See [here] (https://blog.oio.de/2010/11/09/logging-additional-information-like-sessionid-in-every-log4j-message/) for a concise example.