Write my own log from the rule

I have running rule with more tasks inside and I want write my own info strings into any txt file, because later I want check. Is it possible ?

I tried to use

logInfo("LoggerName", "LoggingText")

But it doesn’t work

Thank you

update :
sorry
instruction

logInfo("LoggerName", "LoggingText")

is working, but it will save text into openhab.log
and I’m not sure how often is this file cleared (minimal I want store data from last week)
and looks like that “LoggerName” is information which isn’t used

Take a look at the above. Should give you some ideas

Thanks, but don’t understand how to implement into rule writen in DSL

Two items to do:

  • modify the log4j2 logger configuration file ( /var/lib/openhab/etc/log4j2.xml )
    This is described in the mentioned thread an other example is in Logging to seperate log file OpenHab 3
  • in you DSL rule you trigger logging as you already do
logInfo("LoggerName", "LoggingText")

The entry in log4j2.xml needs to be adjusted for your rule/logging.
In your case the logger needs to be named
name="org.openhab.core.model.script.LoggerName"

oh, now it’s little bit more clear for me.
So I will add this ? :

<!-- my logger -->
                <Logger additivity="false" level="INFO" name="org.openhab.core.model.script.LoggerName">
                        <AppenderRef ref="TEST"/>
                </Logger>
<!-- my logger -->

and where to add into xml ?

So on the end of the /var/lib/openhab/etc/log4j2.xml I added :

	</Loggers>


 <!-- alda appender -->
                <RollingRandomAccessFile fileName="${sys:openhab.logdir}/alda.log" filePattern="${sys:openhab.logdir}/alda.log.%i" name="ALDA">
                        <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5.5p] [%-36.36c] - %m%n"/>
                        <Policies>
                                <OnStartupTriggeringPolicy/>
                                <SizeBasedTriggeringPolicy size="16 MB"/>
                        </Policies>
                </RollingRandomAccessFile>
<!-- alda -->
<!-- alda Logger -->
                <Logger additivity="false" level="INFO" name="org.openhab.core.model.script.alda">
                        <AppenderRef ref="ALDA"/>
                </Logger>
<!-- alda -->

</Configuration>

and into rule I added :
logInfo(“alda”, “TEST”)

I can see that alda.log file was created but logInfo I can find in the openhab.log :

2023-05-31 21:40:17.502 [INFO ] [org.openhab.core.model.script.alda  ] -

The part

<!-- alda appender -->
                <RollingRandomAccessFile fileName="${sys:openhab.logdir}/alda.log" filePattern="${sys:openhab.logdir}/alda.log.%i" name="ALDA">
                        <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5.5p] [%-36.36c] - %m%n"/>
                        <Policies>
                                <OnStartupTriggeringPolicy/>
                                <SizeBasedTriggeringPolicy size="16 MB"/>
                        </Policies>
                </RollingRandomAccessFile>
<!-- alda -->

Needs to be moved into the existing Appenders section

<Appenders>
...
</Appenders>

The second part ( logger )

<!-- alda Logger -->
                <Logger additivity="false" level="INFO" name="org.openhab.core.model.script.alda">
                        <AppenderRef ref="ALDA"/>
                </Logger>
<!-- alda -->

Needs to be moved into the existing Loggers section

<Loggers>
...
</Loggers>

So I changed like this:

<!-- 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>

		<!-- Alda custom file appender -->
		<RollingRandomAccessFile fileName="${sys:openhab.logdir}/alda.log" filePattern="${sys:openhab.logdir}/alda.log.%i" name="ALDA">
			<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5.5p] [%-36.36c] - %m%n"/>
			<Policies>
				<OnStartupTriggeringPolicy/>
				<SizeBasedTriggeringPolicy size="16 MB"/>
			</Policies>
		</RollingRandomAccessFile>

		<!-- OSGi appender -->
		<PaxOsgi filter="*" name="OSGI"/>
	</Appenders>

and:

		<!-- Security audit logger -->
		<Logger additivity="false" level="INFO" name="org.apache.karaf.jaas.modules.audit">
			<AppenderRef ref="AUDIT"/>
		</Logger>

		<!-- Alda custom logger -->
		<Logger additivity="false" level="INFO" name="org.openhab.binding.alda">
			<AppenderRef ref="ALDA"/>
		</Logger>

		<!-- openHAB specific logger configuration -->

then in the rule :
logInfo(“alda”, “TEST”)

result is still the same
file alda-log exists but data I can find in the openhab.log:

2023-05-31 22:10:00.945 [INFO ] [org.openhab.core.model.script.alda  ] - 

This is for a binding not for logging by a rule called e.g. via DSL

should be

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

as you had it before.

yes I already found my error
Yes now it’s working
thanks

1 Like

So log is working, BUT there is only few records (opened via WinSCP) :

Don’t know why, but I need much loger time period

a) I do not understand what you would like to do
b) I do not understand what you tried
c) in case the content of the screenshot is of interest I can’t interpret it as the main part is missing ( screenshots might not be good to read depending on the device you use to display them )
d) please be more detailed in describing your problem

I’m writing my own log file but date in the file are only from the time as on my screenshoot - 17:20:00.467 to 17:40:00.618 - but I need all data sent do this log file not only 12 records. Rest isn’t there.

update - now in the log file I have data 17:20:00.467 to 18:10:00.624 but not before this time

Sounds like it’s working. The logger config isn’t retroactive. Once you got the logger configuration correct, all entries from that point forward should be logged. It won’t go back and log anything that happened before that point though.

If you want to save those old logs (only for org.openhab.core.model.script.alda), you can filter this out from openhab.log:

cat /var/log/openhab/openhab.log | sudo grep model.script.alda >> /var/log/openhab/alda0.log

is the log rotated ( are there other files in the same directory like alda, alda.1.gz, alda.2.gz ) ?

no there isn’t any other file like alda.1.gz

Log saving into alda.log started yesterday evening, but in the alda.log I can see now only this records :

so records which was there (17:20:00.467 to 18:10:00.624) are away

openhab.log is empty

When you restart OH it will rotate the logs. That’s what the OnStartupTriggeringPolicy does. It will also rotate when the log file gets to 16 MB in size.

When it rotates it compresses the old file and adds a number to the file name. For example openhab.log.1.gz.

If you are on openHABian with a default config you have ZRAM configured. In that case all logs are written to a RAM disk meaning they are not saved to the SD card. During a normal shutdown the contents of the RAM disk will be flushed to the SD card. However, if the system crashes or you yank the power, everything written to the logs since the last time the RPi started will be lost.

Completely empty or just empty of logs from this alda rule?