[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 <dan@digitaldan.com>
This commit is contained in:
parent
37c028ddc0
commit
a0920d4c0c
|
@ -2,13 +2,20 @@
|
||||||
(function (global) {
|
(function (global) {
|
||||||
'use strict';
|
'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 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 ScriptExecution = Java.type('org.openhab.core.model.script.actions.ScriptExecution');
|
||||||
const ZonedDateTime = Java.type('java.time.ZonedDateTime');
|
const ZonedDateTime = Java.type('java.time.ZonedDateTime');
|
||||||
|
|
||||||
const formatRegExp = /%[sdj%]/g;
|
const formatRegExp = /%[sdj%]/g;
|
||||||
|
|
||||||
|
function createLogger(name = defaultLoggerName) {
|
||||||
|
return Java.type("org.slf4j.LoggerFactory").getLogger(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
//user configurable
|
||||||
|
let log = createLogger();
|
||||||
|
|
||||||
function stringify(value) {
|
function stringify(value) {
|
||||||
try {
|
try {
|
||||||
if (Java.isJavaObject(value)) {
|
if (Java.isJavaObject(value)) {
|
||||||
|
@ -140,6 +147,7 @@
|
||||||
timers[label] = System.currentTimeMillis();
|
timers[label] = System.currentTimeMillis();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
timeEnd: function (label) {
|
timeEnd: function (label) {
|
||||||
if (label) {
|
if (label) {
|
||||||
const now = System.currentTimeMillis();
|
const now = System.currentTimeMillis();
|
||||||
|
@ -150,6 +158,16 @@
|
||||||
log.info(format.apply(null, [label + ':', '<no timer>']));
|
log.info(format.apply(null, [label + ':', '<no timer>']));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
//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
|
//Support legacy NodeJS libraries
|
||||||
globalThis.global = globalThis;
|
globalThis.global = globalThis;
|
||||||
globalThis.process = { env: { NODE_ENV: '' } };
|
globalThis.process = { env: { NODE_ENV: '' } };
|
||||||
|
|
||||||
})(this);
|
})(this);
|
||||||
|
|
Loading…
Reference in New Issue