]> git.basschouten.com Git - openhab-addons.git/commitdiff
[jsscripting] Append the ruleID or file name to the logger when console logging ...
authorDan Cunningham <dan@digitaldan.com>
Mon, 31 Jan 2022 10:55:50 +0000 (02:55 -0800)
committerGitHub <noreply@github.com>
Mon, 31 Jan 2022 10:55:50 +0000 (11:55 +0100)
* 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>
bundles/org.openhab.automation.jsscripting/src/main/resources/node_modules/@jsscripting-globals.js

index d60a905174028f03ad3cdd1915c2d8d15330c170..f956729ad66afd38ab1997935349a619f535dbb7 100644 (file)
@@ -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)) {
                 timers[label] = System.currentTimeMillis();
             }
         },
+
         timeEnd: function (label) {
             if (label) {
                 const now = System.currentTimeMillis();
                     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;
         }
     };
 
     //Support legacy NodeJS libraries 
     globalThis.global = globalThis;
     globalThis.process = { env: { NODE_ENV: '' } };
-
 })(this);