1

I'm trying to set layout in log4j2 to XMLLayout.

I included in POM jackson-annotations, jackson-module-jaxb-annotations, jersey-media-json-jackson. Also when I log with PatternLayout to console or to file everything works. When I want to use XMLLayout I get exception as below. Additionally it is almost the same exception which is in this thread: Log4j2 SyslogAppender not working, but the solution is only for the Appender, and I don't think that it works, because there is no mdcId attribute.

XML

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
    <Console name="console" target="SYSTEM_OUT">
         <XMLLayout />
        <!--PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/-->
    </Console>
    <!--Flume name="EventLogger" compress="true" type="Embedded">
        <Agent host="192.168.1.50" port="41414"/>
        <RFC5424Layout enterpriseNumber="18060" includeMDC="true" appName="MyApp"/>
    </Flume-->
    <File name="MyFile" fileName="logs/app.log">
        <!--XMLLayout /-->

    </File>
</Appenders>

<Loggers>
    <Logger name="EventLogger" level="warn">
        <AppenderRef ref="console"/>
    </Logger>
    <Root level="warn" additivity="false">
        <AppenderRef ref="MyFile"/>
    </Root>
</Loggers>
</Configuration>

Exception

2014-11-26 09:22:45,975 ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.layout.XmlLayout for element XMLLayout. java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:135)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:766)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:706)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:698)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:698)
at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:358)
Remko Popma
  • 35,130
  • 11
  • 92
  • 114
h__
  • 761
  • 3
  • 12
  • 41

2 Answers2

1

You may be missing a dependency for Jackson. Did you check the Dependency Tree? http://logging.apache.org/log4j/2.x/log4j-core/dependencies.html#Dependency_Tree

You may need all of these:

  • com.fasterxml.jackson.core:jackson-core:jar:2.4.2
  • com.fasterxml.jackson.core:jackson-databind:jar:2.4.2
  • com.fasterxml.jackson.core:jackson-annotations:jar:2.4.0
  • com.fasterxml.jackson.dataformat:jackson-dataformat-xml:jar:2.4.2
  • com.fasterxml.jackson.module:jackson-module-jaxb-annotations:jar:2.4.2
  • org.codehaus.woodstox:stax2-api:jar:3.1.4
Remko Popma
  • 35,130
  • 11
  • 92
  • 114
  • I will check it on Monday. Jackson is for XMLLayout, but I get such error also for FlumeAppender (general the same content of exception but the Flume instead of XMLLayout) – h__ Nov 28 '14 at 21:44
  • OK, let's assume it helped. I don't really know if it helped, because now I get. 2014-12-05 11:06:07,891 ERROR com.fasterxml.jackson.databind.JsonMappingException: Not implemented (through reference chain: org.apache.logging.log4j.core.impl.Log4jLogEvent["Message"]) com.fasterxml.jackson.databind.JsonMappingException: Not implemented (through reference chain: org.apache.logging.log4j.core.impl.Log4jLogEvent["Message"]) similiar to https://issues.apache.org/jira/browse/LOG4J2-739 – h__ Dec 05 '14 at 10:17
  • Thank you! This looks like a problem in Jackson... Did you use the versions I mentioned? If so, would it be possible to try an older version of Jackson? – Remko Popma Dec 06 '14 at 23:18
  • Yes, I used exact these versions. With previous one versions results I will give feedback tomorrow. – h__ Dec 07 '14 at 14:49
  • Still the same, I tried different previous versions. I also tried version from thread from issues.apache.org/jira/browse/LOG4J2-739 but it didn't help. – h__ Dec 08 '14 at 10:52
0

Add woodstox-core-5.0.3.jar:

for example this is my classpath:

JavaSE-1.8
jackson-annotations-2.7.0.jar
jackson-core-2.7.0.jar
jackson-databind-2.7.0.jar
jackson-dataformat-xml-2.1.1.jar
log4j-api-2.5.jar
log4j-core-2.5.jar
hamcrest-core-1.3.jar
junit-4.12.jar
stax2-api-3.1.4.jar
woodstox-core-5.0.3.jar

and XMLLayout works.