[SOLVED] Logging to separate file (log4j2)

logging
Tags: #<Tag:0x00007fd317f26df8>

(autoit) #1

I’m trying to get the example from the docs to work, but failing so far. At the end of /var/lib/openhab2/etc/org.ops4j.pax.logging.cfg I have

# Logger - Demo.log
log4j.logger.org.eclipse.smarthome.model.script.Demo = DEBUG, Demo

# File appender - Demo.log
log4j.appender.Demo=org.apache.log4j.RollingFileAppender
log4j.appender.Demo.layout=org.apache.log4j.PatternLayout
log4j.appender.Demo.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5.5p] [%-36.36c] - %m%n
log4j.appender.Demo.file=${openhab.logdir}/Demo.log
log4j.appender.Demo.append=true
log4j.appender.Demo.maxFileSize=10MB
log4j.appender.Demo.maxBackupIndex=10

On a rule I have

logDebug("Demo", "demo")

No Demo.log is created at all. I know the rule is working since if I replace Debug with Info, I can see the log entry on /var/log/openhab2/openhab.log. I’m running Openhabian 2.2.0-1 on Pi 3. What am I doing wrong?


(Angelos) #2

If I am not wrong, you need to use log4j2 since you are on OH2.2
see example here: Log4j2 Sample Config

Ps: I think that the docs are not up to speed with log4j2 (that’s also my fault :stuck_out_tongue:)


(autoit) #3

Tried to do that, but then I’m getting this in the log as soon as I save the file :confused:

2018-02-14 18:37:47.834 [ERROR] [org.apache.felix.configadmin        ] - [org.osgi.service.log.LogService, org.knopflerfish.service.log.LogService, org.ops4j.pax.logging.PaxLoggingService, org.osgi.service.cm.ManagedService, id=33, bundle=6/mvn:org.ops4j.pax.logging/pax-logging-log4j2/1.10.1]: Unexpected problem updating configuration org.ops4j.pax.logging

java.lang.StringIndexOutOfBoundsException: String index out of range: -1

	at java.lang.String.substring(String.java:1967) [?:?]

	at org.apache.logging.log4j.util.PropertiesUtil.partitionOnCommonPrefixes(PropertiesUtil.java:293) [5:org.ops4j.pax.logging.pax-logging-api:1.10.1]

	at org.apache.logging.log4j.core.config.properties.PropertiesConfigurationBuilder.build(PropertiesConfigurationBuilder.java:155) [6:org.ops4j.pax.logging.pax-logging-log4j2:1.10.1]

	at org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory.getConfiguration(PropertiesConfigurationFactory.java:56) [6:org.ops4j.pax.logging.pax-logging-log4j2:1.10.1]

	at org.ops4j.pax.logging.log4j2.internal.PaxLoggingServiceImpl.doUpdate(PaxLoggingServiceImpl.java:206) [6:org.ops4j.pax.logging.pax-logging-log4j2:1.10.1]

	at org.ops4j.pax.logging.log4j2.internal.PaxLoggingServiceImpl.updated(PaxLoggingServiceImpl.java:158) [6:org.ops4j.pax.logging.pax-logging-log4j2:1.10.1]

	at org.ops4j.pax.logging.log4j2.internal.PaxLoggingServiceImpl$1ManagedPaxLoggingService.updated(PaxLoggingServiceImpl.java:426) [6:org.ops4j.pax.logging.pax-logging-log4j2:1.10.1]

	at org.apache.felix.cm.impl.helper.ManagedServiceTracker.updated(ManagedServiceTracker.java:189) [7:org.apache.felix.configadmin:1.8.16]

	at org.apache.felix.cm.impl.helper.ManagedServiceTracker.updateService(ManagedServiceTracker.java:152) [7:org.apache.felix.configadmin:1.8.16]

	at org.apache.felix.cm.impl.helper.ManagedServiceTracker.provideConfiguration(ManagedServiceTracker.java:85) [7:org.apache.felix.configadmin:1.8.16]

	at org.apache.felix.cm.impl.ConfigurationManager$UpdateConfiguration.run(ConfigurationManager.java:1792) [7:org.apache.felix.configadmin:1.8.16]

	at org.apache.felix.cm.impl.UpdateThread.run0(UpdateThread.java:141) [7:org.apache.felix.configadmin:1.8.16]

	at org.apache.felix.cm.impl.UpdateThread.run(UpdateThread.java:109) [7:org.apache.felix.configadmin:1.8.16]

	at java.lang.Thread.run(Thread.java:748) [?:?]

(autoit) #4

Bumping this in the hopes that someone knows a solution. I only need a simple way to store my own log entries in an easily readable format (ie. not a database).


(Angelos) #5

You are still having problems with your org.ops4j.pax.logging.cfg ?
Post it here to check it


(autoit) #6

Yes, situation is still the same. Full file is

# Common pattern layout for appenders
#log4j2.pattern = %d{ISO8601} | %-5p | %-16t | %-32c{1} | %X{bundle.id} - %X{bundle.name} - %X{bundle.version} | %m%n

# Root logger
log4j2.rootLogger.level = WARN
log4j2.rootLogger.appenderRefs = out, osgi
log4j2.rootLogger.appenderRef.out.ref = LOGFILE
log4j2.rootLogger.appenderRef.osgi.ref = OSGI

# Karaf Shell logger
log4j2.logger.shell.name = org.apache.karaf.shell.support
log4j2.logger.shell.level = OFF
log4j2.logger.shell.appenderRefs = stdout
log4j2.logger.shell.appenderRef.stdout.ref = STDOUT

# Security audit logger
log4j2.logger.audit.name = org.apache.karaf.jaas.modules.audit
log4j2.logger.audit.level = INFO
log4j2.logger.audit.additivity = false
log4j2.logger.audit.appenderRefs = audit
log4j2.logger.audit.appenderRef.audit.ref = AUDIT

# openHAB specific logger configuration

log4j2.logger.openhab.name = org.openhab
log4j2.logger.openhab.level = INFO

log4j2.logger.smarthome.name = org.eclipse.smarthome
log4j2.logger.smarthome.level = INFO

log4j2.logger.smarthomeItemStateEvent.name = smarthome.event.ItemStateEvent
log4j2.logger.smarthomeItemStateEvent.level = ERROR
log4j2.logger.smarthomeItemAddedEvent.name = smarthome.event.ItemAddedEvent
log4j2.logger.smarthomeItemAddedEvent.level = ERROR
log4j2.logger.smarthomeItemRemovedEvent.name = smarthome.event.ItemRemovedEvent
log4j2.logger.smarthomeItemRemovedEvent.level = ERROR
log4j2.logger.smarthomeThingStatusInfoEvent.name = smarthome.event.ThingStatusInfoEvent
log4j2.logger.smarthomeThingStatusInfoEvent.level = ERROR
log4j2.logger.smarthomeThingAddedEvent.name = smarthome.event.ThingAddedEvent
log4j2.logger.smarthomeThingAddedEvent.level = ERROR
log4j2.logger.smarthomeThingRemovedEvent.name = smarthome.event.ThingRemovedEvent
log4j2.logger.smarthomeThingRemovedEvent.level = ERROR
log4j2.logger.smarthomeInboxUpdatedEvent.name = smarthome.event.InboxUpdatedEvent
log4j2.logger.smarthomeInboxUpdatedEvent.level = ERROR

log4j2.logger.events.name = smarthome.event
log4j2.logger.events.level = INFO
log4j2.logger.events.additivity = false
log4j2.logger.events.appenderRefs = event
log4j2.logger.events.appenderRef.event.ref = EVENT
log4j2.logger.events.appenderRef.osgi.ref = OSGI

log4j2.logger.jupnp.name = org.jupnp
log4j2.logger.jupnp.level = ERROR

log4j2.logger.jmdns.name = javax.jmdns
log4j2.logger.jmdns.level = ERROR

# This suppresses all Maven download issues from the log when doing feature installations
# as we are logging errors ourselves in a nicer way anyhow.
log4j2.logger.paxurl.name = org.ops4j.pax.url.mvn.internal.AetherBasedResolver
log4j2.logger.paxurl.level = ERROR

# Filters known issues of pax-web (issue link to be added here).
# Can be removed once the issues are resolved in an upcoming version.
log4j2.logger.paxweb.name = org.ops4j.pax.web.pax-web-runtime
log4j2.logger.paxweb.level = OFF

# Filters known issues of lsp4j, see
# https://github.com/eclipse/smarthome/issues/4639
# https://github.com/eclipse/smarthome/issues/4629
# https://github.com/eclipse/smarthome/issues/4643
# Can be removed once the issues are resolved in an upcoming version.
log4j2.logger.lsp4j.name = org.eclipse.lsp4j
log4j2.logger.lsp4j.level = OFF

# Filters known issues of KarServiceImpl, see
# https://github.com/openhab/openhab-distro/issues/519#issuecomment-351944506
# Can be removed once the issues are resolved in an upcoming version.
log4j2.logger.karservice.name = org.apache.karaf.kar.internal.KarServiceImpl
log4j2.logger.karservice.level = ERROR


# Appenders configuration

# Console appender not used by default (see log4j2.rootLogger.appenderRefs)
log4j2.appender.console.type = Console
log4j2.appender.console.name = STDOUT
log4j2.appender.console.layout.type = PatternLayout
log4j2.appender.console.layout.pattern = %d{HH:mm:ss.SSS} [%-5.5p] [%-36.36c] - %m%n

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

# Event log appender
log4j2.appender.event.type = RollingRandomAccessFile
log4j2.appender.event.name = EVENT
log4j2.appender.event.fileName = ${openhab.logdir}/events.log
log4j2.appender.event.filePattern = ${openhab.logdir}/events.log.%i
log4j2.appender.event.immediateFlush = true
log4j2.appender.event.append = true
log4j2.appender.event.layout.type = PatternLayout
log4j2.appender.event.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} [%-26.26c] - %m%n
log4j2.appender.event.policies.type = Policies
log4j2.appender.event.policies.size.type = SizeBasedTriggeringPolicy
log4j2.appender.event.policies.size.size = 16MB

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

# OSGi appender
log4j2.appender.osgi.type = PaxOsgi
log4j2.appender.osgi.name = OSGI
log4j2.appender.osgi.filter = *

# Logger - Demo.log
log4j2.logger.org.eclipse.smarthome.model.script.Demo = DEBUG, Demo

# File appender - Demo.log
log4j2.appender.Demo=org.apache.log4j2.RollingFileAppender
log4j2.appender.Demo.layout=org.apache.log4j2.PatternLayout
log4j2.appender.Demo.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5.5p] [%-36.36c] - %m%n
log4j2.appender.Demo.file=${openhab.logdir}/Demo.log
log4j2.appender.Demo.append=true
log4j2.appender.Demo.maxFileSize=10MB
log4j2.appender.Demo.maxBackupIndex=10

I haven’t touched it except for the demo code at the bottom.


(Angelos) #7

It seems to me that this part is incomplete (some declarations are missing?)

Try:

### Custom Loggers ###
# Demo
log4j2.logger.Demo.name = org.eclipse.smarthome.model.script
log4j2.logger.Demo.level = DEBUG
log4j2.logger.Demo.additivity = false
log4j2.logger.Demo.appenderRefs = Demo
log4j2.logger.Demo.appenderRef.Demo.ref = Demo

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

check: Log4j2 Sample Config also

Ps: Don’t use the example from https://docs.openhab.org/administration/logging.html#logging-into-separate-file since this is for log4j1 (not 2 that you are using)


(autoit) #8

Thanks, that works beautifully and the sample config clears things up as well. If I ever get the time I will figure out how to contribute to the docs :slight_smile: