log4j2 configuration will not load custom pattern converter

后端 未结 2 1101
栀梦
栀梦 2020-12-11 19:04

I am trying to create a custom pattern converter for log4j 2.0, but am having issues getting my log4j configuration to recognize the pattern. Here is the custom converter:<

相关标签:
2条回答
  • 2020-12-11 19:23

    To make a long story short, since version 2.0-rc2, the packages attribute does not work any more. Instead, there is an annotation processor in log4j-core that will (should?) run during the build and will generate a metadata file for your custom plugins in your jar file. When log4j2 starts it will look for the metadata file in all jar files and quickly discover all available plugins.

    The annotation processor works when compiling with Maven or with plain javac, but it may not work well when compiling in Eclipse. You need to enable annotation processing with right-click on a project > Properties > Java Compiler > Annotation Processing.

    Still, I am not 100% sure this will work from Eclipse. For now, one alternative is to build your custom plugin with Maven.

    I will try to make the packages attribute work again in an upcoming release.

    This issue is tracked here: https://issues.apache.org/jira/browse/LOG4J2-741

    UPDATE (7/28) This is fixed in trunk and will be included in the 2.0.1 release.

    0 讨论(0)
  • 2020-12-11 19:27

    I spent a couple of hours struggling with this one as well...

    Apparently, it's all about class loading!

    Use this trick:

    Run your app using -verbose:class (it will print all the classes loaded by the JVM by their order)

    e.g.

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

    Search for the log4j2 converter class; in your case, it should look like this:

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

    You'll probably note that the converter class is loaded relatively late (I assume after the log4j2 plugins are supposed to get loaded).

    To fix this, you can try one of the following:

    1. Load the converter class explicitly, i.e. Class.forName(...)
    2. Change the converter class location: put it next to your main class or underneath it (in terms of package hierarchy)

    I tried both of these options - the second one worked for me!

    Goodluck

    0 讨论(0)
提交回复
热议问题