openhab / openhab-distro

The binary distribution of openHAB
https://www.openhab.org/
Eclipse Public License 2.0
1.3k stars 394 forks source link

Log4J2 JSONLayout does not work #1083

Open xdanik opened 4 years ago

xdanik commented 4 years ago

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

philippwaller commented 4 years ago

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

kmhughes commented 4 years ago

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

xdanik commented 4 years ago

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

Tycale commented 4 years ago

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".

madb0o commented 3 years ago

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

xdanik commented 2 years ago

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...

wborn commented 2 years ago

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

xdanik commented 2 years ago

@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!).

wborn commented 2 years ago

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:

openhab-bot commented 2 years ago

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

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

wborn commented 2 years ago

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:

xdanik commented 2 years ago

@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? 😉

wborn commented 2 years ago

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.

querdenker2k commented 1 month ago

I am having problems installing this in openhab 4.2.0 @wborn do you have an idea what to do? I am not familiar with osgi and having trouble [WARNING] Error resolving artifact org.openhab.addons.bundles:org.openhab.io.log4j2extra:jar:4.3.0-SNAPSHOT: [Could not find artifact org.openhab.addons.bundles:org.openhab.io.log4j2extra:jar:4.3.0-SNAPSHOT in openhab (https://openhab.jfrog.io/openhab/libs-release/), Could not find artifact org.openhab.addons.bundles:org.openhab.io.log4j2extra:jar:4.3.0-SNAPSHOT in openhab (https://openhab.jfrog.io/openhab/libs-release/), Could not find artifact org.openhab.addons.bundles:org.openhab.io.log4j2extra:jar:4.3.0-SNAPSHOT in openhab-snapshot (https://openhab.jfrog.io/openhab/libs-snapshot/)]

wborn commented 3 weeks ago

do you have an idea what to do?

It's not an official add-on and can only be installed via the Marketplace.

If that does not work, the pax-logging-log4j2-extra dependency may need to be updated for it to work.

xdanik commented 2 weeks ago

I have it working with OH 4.1.3. I believe I have just edited the /var/lib/openhab/etc/log4j2.xml file and the JSON logging started to work. I do not remember I have installed anything since the 4.0 release. This is my log4j2.xml.txt config file.