Log4j2 Sample Config

to control the appender rolling, add the following 2 lines at the end of your custom appender:

log4j2.appender.ZWave.strategy.type = DefaultRolloverStrategy
log4j2.appender.ZWave.strategy.max = 10

(I updated the first post also)

So I finally figured out how to get the logger configuration right to do this. I have a filtered debug log I use to pull certain zwave information out for rule triggers but wanted to keep normal info level logging information going to the default log file. Here is my logger configuration, where lockid is my appender information to filter out the zwave alarm codes to a separate file.:

# Logger - Zwave
log4j2.logger.zwave.name = org.openhab.binding.zwave
log4j2.logger.zwave.level = DEBUG
log4j2.logger.zwave.additivity = false
log4j2.logger.zwave.appenderRefs = lockid, out
log4j2.logger.zwave.appenderRef.lockid.ref = LOCKID
log4j2.logger.zwave.appenderRef.out.ref = LOGFILE
log4j2.logger.zwave.appenderRef.out.level = INFO

Appender configuration in case anyone else wants to apply filters:

# File appender - lockid.log
log4j2.appender.lockid.name = LOCKID
log4j2.appender.lockid.type = RollingRandomAccessFile
log4j2.appender.lockid.fileName = /var/log/openhab2/lockid.log
log4j2.appender.lockid.filePattern = /var/log/openhab2/lockid.log.%i
log4j2.appender.lockid.filter.regex.type = RegexFilter
log4j2.appender.lockid.filter.regex.regex = .*ALARM report - 19.*
log4j2.appender.lockid.filter.regex.onMatch = ACCEPT
log4j2.appender.lockid.filter.regex.onMisMatch = DENY
log4j2.appender.lockid.immediateFlush = true
log4j2.appender.lockid.append = true
log4j2.appender.lockid.layout.type = PatternLayout
log4j2.appender.lockid.layout.pattern = %d{dd-MMM-yyyy HH:mm:ss.SSS} [%-5.5p] [%-50.50c] - %m%n
log4j2.appender.lockid.policies.type = Policies
log4j2.appender.lockid.policies.size.type = SizeBasedTriggeringPolicy
log4j2.appender.lockid.policies.size.size = 10MB
log4j2.appender.lockid.strategy.type = DefaultRolloverStrategy
log4j2.appender.lockid.strategy.max = 5
6 Likes

Please can someone tell me in a dummy-friendly way how i can get the old logging-behavior back? I made a snapshot update a few days agp and since this time i don´t get any new log entries into the events.log and the openhab.log.

You didn’t let it update your config file during the update to the new format. You need to replace /userdata/etc/org.ops4j.pax.logging.cfg

With the contents of https://github.com/openhab/openhab-distro/blob/3b1bbf413f6c4b849891708609dcfa8c2ea609e4/distributions/openhab/src/main/resources/userdata/etc/org.ops4j.pax.logging.cfg

2 Likes

Thanks a lot, that was it…

But i think there was another file which i made a deny to update. Can you tell me, which config file this was?

Here you go https://community.openhab.org/t/karaf-upgrade/33208/5?u=rgerrans

Yes, but i thought there were 2 files which will be overwritten and i typed “N” (no).

So was this config-file for logging the only config-file which was changed inside this snapshot-update?

Sorry, I saw the two cp lines and didn’t read the full actions. Maybe @kai can identify the other file that changed.

Yes there was only one config file responsible for logging.

For future reference if you tell apt that you don’t want to use the new config, it will still be placed in the folder, it will just have dpkg* appended to it. If you choose to overwrite with the new config, your old configuration will be renamed to the same name with dpkg-old appended to it.

The question was, if there was a second changed config file at all. Not only for logging.

use the info that Benjy gave you to find the answer yourself :slight_smile:

cd /var/lib/openhab2/
find . -name *dpkg*

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