From: Dan Cunningham Date: Mon, 31 Jan 2022 10:55:50 +0000 (-0800) Subject: [jsscripting] Append the ruleID or file name to the logger when console logging ... X-Git-Url: https://git.basschouten.com/?a=commitdiff_plain;h=a0920d4c0cdba55f9a5bb32e7f85bde6884fce47;p=openhab-addons.git [jsscripting] Append the ruleID or file name to the logger when console logging (#11945) * Appends the ruleID or file name to the logger when console logging. * Adds configurable logging, updates scriptId logic Signed-off-by: Dan Cunningham --- diff --git a/bundles/org.openhab.automation.jsscripting/src/main/resources/node_modules/@jsscripting-globals.js b/bundles/org.openhab.automation.jsscripting/src/main/resources/node_modules/@jsscripting-globals.js index d60a905174..f956729ad6 100644 --- a/bundles/org.openhab.automation.jsscripting/src/main/resources/node_modules/@jsscripting-globals.js +++ b/bundles/org.openhab.automation.jsscripting/src/main/resources/node_modules/@jsscripting-globals.js @@ -2,13 +2,20 @@ (function (global) { 'use strict'; + //Append the script file name OR rule UID depending on which is available + const defaultLoggerName = "org.openhab.automation.script" + (globalThis["javax.script.filename"] ? ".file." + globalThis["javax.script.filename"].replace(/^.*[\\\/]/, '') : globalThis["ruleUID"] ? ".ui." + globalThis["ruleUID"] : ""); const System = Java.type('java.lang.System'); - const log = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.automation.script"); const ScriptExecution = Java.type('org.openhab.core.model.script.actions.ScriptExecution'); const ZonedDateTime = Java.type('java.time.ZonedDateTime'); - const formatRegExp = /%[sdj%]/g; + function createLogger(name = defaultLoggerName) { + return Java.type("org.slf4j.LoggerFactory").getLogger(name); + } + + //user configurable + let log = createLogger(); + function stringify(value) { try { if (Java.isJavaObject(value)) { @@ -140,6 +147,7 @@ timers[label] = System.currentTimeMillis(); } }, + timeEnd: function (label) { if (label) { const now = System.currentTimeMillis(); @@ -150,6 +158,16 @@ log.info(format.apply(null, [label + ':', ''])); } } + }, + + //Allow user customizable logging names + set loggerName(name) { + log = createLogger(name); + this._loggerName = name; + }, + + get loggerName() { + return this._loggerName || defaultLoggerName; } }; @@ -200,5 +218,4 @@ //Support legacy NodeJS libraries globalThis.global = globalThis; globalThis.process = { env: { NODE_ENV: '' } }; - })(this);