openhab-distro icon indicating copy to clipboard operation
openhab-distro copied to clipboard

Log4J2 JSONLayout does not work

Open xdanik opened this issue 4 years ago • 19 comments

Hello, I am trying t configure log4j2 loging with appender layout == JSONLayout. log4j2.appender.json.layout.type = JSONLayout

However I am getting error:

Mar 01 00:42:56 pi systemd[1]: Started openHAB 2 - empowering the smart home.
Mar 01 00:43:03 pi karaf[17540]: org.ops4j.pax.logging.pax-logging-api [log4j2] ERROR : Log4J2 configuration problem: com/fasterxml/jackson/databind/ser/FilterProvider Ignored FQCN: org.apache.logging.log4j.spi.AbstractLogger
Mar 01 00:43:03 pi karaf[17540]: java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ser/FilterProvider
Mar 01 00:43:03 pi karaf[17540]:         at org.apache.logging.log4j.core.layout.JsonLayout.<init>(JsonLayout.java:158)
Mar 01 00:43:03 pi karaf[17540]:         at org.apache.logging.log4j.core.layout.JsonLayout.<init>(JsonLayout.java:69)
Mar 01 00:43:03 pi karaf[17540]:         at org.apache.logging.log4j.core.layout.JsonLayout$Builder.build(JsonLayout.java:102)
Mar 01 00:43:03 pi karaf[17540]:         at org.apache.logging.log4j.core.layout.JsonLayout$Builder.build(JsonLayout.java:77)
Mar 01 00:43:03 pi karaf[17540]:         at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:122)
Mar 01 00:43:03 pi karaf[17540]:         at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1002)
Mar 01 00:43:03 pi karaf[17540]:         at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:942)
Mar 01 00:43:03 pi karaf[17540]:         at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:934)
Mar 01 00:43:03 pi karaf[17540]:         at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:552)
Mar 01 00:43:03 pi karaf[17540]:         at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:241)
Mar 01 00:43:03 pi karaf[17540]:         at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:288)
Mar 01 00:43:03 pi karaf[17540]:         at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:579)
Mar 01 00:43:03 pi karaf[17540]:         at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:285)
Mar 01 00:43:03 pi karaf[17540]:         at org.ops4j.pax.logging.log4j2.internal.PaxLoggingServiceImpl.configureLog4J2(PaxLoggingServiceImpl.java:409)
Mar 01 00:43:03 pi karaf[17540]:         at org.ops4j.pax.logging.log4j2.internal.PaxLoggingServiceImpl.updated(PaxLoggingServiceImpl.java:272)
Mar 01 00:43:03 pi karaf[17540]:         at org.ops4j.pax.logging.log4j2.internal.PaxLoggingServiceImpl$1ManagedPaxLoggingService.updated(PaxLoggingServiceImpl.java:569)
Mar 01 00:43:03 pi karaf[17540]:         at org.apache.felix.cm.impl.helper.ManagedServiceTracker.updated(ManagedServiceTracker.java:189)
Mar 01 00:43:03 pi karaf[17540]:         at org.apache.felix.cm.impl.helper.ManagedServiceTracker.updateService(ManagedServiceTracker.java:152)
Mar 01 00:43:03 pi karaf[17540]:         at org.apache.felix.cm.impl.helper.ManagedServiceTracker.provideConfiguration(ManagedServiceTracker.java:85)
Mar 01 00:43:03 pi karaf[17540]:         at org.apache.felix.cm.impl.ConfigurationManager$ManagedServiceUpdate.provide(ConfigurationManager.java:1118)
Mar 01 00:43:03 pi karaf[17540]:         at org.apache.felix.cm.impl.ConfigurationManager$ManagedServiceUpdate.run(ConfigurationManager.java:1074)
Mar 01 00:43:03 pi karaf[17540]:         at org.apache.felix.cm.impl.UpdateThread.run0(UpdateThread.java:138)
Mar 01 00:43:03 pi karaf[17540]:         at org.apache.felix.cm.impl.UpdateThread.run(UpdateThread.java:105)
Mar 01 00:43:03 pi karaf[17540]:         at java.lang.Thread.run(Thread.java:748)
Mar 01 00:43:03 pi karaf[17540]: Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ser.FilterProvider cannot be found by org.ops4j.pax.logging.pax-logging-log4j2_1.11.2
Mar 01 00:43:03 pi karaf[17540]:         at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:484)
Mar 01 00:43:03 pi karaf[17540]:         at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:395)
Mar 01 00:43:03 pi karaf[17540]:         at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:387)
Mar 01 00:43:03 pi karaf[17540]:         at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:150)
Mar 01 00:43:03 pi karaf[17540]:         at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
Mar 01 00:43:03 pi karaf[17540]:         ... 25 more

It seems like Jackson library is not loaded. I have tried manualy install Jackson bundles:

bundle:install -l 5 mvn:com.fasterxml.jackson.core/jackson-core/2.10.2
bundle:install -l 5 mvn:com.fasterxml.jackson.core/jackson-annotations/2.10.2
bundle:install -l 5 mvn:com.fasterxml.jackson.core/jackson-databind/2.10.2

And add Jackson to /var/lib/openhab2/etc/startup.properties to make sure that these bundles are loaded before Log4J2.

mvn\:com.fasterxml.jackson.core/jackson-core/2.10.2 = 5
mvn\:com.fasterxml.jackson.core/jackson-annotations/2.10.2 = 5
mvn\:com.fasterxml.jackson.core/jackson-databind/2.10.2 = 5

But I am still getting the same error. I am running OpenHab 2.5.2-1. org.ops4j.pax.logging.cfg.txt

Am I missing something? https://community.openhab.org/t/logging-to-json-maybe-missing-dependency/93838

xdanik avatar Mar 01 '20 13:03 xdanik

I facing the same problem with current openHAB snapshot build 56 of version 2.5.3.

philippwaller avatar Mar 04 '20 09:03 philippwaller

I don't know fully how you are doing this, but I wanted JsonLogging. I am using Karaf, building my own assembly. The steps I needed are in the answer to my stackoverflow question.

https://stackoverflow.com/questions/61493642/failing-to-get-karaf-4-2-6-to-log-using-log4j2-with-jsonlayout-for-a-layout-type

kmhughes avatar May 01 '20 16:05 kmhughes

I have tested adding mvn\:org.ops4j.pax.logging/pax-logging-log4j2-extra/1.11.2 = 8 to the /var/lib/openhab2/etc/startup.properties, but I am still getting the same error 😕

I have also manualy installed the bundle from karaf console using bundle:install mvn\:org.ops4j.pax.logging/pax-logging-log4j2-extra/1.11.2

xdanik avatar May 01 '20 17:05 xdanik

Same error here, also tried @xdanik 's instructions but it didn't help. In the karaf console, in the bundle:list I can see that pax-logging-log4j2-extra is "Installed" but not "loaded".

Tycale avatar May 13 '20 22:05 Tycale

I'm also running into this issue - any solution?

madb0o avatar Nov 13 '20 01:11 madb0o

I was able to get it working on OpenHab 3.1.0.

I have placed following files into the "addons" folder in order to get them loaded:

And modified log4j2.xml accordingly: (under <Appenders>)

<!-- JSON -->
<RollingFile append="true" fileName="${sys:openhab.logdir}/openhab.json" filePattern="${sys:openhab.logdir}/openhab.json.%i" name="JSON">
	<JSONLayout compact="true" complete="false" eventEol="true" objectMessageAsJsonObject="true"/>
	<Policies>
		<SizeBasedTriggeringPolicy size="10 MB"/>
	</Policies>
	<DefaultRolloverStrategy max="2"/>
</RollingFile>

And added the appender to the default "openhab" log:

<!-- Root logger configuration -->
<Root level="WARN">
	<AppenderRef ref="JSON"/>
	<AppenderRef ref="LOGFILE"/>
	<AppenderRef ref="OSGI"/>
</Root>

@wborn Would be please possible to add these dependencies to the core so they are available by default? It seems like some version of jackson is already included in the core...

xdanik avatar Oct 04 '21 21:10 xdanik

Maybe, but I have other things on my todo list that I'd currently rather look into. :wink:

wborn avatar Oct 05 '21 07:10 wborn

@wborn I guessed that 😉. I would like to prepare the change myself, but my experiences with Maven are pretty basic and I have no idea what files to edit (so many pom.xml files here!).

xdanik avatar Oct 05 '21 08:10 xdanik

I have no idea what files to edit

Yes that is the most work, see how it fits into everything there already is without installing all sorts of bundles that nobody needs if they don't use this specific functionality. :slightly_smiling_face:

wborn avatar Oct 05 '21 08:10 wborn

This issue has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/log4j2-extra/127214/1

openhab-bot avatar Oct 05 '21 11:10 openhab-bot

Maybe it can be added as an add-on, e.g. I quickly created a LOG4J2 Extra add-on on the Marketplace. I spent more time on making the logo as the add-on. :wink:

wborn avatar Oct 05 '21 11:10 wborn

@wborn Great, thanks! Can't wait to test it to tonight. What does mean, that its on the Marketplace? I tried to install it using the "miscellaneous add-ons" section in GUI and the addon was not showing in the list of available addons. Should I place it in the "addons" directory instead of the individual JAR files?

Still - the file seems small enough that nobody will complain if it were part of the core by default. Right? 😉

xdanik avatar Oct 05 '21 11:10 xdanik

If you use openHAB 3.2.0.M3 or a recent 3.2.0-SNAPSHOT build you can install it using the new Marketplace (see Announcement, https://github.com/openhab/openhab-webui/pull/1158#issuecomment-914140336).

It is currently listed under "Bindings" instead of "Other" due to a bug (https://github.com/openhab/openhab-core/issues/2507).

You can also drop the KAR in your /addons dir and it will probably also work.

the file seems small enough that nobody will complain if it were part of the core by default

My add-on has a dependency on the Jackson feature which will install many other bundles. That's probably also the reason why they decided to separate it into the extra bundle.

The Log4j 2 Extra bundle seems to be only a bundle fragment that makes the Jackson bundles (and others) available to Pax Logging. Although it has many more imports, so probably more bundles would be needed to be able to use all the "Extra" functionality, like:

It is very likely over time more bundles need to be added to satisfy the needs of more users. So it can all stay optional by creating an add-on for this. I.e. similar to how the Metrics Add-on can be optionally installed.

wborn avatar Oct 05 '21 11:10 wborn