I am trying to log in JSON format through log4j2.
This is my log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" name="MyApp" packages="org.apache.logging.log4j.core.layout.JSONLayout">
<Appenders>
<RollingRandomAccessFile name="RollingRandomAccessFile" fileName="logs/app3.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz" append="false">
<JsonLayout/>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="RollingRandomAccessFile"/>
</Root>
</Loggers>
</Configuration>
These are the dependencies I have used:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>net.logstash.log4j</groupId>
<artifactId>jsonevent-layout</artifactId>
<version>1.7</version>
</dependency><dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
When I run the program I get an error as follows:
543 main ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.layout.JsonLayout for element JsonLayout.
Debug:
2016-05-09 16:06:04,909 main DEBUG Initializing configuration XmlConfiguration[location=/home/mrunal/repo/jigsaw/jigsaw/jigsaw-solution/jigsaw-matchmanager-business/target/classes/log4j2.xml]
2016-05-09 16:06:04,916 main DEBUG Installed script engines
2016-05-09 16:06:05,717 main DEBUG Oracle Nashorn Version: 1.8.0_91, Language: ECMAScript, Threading: Not Thread Safe, Compile: true, Names: {nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript}
2016-05-09 16:06:05,823 main DEBUG Took 0.104995 seconds to load 14 plugins from package org.apache.logging.log4j.core.layout
2016-05-09 16:06:05,824 main DEBUG PluginManager 'Core' found 97 plugins
2016-05-09 16:06:05,824 main DEBUG PluginManager 'Level' found 0 plugins
2016-05-09 16:06:05,831 main DEBUG No scheduled items
2016-05-09 16:06:05,835 main DEBUG PluginManager 'Lookup' found 13 plugins
2016-05-09 16:06:05,837 main DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.JsonLayout].
2016-05-09 16:06:05,853 main DEBUG PluginManager 'TypeConverter' found 23 plugins
2016-05-09 16:06:05,863 main DEBUG createLayout(locationInfo="false", properties="false", complete="false", compact="false", eventEol="false", charset="UTF-8")
2016-05-09 16:06:05,864 main ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.layout.JsonLayout for element JSONLayout.
2016-05-09 16:06:05,865 main DEBUG Building Plugin[name=TimeBasedTriggeringPolicy, class=org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy].
2016-05-09 16:06:05,867 main DEBUG createPolicy(interval="null", modulate="null")
2016-05-09 16:06:05,868 main DEBUG Building Plugin[name=SizeBasedTriggeringPolicy, class=org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy].
2016-05-09 16:06:05,869 main DEBUG createPolicy(size="250 MB")
2016-05-09 16:06:05,872 main DEBUG Building Plugin[name=Policies, class=org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy].
2016-05-09 16:06:05,882 main DEBUG createPolicy(={TimeBasedTriggeringPolicy(nextRolloverMillis=0, interval=1, modulate=false), SizeBasedTriggeringPolicy(size=262144000)})
2016-05-09 16:06:05,882 main DEBUG Building Plugin[name=DefaultRolloverStrategy, class=org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy].
2016-05-09 16:06:05,887 main DEBUG createStrategy(max="20", min="null", fileIndex="null", compressionLevel="null", ={}, stopCustomActionsOnError="true", Configuration(/home/mrunal/repo/jigsaw/jigsaw/jigsaw-solution/jigsaw-matchmanager-business/target/classes/log4j2.xml))
2016-05-09 16:06:05,888 main DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender].
2016-05-09 16:06:05,892 main DEBUG createAppender(fileName="logs/app3.log", filePattern="logs/${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz", append="false", name="RollingRandomAccessFile", immediateFlush="null", bufferSize="null", Policies(CompositeTriggeringPolicy(policies=[TimeBasedTriggeringPolicy(nextRolloverMillis=0, interval=1, modulate=false), SizeBasedTriggeringPolicy(size=262144000)])), DefaultRolloverStrategy(DefaultRolloverStrategy(min=1, max=20)), JSONLayout(null), Filter=null, ignoreExceptions="null", advertise="null", advertiseURI="null", Configuration(/home/mrunal/repo/jigsaw/jigsaw/jigsaw-solution/jigsaw-matchmanager-business/target/classes/log4j2.xml))
2016-05-09 16:06:05,893 main INFO Log4j appears to be running in a Servlet environment, but there's no log4j-web module available. If you want better web container support, please add the log4j-web JAR to your web archive or server lib directory.
2016-05-09 16:06:05,894 main DEBUG PluginManager 'Converter' found 38 plugins
2016-05-09 16:06:05,895 main DEBUG Starting OutputStreamManager SYSTEM_OUT.false-2
2016-05-09 16:06:05,909 main DEBUG Starting RollingRandomAccessFileManager logs/app3.log
2016-05-09 16:06:05,914 main DEBUG PluginManager 'FileConverter' found 2 plugins
2016-05-09 16:06:05,935 main DEBUG Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin].
2016-05-09 16:06:05,936 main DEBUG createAppenders(={RollingRandomAccessFile})
2016-05-09 16:06:05,936 main DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
2016-05-09 16:06:05,937 main DEBUG createAppenderRef(ref="RollingRandomAccessFile", level="null", Filter=null)
2016-05-09 16:06:05,937 main DEBUG Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger].
2016-05-09 16:06:05,938 main DEBUG createLogger(additivity="null", level="TRACE", includeLocation="null", ={RollingRandomAccessFile}, ={}, Configuration(/home/mrunal/repo/jigsaw/jigsaw/jigsaw-solution/jigsaw-matchmanager-business/target/classes/log4j2.xml), Filter=null)
2016-05-09 16:06:05,941 main DEBUG Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin].
2016-05-09 16:06:05,943 main DEBUG createLoggers(={root})
2016-05-09 16:06:05,944 main DEBUG Configuration XmlConfiguration[location=/home/mrunal/repo/jigsaw/jigsaw/jigsaw-solution/jigsaw-matchmanager-business/target/classes/log4j2.xml] initialized
2016-05-09 16:06:05,952 main DEBUG Starting configuration XmlConfiguration[location=/home/mrunal/repo/jigsaw/jigsaw/jigsaw-solution/jigsaw-matchmanager-business/target/classes/log4j2.xml]
2016-05-09 16:06:05,952 main DEBUG Started configuration XmlConfiguration[location=/home/mrunal/repo/jigsaw/jigsaw/jigsaw-solution/jigsaw-matchmanager-business/target/classes/log4j2.xml] OK.
2016-05-09 16:06:05,953 main DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false-1
2016-05-09 16:06:05,953 main DEBUG Stopped org.apache.logging.log4j.core.config.DefaultConfiguration@7de26db8 OK
2016-05-09 16:06:06,027 main DEBUG Registering MBean org.apache.logging.log4j2:type=1a6c5a9e
2016-05-09 16:06:06,030 main DEBUG Registering MBean org.apache.logging.log4j2:type=1a6c5a9e,component=StatusLogger
2016-05-09 16:06:06,032 main DEBUG Registering MBean org.apache.logging.log4j2:type=1a6c5a9e,component=ContextSelector
2016-05-09 16:06:06,034 main DEBUG Registering MBean org.apache.logging.log4j2:type=1a6c5a9e,component=Loggers,name=
2016-05-09 16:06:06,036 main DEBUG Registering MBean org.apache.logging.log4j2:type=1a6c5a9e,component=Appenders,name=RollingRandomAccessFile
2016-05-09 16:06:06,041 main DEBUG Reconfiguration complete for context[name=1a6c5a9e] at URI /home/mrunal/repo/jigsaw/jigsaw/jigsaw-solution/jigsaw-matchmanager-business/target/classes/log4j2.xml (org.apache.logging.log4j.core.LoggerContext@732c2a62) with optional ClassLoader: null
2016-05-09 16:06:06,041 main DEBUG Shutdown hook enabled. Registering a new one.
16-05-09 16:06:06,043 main DEBUG LoggerContext[name=1a6c5a9e, org.apache.logging.log4j.core.LoggerContext@732c2a62] started OK.
Is there a dependency I am missing out or this some other problem? Please suggest possible things I can try for solving this problem. If you know a better solution to write logs in Logstash Json format you may suggest.
I suspect you are missing one of the Jackson dependencies.
You have jackson-core-asl, I think you need these three:
The ultimate source of truth for dependencies is the dependency tree of the Log4j 2 implementation module.
You may be able to get more details on the problem by setting <Configuration status="debug" ...
in your configuration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With