Seeking help: Redirecting openhab-js script logs to a separate file not working

Hi everyone,

I am trying to redirect the output of a specific JavaScript (GraalJS) script into its own log file, but it still doesn’t seem to work. The logs don’t appear in the new file

1. My Script: I initialized the logger like this:

JavaScriptvar logger = log('WeatherWarnings');
logger.info("Test message");

2. My log4j2.xml configuration: I added the following Appender and Logger:

<RollingFile fileName="${sys:openhab.logdir}/weather-warnings.log" filePattern="${sys:openhab.logdir}/weather-warnings.log.%i.gz" name="WEATHER_FILE">
    <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5.5p] [%-36.36c] - %m%n"/>
    <Policies>
        <SizeBasedTriggeringPolicy size="8 MB"/>
    </Policies>
</RollingFile>

<Logger additivity="false" level="INFO" name="org.openhab.automation.openhab-js.WeatherWarnings">
    <AppenderRef ref="WEATHER_FILE"/>
</Logger>

3. What I see in the logs: In the standard openhab.log, the entries look like this: [INFO ] [org.openhab.automation.openhab-js.WeatherWarnings] - Test message

The Problem: Even though the names seem to match, the file weather-warnings.log is not being created (or remains empty). I have already checked file permissions for the log folder.

Is there a specific prefix I’m missing, or do I need to trigger a refresh differently than just saving the .xml file?

Any help is appreciated!

I’ve never used RollingFile as an appender. The docs seem to indicate it works the same as RollingRandomAccessFile appender (which is what the built in log configurations in log4j2.xml use). That’s the only difference I see compared to what I use for custom loggers.

Here’s an example from my config for comparison just in case:

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


                <Logger additivity="false" level="TRACE" name="org.openhab.binding.honeywell">
                        <AppenderRef ref="HONEYWELL"/>
                </Logger>

Obviously, the appender goes in the appenders section and the logger goes in the loggers section.

This is a little ambiguous. Is it not created and remains empty if you create it, or sometimes it is created and sometimes it isn’t?

Maybe a refresh? I’ve always had changes made to log4j2.xml picked up immediately upon saving it. But if this file was edited through SAMBA or something like that maybe the file event gets lost. Maybe try touching the file or restarting OH.

If that doesn’t work, post your full log4j2.xml file. Maybe there’s a syntax error in it somewhere.

I have successfully configured a custom log file and fill it with KNX diagnosis through JS Scripting, see: openhab-conf/README.md at main · florian-h05/openhab-conf · GitHub

1 Like

So I need to use


org.openhab.logging.xxxx

Instead of

org.openhab.automation.openhab-js.xxx

xxx is the name I used

So in my example it is

org.openhab.logging.WeatherWarnings

I don’t think you need to do it that way, my post was mainly meant to provide an example that works for me.

Using a logger name without a dash - in it would be worth a try though, maybe the dash is causing issues.

Did you succeed?

Haven’t tested further was today again whole day in the hospital and than some wled measurements for the led curtain plugin…will check during the week…

1 Like