This post is OBE. See Log4j2 Sample Config
**NOTE: With the build from November 10 #579 the location of all the files in runtime/karaf/etc have moved to userdata/etc. The instructions below have been updated accordingly. If you are running an older verison of openHAB (anything older than build #579), the location of the file is runtime/karaf/etc.
As almost everyone is aware, openHAB 2 uses a completely different logging library and approach to logging than openHAB 1.x does, replacing logback with log4j.
This means a completely new way of configuring the logger to split out the logs from different bindings to their own file, for example. But this can still be done.
As I mentioned, openHAB 2 uses log4j 2.x (I’m not sure of the exact version) and instead of using XML or JSON for configuring the loggers, openHAB 2 uses a traditional properties file. This file is located at:
userdata/etc/org.ops4j.pax.logging.cfg
Log4j uses pretty much the same concepts as logback does: loggers and appenders.
The logger specifies the namespace that the logger applies to, the level to log at, which appenders to log to, and whether or not the logger is additive. There is a parent child relationship with loggers with the logger for org.openhab
being the parent to org.openhab.binding.zwave
. If additivity is set to true the log sent to the child logger also get sent to the parent logger. So if you don’t want to see the zwave binding’s logs in openhab.log, set additivity to false.
The appender specifies where (e.g. which file) and the format of the logging statements. One appender can be used for multiple loggers.
So, if you wanted to split out the zwave log into its own file and set its level to TRACE you would define the logger:
# zwave logger
log4j.logger.org.openhab.binding.zwave = TRACE, zwave, osgi:*
log4j.additivity.org.openhab.binding.zwave = false
and define the appender
# File appender - zwave.log
log4j.appender.zwave=org.apache.log4j.RollingFileAppender
log4j.appender.zwave.layout=org.apache.log4j.PatternLayout
log4j.appender.zwave.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%-26.26c{1}] - %m%n
log4j.appender.zwave.file=${openhab.logdir}/zwave.log
log4j.appender.zwave.append=true
log4j.appender.zwave.maxFileSize=10MB
log4j.appender.zwave.maxBackupIndex=10
And you are good to go. The secret sauce is the comma separated set of parameters on the logger definition line TRACE, zwave, osgi:*
. The first item sets the logging level to TRACE. The second two parameters define the appenders that logger logs to. In this case it is our zwave appender (the name of the appender is the word after .appender) and the OSGI console if you are someone who watches logs through the Karaf console.
To turn off the events.log file all you need to do is remove the appender event
from the log4j.logger.smarthome.event = INFO, event, osgi:*
line.
WARNING: the official openHAB 2 docs on logging warns:
Currently the file org.ops4j.pax.logging.cfg will get overwritten with the default version on every update of openHAB. There is an issue on this.
So backup your file and remember to reapply it after you run an update.