Javascript - logging into different file

I want to monitor the behaviour of a few items over the course of a day or two. So the standard log is not very convenient. Therefore I thought of writing a Javascript rule that would log state changes/updates to a different log file.

But I don’t the documentation about it… (actions - Documentation & JavaScript Scripting - Automation | openHAB).

I assume it is possible to log to a different file?

Thanks in advance for your help!

It would be easier to just use grep to extract the lines that mention your Item(s) from events.log. You can even redirect that into another file if you need to for some reason.

grep MyItemName events.log > MyItemName.log

There are equivalent commands in PowerShell. See How to watch and look through logging for more details.

To cause any logger to log to another file, you’ll need to edit /var/lib/openhab/etc/log4j2.xml by adding a new log appender which writes to a file (use the openhab.log one as a template) and a new logger that maps the logger name from the rule to use that appender.

When you use console to log the logger name is org.openhab.automation.script.ui.<ruleUID>. If it’s a file based rule I think it’s <filename> instead. If you created your own logger, you already know the logger name.

Maybe this helps?
https://community.openhab.org/t/logging-own-log-entries-into-different-own-log-files/48166/8

I tried fiddling a bit, but not to a satisfactory result. The log file has been created, but nothing is logged into it.

Unfortunately, I wasn’t able to extract all the necessary info from the discussion regarding openHAB 2…

This is what I added to /var/lib/openhab/etc/log4j2.xml for now:

	<Appenders>
		
                (...)

		<!-- Zelfgemaakte appender voor de venster-verwarmingsaga -->
		<RollingRandomAccessFile fileName="${sys:openhab.logdir}/venster-verwarming.log" filePattern="${sys:openhab.logdir}/venster-verwarming.log.%i.gz" name="VENSTERVERWARMING">
			<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>
	</Appenders>

	<Loggers>

                (...)

		<!-- Zelfgemaakte audit logger voor de venster-verwarmingsaga -->
		<Logger additivity="false" level="INFO" name="org.apache.karaf.jaas.modules.audit">
			<AppenderRef ref="VENSTERVERWARMING"/>
		</Logger>

                (...)

	</Loggers>

</Configuration>

I assume something has to be altered to org.apache.karaf.jaas.modules.audit?

I didn’t see anything obviously wrong with the config. Make sure the name matches the longer you are using in your rules. It is very unlikely “org.apache.karaf.jaas.modules.audit” is it. See my post above for what the default logger name would be if you are not changing it in your rule.

Aha, I figured out where to put what you hinted to :wink:

<Appenders>
		
                (...)

		<!-- Zelfgemaakte appender voor de venster-verwarmingsaga -->
		<RollingRandomAccessFile fileName="${sys:openhab.logdir}/venster-verwarming.log" filePattern="${sys:openhab.logdir}/venster-verwarming.log.%i.gz" name="VENSTERVERWARMING">
			<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>
	</Appenders>

	<Loggers>

                (...)

		<!-- Zelfgemaakte audit logger voor de venster-verwarmingsaga -->
		<Logger additivity="false" level="INFO" name="org.openhab.automation.script.file.vensterVerwarming.js">
			<AppenderRef ref="VENSTERVERWARMING"/>
		</Logger>

                (...)

	</Loggers>

</Configuration>

The name of the rule file is vensterVerwarming.js.

And it works now. :slight_smile:

1 Like