openHAB 3: conditional log Items

I want to skip the logging for some items like Spotify track progress (which updates every second) so that the logs are not polluted.

Since openHAB version 3 log4j2 is used as logging framework and the log4j2.xml has to be used to configure logging. We can use a RegexFilter to configure such behaviour.

How to

The RegexFilter allows the formatted or unformatted message to be compared against a regular expression.

https://logging.apache.org/log4j/2.x/manual/filters.html

The config file for logging is located in the userdata/etc folder (manual setup) or in /var/lib/openhab/etc (apt/deb-based setup).

Locate the Event Log Appender and add a RegexFilter. I added filters for Spotify, CPU load changes and heart beats.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Configuration monitorInterval="10">
 
  <Appenders>
    <!-- Console appender not used by default (see Root logger AppenderRefs) -->
    <Console name="STDOUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%-5.5p] [%-36.36c] - %m%n"/>
    </Console>
 
    <!-- Rolling file appender -->
    <RollingFile fileName="${sys:openhab.logdir}/openhab.log" filePattern="${sys:openhab.logdir}/openhab.log.%i.gz" name="LOGFILE">
      <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5.5p] [%-36.36c] - %m%n"/>
      <Policies>
        <OnStartupTriggeringPolicy/>
        <SizeBasedTriggeringPolicy size="16 MB"/>
      </Policies>
      <DefaultRolloverStrategy max="7"/>
    </RollingFile>
 
    <!-- Event log appender -->
    <RollingRandomAccessFile fileName="${sys:openhab.logdir}/events.log" filePattern="${sys:openhab.logdir}/events.log.%i.gz" name="EVENT">
      <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5.5p] [%-36.36c] - %m%n"/>
      <Policies>
        <OnStartupTriggeringPolicy/>
        <SizeBasedTriggeringPolicy size="16 MB"/>
      </Policies>
      <DefaultRolloverStrategy max="7"/>
      <RegexFilter regex=".*Spotify_TrackP.*|.*CPU_.*|.*Shelly.+HeartBeat.*" onMatch="DENY" onMismatch="ACCEPT"/>
    </RollingRandomAccessFile>

The changes are automatically picked up by openhab without restart. After some seconds those events should not be logged anymore.

If you have found a spelling error, please, notify us by selecting that text and pressing Ctrl+Enter.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Spelling error report

The following text will be sent to our editors: