Logging in block library

I’m dipping my toe into block libraries for reusing some more complex things. But struggling with what seems to be the basics. I’ve created a simple library with some JS, but it no worky, so I want to dump out some debug information.

How do I call logs in the block library? I’ve done a copy and paste job from a blockly rule:

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID); 
logger.info('Item {{input:ITEMNAME}}');

But when this gets called I’m getting logger as undefined. Can anybody point me in the right direction? I’ve had a bit of a hunt around other people’s libraries, but I’m not seeing anybody doing this.

jdk.nashorn.internal.runtime.ECMAException: TypeError: Cannot read property "info" from undefined
        at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57) ~[jdk.scripting.nashorn:?]
        at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:213) ~[jdk.scripting.nashorn:?]
        at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:185) ~[jdk.scripting.nashorn:?]
        at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:172) ~[jdk.scripting.nashorn:?]
        at jdk.nashorn.internal.runtime.Undefined.get(Undefined.java:161) ~[jdk.scripting.nashorn:?]
        at jdk.nashorn.internal.scripts.Script$Recompilation$4812$918$\^eval\_.L:34(<eval>:36) ~[?:?]
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:655) ~[jdk.scripting.nashorn:?]
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:513) ~[jdk.scripting.nashorn:?]
        at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:527) ~[jdk.scripting.nashorn:?]
        at jdk.nashorn.javaadapters.org_eclipse_xtext_xbase_lib_Procedures$Procedure0.apply(Unknown Source) ~[?:?]
        at org.openhab.core.internal.scheduler.SchedulerImpl.lambda$12(SchedulerImpl.java:191) ~[?:?]
        at org.openhab.core.internal.scheduler.SchedulerImpl.lambda$1(SchedulerImpl.java:88) ~[?:?]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) [?:?]
        at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) [?:?]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
        at java.lang.Thread.run(Thread.java:829) [?:?]
  • Platform information:
    • Hardware: x64/lots
    • OS: Linux/5.14.21-150400.24.63-default (amd64)
    • Java Runtime Environment: 11.0.19
    • openHAB version: 3.4.4

Given that OH 4 is out and Blockly converts to the newer JS Scripting ECMAScript 2022 as opposed to Nashorn ECMAScript 5.1, you would be wise to focus new work on block libraries on OH 4, particularly if these are libraries you want to publish to the marketplace or otherwise share.

If you do, you’ll be able to use the JS Scripting Helper Library (which comes with the add-on) meaning you don’t have to create the logger in the first place.

Just call console.info(...).

I don’t really do a whole lot with Blockly and it’s been a very long time since I’ve messed with Nashorn JS, but the code you show looks reasonable to me. I couldn’t guess why this code is not working.