Log4j2 Sample Config

Cannot make it work with log4j2, could you please post your full setup (logger and appender)? Thx.

Hey @sihui

the logger for my rules looks exactly like I mentioned above

  log4j2.logger.rules.name = org.eclipse.smarthome.model.script.RULES
  log4j2.logger.rules.level = DEBUG

It’s more or less the same like @Dim describes in his 1st post (# Custom Log Levels)

I have not configured a custom appender yet.
The RULES logs go the openhab.log and OSGI only.

Ahh, okay, thanks. I thought you are logging into a separate file. Then I have to dig into the docs …

Is it possible to change the log level of the ItemStateChangedEvent only for a specific item?

I want the following logs to be suppressed:
2017-12-12 00:02:52.207 [vent.ItemStateChangedEvent] - Temperature changed from 5.1 to 4.8

I don’t think so. The events.log is set up to log all the activity on the event bus and that include State changes.

See How to watch and look through logging for some ways to filter on a log file to focus on just that information you want.

Here’s an example that I implemented today.
Objectives:
-Specify a default log level for all rules.
-(Temporarily) overrule the log level for individual log files.
-Use a dedicated log file for any logging created by my rules.

I have edited the /var/lib/openhab2/etc/org.ops4j.pax.logging.cfg file and added the following three sections:

# Rules files logger (default)
log4j2.logger.rules.name = org.eclipse.smarthome.model.script.rules
log4j2.logger.rules.level = INFO
log4j2.logger.rules.additivity = false
log4j2.logger.rules.appenderRefs = rules
log4j2.logger.rules.appenderRef.stdout.ref = RULES

# Log level for individual rules files
#log4j2.logger.rulesExperiment.name = org.eclipse.smarthome.model.script.rules.experiment
#log4j2.logger.rulesExperiment.level = DEBUG
#log4j2.logger.rulesNetwork.name = org.eclipse.smarthome.model.script.rules.network
#log4j2.logger.rulesNetwork.level = DEBUG
#log4j2.logger.rulesPresence.name = org.eclipse.smarthome.model.script.rules.presence
#log4j2.logger.rulesPresence.level = DEBUG
#log4j2.logger.rulesRfxtrx.name = org.eclipse.smarthome.model.script.rules.rfxtrx
#log4j2.logger.rulesRfxtrx.level = DEBUG
#log4j2.logger.rulesSun.name = org.eclipse.smarthome.model.script.rules.sun
#log4j2.logger.rulesSun.level = DEBUG
#log4j2.logger.rulesTimers.name = org.eclipse.smarthome.model.script.rules.timers
#log4j2.logger.rulesTimers.level = DEBUG

# Rules file appender
log4j2.appender.rules.type = RollingRandomAccessFile
log4j2.appender.rules.name = RULES
log4j2.appender.rules.fileName = ${openhab.logdir}/rules.log
log4j2.appender.rules.filePattern = ${openhab.logdir}/rules.log.%i
log4j2.appender.rules.immediateFlush = true
log4j2.appender.rules.append = true
log4j2.appender.rules.layout.type = PatternLayout
log4j2.appender.rules.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} [%-5.5p] [%-36.36c] - %m%n
log4j2.appender.rules.policies.type = Policies
log4j2.appender.rules.policies.size.type = SizeBasedTriggeringPolicy
log4j2.appender.rules.policies.size.size = 8MB

I hope this can help anyone who is struggling with the new log4j2 configuration.

edit
In the rules files I call the log methods as follows:

logDebug("rules.experiment","blah blah");
logInfo("rules.timers","blah blah");
logError("rules.network","blah blah");

Cheers,
Robert.

2 Likes

I cannot get this to work:

What I’ve done:
’’’# Rules files logger
log4j2.logger.rules.name = org.eclipse.smarthome.model.script.RULES
log4j2.logger.rules.level = INFO
log4j2.logger.rules.additivity = false
log4j2.logger.rules.appenderRefs = rules
log4j2.logger.rules.appenderRef.stdout.ref = RULES

‘’’# Rules file appender
log4j2.appender.rules.type = RollingRandomAccessFile
log4j2.appender.rules.name = RULES
log4j2.appender.rules.fileName = ${openhab.logdir}/rules.log
log4j2.appender.rules.filePattern = ${openhab.logdir}/rules.log.%i
log4j2.appender.rules.immediateFlush = true
log4j2.appender.rules.append = true
log4j2.appender.rules.layout.type = PatternLayout
log4j2.appender.rules.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} [%-5.5p] [%-36.36c] - %m%n
log4j2.appender.rules.policies.type = Policies
log4j2.appender.rules.policies.size.type = SizeBasedTriggeringPolicy
log4j2.appender.rules.policies.size.size = 8MB

rule:
logInfo(“RULES”, “Log message from rule xyz”)

in openhab.log
2017-12-25 13:22:36.798 [INFO ] [eclipse.smarthome.model.script.RULES] - Log message from rule xyz

What I understood:
The config file is org.ops4j.pax.logging.cfg (in OH2.1 to use with 4j logging and now for 4j2 logging?)

In my config file there a all entries beginning with log4j, because I updated from OH2.1 to OH 2.2

Should I update the file with the default from the first post here? And erase all old lines with log4j?

Shouldn’t this be

log4j2.logger.rules.name = org.eclipse.smarthome.model.script.rules

instead?

The file name should be org.ops4j.pax.logging.cfg

Does not fix it.

I tried this one too, with no success

I tried upper and lower cases, still the same
2017-12-25 15:13:20.772 [INFO ] [eclipse.smarthome.model.script.EXTRA] - PRESENCE: Somebody is home
2017-12-25 15:13:20.772 [INFO ] [eclipse.smarthome.model.script.RULES] - Log message from rule xyz
2017-12-25 15:13:20.772 [INFO ] [eclipse.smarthome.model.script.extra] - PRESENCE: Somebody is home
2017-12-25 15:13:20.772 [INFO ] [eclipse.smarthome.model.script.rules] - Log message from rule xyz

I got it fixed now.

I erased the old org.ops4j.pax.logging.cfg and took a now one from an brand new OH2.2 blank installation.

I I add to this file the needed custom config for a logfile, all is working well.

Hi @HomeAutomation,

In the rules files I call the log methods as follows:

logDebug("rules.experiment","blah blah");
logInfo("rules.timers","blah blah");
logError("rules.network","blah blah");

About the first argument: the lower case “rules” refers to the file appender and the identifier after the dot is my file name (without the “.rules” extension).

Cheers,
Robert.

Since the upgrade to OH2.2 my logging to a remote syslog server is no longer working. I’ve tried to modify the new org.ops4j.pax.logging.cfg file but to no avail. Has anybody got remote syslog logging working in the new OH2.2?

Thanks.

So I’ve started moving in the right direction with this and changed the custom configuration to fit my needs. I’m looking to have two custom loggers. One of the z-wave binding, and one of the weather underground binding. I’m looking to have them both set at debug logging and to have their own log files. The z-wave one works as desired, the weather underground one does not. After saving the new configuration it created a weatherunderground.log file but nothing is generating in it. I know the binding is running because it keeps updating it’s data.

Here is the custom loggers and custom apprehenders portion of my configuration. I included the sections as a whole, including the working z-wave section.

### Custom Loggers ###
# ZWave
log4j2.logger.ZWave.name = org.openhab.binding.zwave
log4j2.logger.ZWave.level = DEBUG
log4j2.logger.ZWave.additivity = false
log4j2.logger.ZWave.appenderRefs = ZWave
log4j2.logger.ZWave.appenderRef.ZWave.ref = ZWAVE

# weatherunderground
log4j2.logger.weatherunderground.name = org.openhab.binding.weatherunderground
log4j2.logger.weatherunderground.level = DEBUG
log4j2.logger.weatherunderground.additivity = false
log4j2.logger.weatherunderground.appenderRefs = weatherunderground
log4j2.logger.weatherunderground.appenderRef.weatherunderground.ref = weatherunderground

# Appenders configuration

### Custom Appenders ###
# ZWave
log4j2.appender.ZWave.name = ZWAVE
log4j2.appender.ZWave.type = RollingRandomAccessFile
log4j2.appender.ZWave.fileName = /var/log/openhab2/ZWave.log
log4j2.appender.ZWave.filePattern = /var/log/openhab2/ZWave.log.%i
log4j2.appender.ZWave.immediateFlush = true
log4j2.appender.ZWave.append = true
log4j2.appender.ZWave.layout.type = PatternLayout
log4j2.appender.ZWave.layout.pattern = %d{dd-MMM-yyyy HH:mm:ss.SSS} [%-5.5p] [%-50.50c] - %m%n
log4j2.appender.ZWave.policies.type = Policies
log4j2.appender.ZWave.policies.size.type = SizeBasedTriggeringPolicy
log4j2.appender.ZWave.policies.size.size = 10MB
log4j2.appender.ZWave.strategy.type = DefaultRolloverStrategy
log4j2.appender.ZWave.strategy.max = 10

# weatherunderground
log4j2.appender.weatherunderground.name = weatherunderground
log4j2.appender.weatherunderground.type = RollingRandomAccessFile
log4j2.appender.weatherunderground.fileName = /var/log/openhab2/weatherunderground.log
log4j2.appender.weatherunderground.filePattern = /var/log/openhab2/weatherunderground.log.%i
log4j2.appender.weatherunderground.immediateFlush = true
log4j2.appender.weatherunderground.append = true
log4j2.appender.weatherunderground.layout.type = PatternLayout
log4j2.appender.weatherunderground.layout.pattern = %d{dd-MMM-yyyy HH:mm:ss.SSS} [%-5.5p] [%-50.50c] - %m%n
log4j2.appender.weatherunderground.policies.type = Policies
log4j2.appender.weatherunderground.policies.size.type = SizeBasedTriggeringPolicy
log4j2.appender.weatherunderground.policies.size.size = 10MB
log4j2.appender.weatherunderground.strategy.type = DefaultRolloverStrategy
log4j2.appender.weatherunderground.strategy.max = 10

Anyone have any idea why only the weather underground one would be giving me problems?

After some more poking around I got it figured out. It was two things. The first thing I corrected was the line

log4j2.logger.weatherunderground.name = org.openhab.binding.weatherunderground

Was incorrect. It should have been

log4j2.logger.weatherunderground.name = org.eclipse.smarthome.binding.weatherunderground

To find the correct name for the binding I ran bundle:list in the console. I then checked what the start ID (left most column) for the weather underground bundle was. It was 218 so then in the console I entered

bundle:tree-show 218

At the top of the output (tree) is where I got the correct format of the binding.

After that I changed the following 2 lines

log4j2.logger.weatherunderground.appenderRef.weatherunderground.ref = weatherunderground
##and##
log4j2.appender.weatherunderground.name = weatherunderground

to

log4j2.logger.weatherunderground.appenderRef.weatherunderground.ref = WEATHERUNDERGROUND
##and##
log4j2.appender.weatherunderground.name = WEATHERUNDERGROUND

Simply put them in caps lock.

Maybe this will help someone in the future.

4 Likes

Previous release had a Sift appender which could be used for multiple bundles and each writing to separate log file based on bundle name. How do we achieve this using OpenHAB 2.2 with log4j2?

See first post “Custom config”

Thanks. I saw that post, but there separate loggers (e.g. KNX, ZWave etc) are created for each bundle.

I would prefer to create a single logger and use it for multiple bundle. For OpenHAB 2.1, I was using following in my org.ops4j.pax.logging.cfg file:

// Shared sift appender
log4j.appender.sift=org.apache.log4j.sift.MDCSiftingAppender
log4j.appender.sift.key=bundle.name
log4j.appender.sift.default=openhab
log4j.appender.sift.appender=org.apache.log4j.FileAppender
log4j.appender.sift.appender.layout=org.apache.log4j.PatternLayout
log4j.appender.sift.appender.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5.5p] [%t] [%-36.36c] - %m%n
log4j.appender.sift.appender.file=${openhab.logdir}/${bundle.name}.log
log4j.appender.sift.appender.append=true

// Refer above sift appender in each of my custom bundle
log4j.logger.mycustombundle1=INFO, sift
log4j.logger.mycustombundle2=INFO, sift
log4j.logger.mycustombundle3=INFO, sift
log4j.logger.mycustombundle4=INFO, sift

Thank you @Dim

1 Like

Hi All,
there are many logging related threads but this seems most relevant to my case. I want to have 2 different custom loggers but appending to the same file. I know how to make them appending to different files but since they are related to the same binding I want them to land in single file.
This is related to eBUS binding which logs to different patterns (the below was produced by myself and logs to separate files as you can see):

### Custom Loggers ###
# EBUS
log4j2.logger.eBus.name = org.openhab.binding.ebus
log4j2.logger.eBus.level = WARN
log4j2.logger.eBus.additivity = false
log4j2.logger.eBus.appenderRefs = eBus
log4j2.logger.eBus.appenderRef.eBus.ref = EBUS

# CSDEV
log4j2.logger.csdev.name = de.csdev.ebus
log4j2.logger.csdev.level = DEBUG
log4j2.logger.csdev.additivity = false
log4j2.logger.csdev.appenderRefs = csdev
log4j2.logger.csdev.appenderRef.eBus.ref = CSDEV

### Custom Appenders ###
# EBUS
log4j2.appender.eBus.name = EBUS
log4j2.appender.eBus.type = RollingRandomAccessFile
log4j2.appender.eBus.fileName = ${openhab.logdir}/ebus.log
log4j2.appender.eBus.filePattern = ${openhab.logdir}/ebus.log.%i
log4j2.appender.eBus.immediateFlush = true
log4j2.appender.eBus.append = true
log4j2.appender.eBus.layout.type = PatternLayout
log4j2.appender.eBus.layout.pattern = %d{dd-MMM-yyyy HH:mm:ss.SSS} [%-5.5p] [%-50.50c] - %m%n
log4j2.appender.eBus.policies.type = Policies
log4j2.appender.eBus.policies.size.type = SizeBasedTriggeringPolicy
log4j2.appender.eBus.policies.size.size = 10MB
log4j2.appender.eBus.strategy.type = DefaultRolloverStrategy
log4j2.appender.eBus.strategy.max = 10

# CSDEV
log4j2.appender.csdev.name = CSDEV
log4j2.appender.csdev.type = RollingRandomAccessFile
log4j2.appender.csdev.fileName = ${openhab.logdir}/csdev.log
log4j2.appender.csdev.filePattern = ${openhab.logdir}/csdev.log.%i
log4j2.appender.csdev.immediateFlush = true
log4j2.appender.csdev.append = true
log4j2.appender.csdev.layout.type = PatternLayout
log4j2.appender.csdev.layout.pattern = %d{dd-MMM-yyyy HH:mm:ss.SSS} [%-5.5p] [%-50.50c] - %m%n
log4j2.appender.csdev.policies.type = Policies
log4j2.appender.csdev.policies.size.type = SizeBasedTriggeringPolicy
log4j2.appender.csdev.policies.size.size = 10MB
log4j2.appender.csdev.strategy.type = DefaultRolloverStrategy
log4j2.appender.csdev.strategy.max = 10


how to make it to get appended to the same file? I can’t find good manual describing the logging config, and by looking how it looks in other places but I have to say I am closer to intuition than true firm understanding of how to control it.

Hi @witek_1308,

I have described this in one of my blog posts. Unfortunately it´s in german but i think you can adopt the code samples easily.

Best regards!

http://cleveres-heim.de/howto/openhab-2-3-log4j2-beispiel-konfiguration-knx-binding-logger/