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 840f316c2c5bd..e9f46e8fec318 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 @@ -1,21 +1,38 @@ // ThreadsafeTimers is injected into the JS runtime +// The identifier for ThreadsafeTimers is set inside the openhab-js library and only changed here when the logger name is changed +// The default identifier of the script has to be computed on script executon, as it is not available at the time of script compilation (function (global) { 'use strict'; // Append the script file name OR rule UID depending on which is available - const defaultIdentifier = '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 formatRegExp = /%[sdj%]/g; - // Pass the defaultIdentifier to ThreadsafeTimers to enable naming of scheduled jobs - ThreadsafeTimers.setIdentifier(defaultIdentifier); - function createLogger (name = defaultIdentifier) { - return Java.type('org.slf4j.LoggerFactory').getLogger(name); + /** + * Gets the default identifier of the script. + * @returns {string} + */ + function getDefaultIdentifier () { + return 'org.openhab.automation.script' + (globalThis['javax.script.filename'] ? '.file.' + globalThis['javax.script.filename'].replace(/^.*[\\\/]/, '') : globalThis.ruleUID ? '.ui.' + globalThis.ruleUID : ''); } - // User configurable - let log = createLogger(); + /** + * Gets a logger instance for given name. + * @param {string} [name] optional logger name, defaults to the default identifier of the script (see {@link getDefaultIdentifier}) + * @returns {*} logger instance + */ + function getLogger (name) { + if (typeof name === 'string') { + log = Java.type('org.slf4j.LoggerFactory').getLogger(name); + } + if (log === null) { + log = Java.type('org.slf4j.LoggerFactory').getLogger(getDefaultIdentifier()); + } + return log; + } + + let log = null; function stringify (value) { try { @@ -86,7 +103,7 @@ const console = { assert: function (expression, message) { if (!expression) { - log.error(message); + getLogger().error(message); } }, @@ -102,32 +119,32 @@ // update counters[label] = ++counter; - log.debug(format.apply(null, [label + ':', counter])); + getLogger().debug(format.apply(null, [label + ':', counter])); } }, debug: function () { - log.debug(format.apply(null, arguments)); + getLogger().debug(format.apply(null, arguments)); }, info: function () { - log.info(format.apply(null, arguments)); + getLogger().info(format.apply(null, arguments)); }, log: function () { - log.info(format.apply(null, arguments)); + getLogger().info(format.apply(null, arguments)); }, warn: function () { - log.warn(format.apply(null, arguments)); + getLogger().warn(format.apply(null, arguments)); }, error: function () { - log.error(format.apply(null, arguments)); + getLogger().error(format.apply(null, arguments)); }, trace: function () { - log.trace(new Error(format.apply(null, arguments)).stack.replace(/^Error: /, '')); + getLogger().trace(new Error(format.apply(null, arguments)).stack.replace(/^Error: /, '')); }, time: function (label) { @@ -140,10 +157,10 @@ if (label) { const now = System.currentTimeMillis(); if (timers.hasOwnProperty(label)) { - log.info(format.apply(null, [label + ':', (now - timers[label]) + 'ms'])); + getLogger().info(format.apply(null, [label + ':', (now - timers[label]) + 'ms'])); delete timers[label]; } else { - log.info(format.apply(null, [label + ':', ''])); + getLogger().info(format.apply(null, [label + ':', ''])); } } }, @@ -151,13 +168,13 @@ // Allow user customizable logging names // Be aware that a log4j2 required a logger defined for the logger name, otherwise messages won't be logged! set loggerName (name) { - log = createLogger(name); + getLogger(name); this._loggerName = name; ThreadsafeTimers.setIdentifier(name); }, get loggerName () { - return this._loggerName || defaultIdentifier; + return this._loggerName || getDefaultIdentifier(); } };