Own logfile

Hi,

I need a seperate logfile for a temperatur. I habe OpenHab 5.0.2 and I configured all in the log4j2.xml and a rule to log the values into a seperate logfile. Unfortunately the value is in the openhab.log and not in the temperature.log. Have someone a idee, whats my mistake is?

log4j2.xml:

    <!-- Temperature appender -->

    <RollingRandomAccessFile fileName="${sys:openhab.logdir}/temperature.log" filePattern="${sys:openhab.logdir}/temperature.log.%i.gz" name="TEMPERATURE">

        <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} \[%-5.5p\] \[%-36.36c\] - %m%n"/>

        <Policies>

            <OnStartupTriggeringPolicy/>

            <SizeBasedTriggeringPolicy size="5 MB"/>

        </Policies>

        <DefaultRolloverStrategy max="3"/>

    </RollingRandomAccessFile>
    <!-- Temperature Logger -->

    <Logger additivity="false" level="INFO" name="org.openhab.core.model.script.temperaturelog">

        <AppenderRef ref="TEMPERATURE"/>

    </Logger>

</Loggers>

Rule:

logging.rules:

rule “Write temperature to custom log”

when

Time cron "0 \* \* \* \* ?" //jede Minute

then

val temp = shelly_heizung_temp1.state.toString 

logInfo("org.openhab.core.model.script.temperaturelog", "T1: " + temp)

end

your code goes here

I’m not sure if it’ll suffice but at least you will have to change the rule slightly:

rule “Write temperature to custom log”
when
    Time cron "0 * * * * ?" // once a minute
then
    logInfo("temperaturelog", "T1: {}", shelly_heizung_temp1.state)
end

Please be aware that the logX() action will always use org.openhab.core.model.script. as the first part of the logger name, so you must not write down this part.

Hi,

thank you for the reponse.

First I changed the rule, after that try I deleted this first part from the logger name, but it is not working.

The logger is on The end of the loggers section, 8s this a problem?

Best regards,

Martin

I dont understand it. I tried a lot of variants in the /var/lib/openhab/etc/log4j2.xml but it changed nothing. At the end I edited the standard logfile name openhab.log to openhabtest.log, but it changed nothing as well. After every change I make sudo systemctl restart openhab.

this works fine I took your log4j entries copied them into my openhab log4j.xml
I did however edit out the slashes you had in the pattern
restarted openhab and had a new temperature log file
then created this simple rule based on a shelly device I had that reports internal temperature and it correctly wrote to the new log.

<?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"/>
		</RollingRandomAccessFile>

		<!-- Audit file appender -->
		<RollingRandomAccessFile fileName="${sys:openhab.logdir}/audit.log" filePattern="${sys:openhab.logdir}/audit.log.%i.gz" name="AUDIT">
			<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5.5p] [%-36.36c] - %m%n"/>
			<Policies>
				<OnStartupTriggeringPolicy/>
				<SizeBasedTriggeringPolicy size="8 MB"/>
			</Policies>
			<DefaultRolloverStrategy max="7"/>
		</RollingRandomAccessFile>
        <!-- Temperature appender -->

        <RollingRandomAccessFile fileName="${sys:openhab.logdir}/temperature.log" filePattern="${sys:openhab.logdir}/temperature.log.%i.gz" name="TEMPERATURE">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5.5p] [%-36.36c] - %m%n"/>
            <Policies>
                <OnStartupTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="5 MB"/>
            </Policies>
           <DefaultRolloverStrategy max="3"/>
        </RollingRandomAccessFile>
		<!-- OSGi appender -->
		<PaxOsgi filter="*" name="OSGI"/>
	</Appenders>

	<Loggers>
		<!-- Root logger configuration -->
		<Root level="WARN">
			<AppenderRef ref="LOGFILE"/>
			<AppenderRef ref="OSGI"/>
		</Root>

		<!-- Karaf Shell logger -->
		<Logger level="OFF" name="org.apache.karaf.shell.support">
			<AppenderRef ref="STDOUT"/>
		</Logger>

		<!-- Security audit logger -->
		<Logger additivity="false" level="INFO" name="org.apache.karaf.jaas.modules.audit">
			<AppenderRef ref="AUDIT"/>
		</Logger>
        <!-- Temperature Logger -->
        <Logger additivity="false" level="INFO" name="org.openhab.core.model.script.temperaturelog">
          <AppenderRef ref="TEMPERATURE"/>
        </Logger>
		<!-- openHAB specific logger configuration -->

		<Logger level="INFO" name="org.openhab"/>

		<Logger level="ERROR" name="openhab.event.ItemStateEvent"/>
		<Logger level="ERROR" name="openhab.event.ItemStateUpdatedEvent"/>
		<Logger level="ERROR" name="openhab.event.GroupStateUpdatedEvent"/>
		<Logger level="ERROR" name="openhab.event.ItemAddedEvent"/>
		<Logger level="ERROR" name="openhab.event.ItemRemovedEvent"/>
		<Logger level="ERROR" name="openhab.event.ItemChannelLinkAddedEvent"/>
		<Logger level="ERROR" name="openhab.event.ItemChannelLinkRemovedEvent"/>
		<Logger level="ERROR" name="openhab.event.ChannelDescriptionChangedEvent"/>
		<Logger level="ERROR" name="openhab.event.ThingStatusInfoEvent"/>
		<Logger level="ERROR" name="openhab.event.ThingAddedEvent"/>
		<Logger level="ERROR" name="openhab.event.ThingUpdatedEvent"/>
		<Logger level="ERROR" name="openhab.event.ThingRemovedEvent"/>
		<Logger level="ERROR" name="openhab.event.InboxUpdatedEvent"/>
		<Logger level="ERROR" name="openhab.event.RuleStatusInfoEvent"/>
		<Logger level="ERROR" name="openhab.event.RuleAddedEvent"/>
		<Logger level="ERROR" name="openhab.event.RuleRemovedEvent"/>
		<Logger level="ERROR" name="openhab.event.StartlevelEvent"/>
		<Logger level="ERROR" name="openhab.event.AddonEvent"/>

		<Logger additivity="false" level="INFO" name="openhab.event">
			<AppenderRef ref="EVENT"/>
			<AppenderRef ref="OSGI"/>
		</Logger>

		<Logger level="ERROR" name="javax.jmdns"/>
		<Logger level="ERROR" name="org.jupnp"/>

		<!-- This suppresses all Maven download issues from the log when doing feature installations -->
		<!-- as we are logging errors ourselves in a nicer way anyhow. -->
		<Logger level="ERROR" name="org.ops4j.pax.url.mvn.internal.AetherBasedResolver"/>

		<!-- Filters known issues of pax-web (issue link to be added here). -->
		<!-- Can be removed once the issues are resolved in an upcoming version. -->
		<Logger level="OFF" name="org.ops4j.pax.web.pax-web-runtime"/>

		<!-- 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. -->
		<Logger level="OFF" name="org.eclipse.lsp4j"/>

		<!-- Filters warnings for events that could not be delivered to a disconnected client. -->
		<Logger level="ERROR" name="org.apache.cxf.jaxrs.sse.SseEventSinkImpl"/>

		<!-- Filters warnings from broken connections during startup -->
		<!-- https://github.com/openhab/openhab-core/issues/2998 -->
		<Logger level="ERROR" name="org.apache.cxf.phase.PhaseInterceptorChain"/>

		<!-- 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. -->
		<Logger level="ERROR" name="org.apache.karaf.kar.internal.KarServiceImpl"/>

		<!-- Filters warnings about unavailable ciphers when JCE is not installed, see -->
		<!-- https://github.com/openhab/openhab-distro/issues/999 -->
		<Logger level="ERROR" name="org.apache.karaf.shell.ssh.SshUtils"/>

		<!-- Filters known issues of javax.mail, see -->
		<!-- https://github.com/openhab/openhab-addons/issues/5530 -->
		<Logger level="ERROR" name="javax.mail"/>

		<!-- Filters disconnection warnings of the ChromeCast Java API, see -->
		<!-- https://github.com/openhab/openhab-addons/issues/3770 -->
		<Logger level="ERROR" name="su.litvak.chromecast.api.v2.Channel"/>

		<!-- Added by Karaf to prevent debug logging loops, see -->
		<!-- https://issues.apache.org/jira/browse/KARAF-5559 -->
		<Logger level="WARN" name="org.apache.sshd"/>
		<Logger level="TRACE" name="org.binding.exec"/>
		<Logger level="INFO" name="org.openhab.binding.exec"/>
		<Logger name="org.openhab.binding.shelly"/>
		<Logger name="org.openhab.binding.mideaac"/>
		


	</Loggers>

</Configuration>

and this simple rule.

configuration: {}
triggers:
  - id: "1"
    configuration:
      cronExpression: 0 * * * * ? *
    type: timer.GenericCronTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: 'logInfo("temperaturelog", "T1: {}",
        ShellyPlus_Plug_Device_Status_Internal_Temperature.state)'
    type: script.ScriptAction

and this output

Hi, thanks for your answer.

I copied your xml to my log4j2.xml, but it is not working.

See my picture with the settings. (I dont have slashes in the xml)

Is there anything else that I have to configure?

Additional I cant change the log4j2.xml directly, I must change it with sudo nano or I must copy the new file in tmp and move it to the \openHAB-userdata\etc.

How do you changed it?

No, I did not have to configure anything else I did it exactly as I posted nothing extra.
So, after you applied changes to the xml did you make sure the correct permissions are still on the file?
Did you restart openhab and did it create a new temperaturelog file in your logs folder?
if you have the log4j.xml correct it should create the new log file once you have that then any other issue is going to be with how you did your rules.

I removed the temperature.log to check it, after a restart there is no new temperature.log (I created the temperature.log by myself in the last days for testing).

I always make a sudo systemctl restart openhab after any changes.

How I can check the correct permissions of the xml? I belive the xml is not in use, because I changed the standard logfile name openhab.log to openhabtest.log, but it was still the openhab.log.

The rule is working, I see the trace every 30s in the openhab.log

if you have not gotten the xml file permissioned correctly and owned by openhab then it will not be able to read it and create and configure your new log file.
you check and change permissions on the xml file the same way you check or change permissions on any file in linux.

this statement right here says you have not gotten the xml correct.
If it is correct it will auto create that file with every restart
you need to check your ownership and permissions on that file.
I do not know how your are making these changes I can tell you how I do it (note I am lazy) I use winscp to copy over the file make my changes and copy it back I then change ownership and permissions from a console session. navigate to the correct location for the file then sudo chown username you want own the file here your file name here
so, for this one assuming you are in correct folder it would be
sudo chown openhab log4j2.xml


I got it :blush:.

The file org.ops4j.pax.logging.cfg in the same directory had the wrong content (I dont know why). Default is org.ops4j.pax.logging.log4j2.config.file=${karaf.etc}/log4j2.xml, but this line wasnt in the file. Instead this line there were the loggers and appenders configurations. I changed the content and I got immediately the temperature.log (without a restart).

Thank you for your help.

1 Like

With the infos of this post, I got the status-logging of my DSL-Rules to an own logfile working. But only when I remove the additivity=“false” parameter in the definition of the logger, I get log data written to my own logfile.

Can someone explain, what this parameter is good for?

Another thing is, that on inspecting the logfiles in /var/log/openhab what I see, is that now my own log entries are written to my own logfile and additionally to the openhab.log. Is this normal behaviour?
I’m on a RASPI 3B+ with openhab 4.3.10-1, but I think this shouldn’t matter(?)

1 Like

Thanks a lot. That helps.