How to set log into javascript transformation (OH3)

Hi all,

I have a javascript to try to debug, but I’m not able to see any log in my openhab.log.

Please consider this is my first javascript rule :frowning:

(function(i) {
    var log = Java.type("org.slf4j.LoggerFactory").getLogger("meteoalarm-wind");
    log("the script is starting");
    var value = /.*aw1([1-4])\..*/.exec(i);
    log.debug("Value" + value);
    if (value === null) {
        return 0;
    }
    if (value[1] > 1 && value[1] < 5) {
       return value[1];
    }
    return 0
})(input)

As you can see, I’ve tried a lot based on few examples found in the community, but I’m still not able to activate my logs.

Any help will be appreciated :frowning:
Andrea

Hey. The first line seems about right
var log = Java.type("org.slf4j.LoggerFactory").getLogger("meteoalarm-wind");

Have you defined the log level from the openHAB Console?
I believe the default logging level is above DEBUG. try using log.error(someString).
If you see output on the console’s log:tail command, then that’s your issue.

To fix it, run log:set TRACE meteoalarm-wind from the openHAB Console.
Instead of TRACE you can also use the other log levels. Anything below the set level will not be logged.

The levels are (from most verbose to least verbose):

TRACE
DEBUG
INFO
WARN
ERROR
FATAL
OFF

There is also DEFAULT. This uses the default log level.

Comment; this is not a javascript rule, this little script is deployed as a transformation. (Hence the need for logging, because it is a horrible place to debug code).
So far as I know it does not matter to the code, but this will affect test procedures.

1 Like

I have no idea and no expectation that logging even works in a JS transformation.

For clarity it is important to use the right terminology. This is not a rule. This is a transformation.

That will create a logger named “meteoalarm-wind”. There is no logger or appender defined for a logger by that name in log4j2.xml. So you will have to either add a config to that file to enable loggers by that name to log out, or you will have to use a name for the logger that is already configured to be logged.

By default when using the openHAB Log Actions or loggers from the Helper Libraries the names of the loggers start with org.openhab.model.script.. When you look in log4j2.xml you will see that all loggers at that name and below are configured to log at the INFO level to openhab.org. So, assuming that logging from a transformation can work at all, which is not a guarantee, I recommend using org.openhab.model.script.meteoalarm-wind as the name of the logger.

log is an Object, not a function. And there is no specification of the level you want the statement logged out as. A typical way to log at the INFO level would be log.info("the script is starting"); You did it correctly on the second log statement but…

unless you change the logging config this statement won’t be logged out because the level for the logger is INFO by default, not DEBUG.

Again, this is how you would use the slf4j logger in a JavaScript Rule. I have no idea and no expectation that it will work at all in a JavaScript Transformation.

1 Like

Thank you @rlkoshak for your support here.

I’ve added this line in log4j2.xml in the Loggers section:

<Logger level="INFO" name = "org.openhab.model.script.meteoalarm-wind"/>

then I’ve modified the script above accordingly:

// variable "input" contains data passed by OpenHAB binding
//  function scrapes image filename
(function(i) {
    var log = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.meteoalarm-wind");
    log.info("meteoalarm-wind transform is starting")
    var value = /.*aw3([1-4])\..*/.exec(i);
    log.info("value is" + value)
    if (value === null) {
        return 0;
     }
    if (value[1] > 1 && value[1] < 5) {
       return value[1];
    } 
    return 0;    
})(input)

Unfortunately I’m not able to see any log about this transformation. And I don’t know where is my issue. In facts, the regex is doing the right job, and if the value is between 2 and 4 I see in my items. But if the regex is not matching anything, the result should be null (and not the array), and in that case I would update the item with a “0” but nothing happens.

I’m a bit stuck and I was thinking adding logs would helped a lot :frowning:

here my previous post about the issue (apologies for wrong words, and concepts … I’m playing with JS for the first time :slight_smile: ) :

Thanks
Andrea

edit: just tried the script right now again, and even if the value is between 2 and 4 for a specific alert, I’m not able to update the item. Nothing … very difficult troubleshooting without a log :frowning:

Be careful about trying to log out something that is null.
If that breaks because it is not a string, the code stops and nothing will be returned. (Well, the transformation framework intervenes at that point and passes you the entire input instead)

first step :slight_smile:

here the script

// variable "input" contains data passed by OpenHAB binding
//  function scrapes image filename
(function(i) {
    var log = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.meteoalarm-wind");
    var value = /.*aw10([1-4])\..*/.exec(i);
    log.info("Rain Value is " + value[1])
    log.info("Rain Value is " + value)
    if (value[1] > 1 && value[1] < 5) {
       return value[1];
    } 
    if (value === null) {
        return 0;
    }   
})(input)

at this moment in time I can log if the alert is present, with a value between 2 and 4. Null is still a mystery :frowning: if the regex not matches, the log is not reporting anything … the issue is there.

2021-04-20 19:31:23.954 [INFO ] [openhab.model.script.meteoalarm-wind] - Rain Value is 2
2021-04-20 19:31:23.959 [INFO ] [openhab.model.script.meteoalarm-wind] - Rain Value is 								<img src="/theme/common/pictures/aw102.jpg">,2

Andrea

shall I log the value reporting null, perhaps converting to String?

an output like I’m expecting with

var value = /.*aw10([1-4])\..*/.test(i);

?

aargh you’ve undone all the good work in the other thread. Stop confusing us and you by working in two places and doing two things at once.

If the regex does not match and returns null, this code will blow up and die on your first if() trying to make an array out of null.
It doesn’t matter what you do after that,because it’s dead and that code doesn’t get run.

The loggers defined in the config file inherit from higher up loggers. So org.openhab is defined as a logger with level INFO meaning that anything under org.openhab, like org.openhab.model.script.meteoalarm-wind will already be configured to log out to openhab.log at the INFO level. So the line you added isn’t really necessary. Though it certainly shouldn’t be a problem either.

Well, like I said at least twice in my original reply, and will repeat here again, I’ve never heard of someone logging from a transform and I will not be surprised if you cannot log from a JS transformation. Assuming you do everything correctly as one would do in a JavaScript rule, which appears to be the case, and you still don’t get any logs that strongly implies you cannot log from a JS transform.

OK, that’s good news. It means one can log from a JS transformation.

This is good to know as it means that perhaps more stuff is also possible from a JS transformation. For example, perhaps the ItemRegistry can be pulled down and the state of some other Item can be used to transform the state. That is a much asked for request.

As for the null thing, as rossko57 indicates, null isn’t an array. There is no such thing as null[1]. You have to test for null first.

Note, It’s also possible that the chained transformations do not work the way you think. I don’t use the HTTP binding but I believe it works the same as the MQTT binding. In the MQTT binding, if the REGEX doesn’t match anything the transformation just stops. It doesn’t return null to the next transformation in the chain. The event is essentially just thrown away. That is by design as the REGEX part is used as a filter so the next transformation in the chain only processes relevant messages.

Assuming that the HTTP binding works the same, you’ll have to abandon the REGEX transform and do it all in the JS transform, or you’ll have to abandon setting the Item to 0 when the REGEX fails to match anything.

As it happens, this is the whole reason to use a JS transform here, which does its own regex-ing - the idea is to"do something" even if the regex “fails”. Not possible with a REGEX transformation, which will just fizzle out on a no-match.

All this has got lost by smearing the issues across two threads.

1 Like

I think you are exaggerating here :wink:

I’ve just clarified here why I was trying to log into my javascript “transformation”.

Nothing is lost here, we found we can log, and most likely I’ve solved the issue in the script.

Thanks to this great community, in particular you, J-N-K and Rick.

I thank all of you, guys :slight_smile:

Andrea

Make sure you are running the correct version of Java.
I could not do any logging in javascript transformations, rules did not want to run, etc…
Got fixed when I went from Java 16 → Java 11.