Log4j2 configuration will not load custom template converter

I am trying to create my own template converter for log4j 2.0, but I am having problems getting my log4j configuration to recognize the template. Here is a custom converter:

package com.test.log4j.plugins;

import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.pattern.ConverterKeys;
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;

@Plugin(name="MarkerNamePatternConverter", category="Converter")
@ConverterKeys({"markername"})
public class MarkerNamePatternConverter extends LogEventPatternConverter {

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

    protected MarkerNamePatternConverter(String name, String style) {
        super(name, style);
    }

    @Override
    public void format(LogEvent event, StringBuilder toAppendTo) {
        Marker marker = event.getMarker();
        if (marker != null) {
            // MarkerPatternConverter appends Marker.toString()
            // which includes the parents of the marker.  We just
            // want the marker name
            toAppendTo.append(marker.getName());
        }
    }
}

And here is my log4j configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace" packages="com.test.log4j.plugins">
  <Appenders>
    <Console name="console" target="SYSTEM_OUT">
      <PatternLayout pattern=" %-6level %markername  %d{YYYY-MM-dd HH:mm:ss}  %msg%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Root level="debug">
      <AppenderRef ref="console"/>
    </Root>
  </Loggers>
</Configuration>

Please note that I have included a package that contains a custom converter in my configuration, the absence of which seems to be a common cause of problems when using custom plugins.

When the test code is executed, the output of the configuration state does not indicate that the plug-in is loaded or found, and log4j processes the name% as if it were a conversion template "% marker" followed by a "name". For instance,

Marker marker = MarkerManager.getMarker("TEST");
log.info(marker, "test message");

outputs the following result:

INFO   TESTname  2014-07-23 14:47:57  test message

, , . "fmarker", log4j :

2014-07-23 14:44:55,814 ERROR Unrecognized format specifier [fmarker]
2014-07-23 14:44:55,816 ERROR Unrecognized conversion specifier [fmarker] starting at     position 18 in conversion pattern.
INFO   %fmarker  2014-07-23 14:44:55  test message

, , , , , - , , :

  • @Plugin @ConverterKeys
  • ""
  • newInstance (String [])
  • ""
  • "packages".

Maven, - Java-, Eclipse.

, ?

+4
2

, 2.0-rc2, . log4j-core , (?) jar. log4j2 , jar .

Maven javac, Eclipse . , > a > Java > .

, 100%, Eclipse. Maven.

.

: https://issues.apache.org/jira/browse/LOG4J2-741

UPDATE (7/28) 2.0.1.

+2

...

-, !

:

-verbose: class ( , JVM )

.

java -verbose:class -cp <your_jar> <your_main_class> ...

log4j2; :

[Loaded com.test.log4j.plugins.MarkerNamePatternConverter from jar:file: ...]     

, , ( , , log4j2 ).

, :

  • , Class.forName(...)
  • : ( ).

- !

0

All Articles