Log4j2 Sample Config

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

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.