浏览代码

dev: add injected logger support

This allows a logger to be specified in the execution context. If
LogService sees this, it will perform its usual logging but also call
the injected logger.
KernelDeimos 1 月之前
父节点
当前提交
a3ae60861c
共有 2 个文件被更改,包括 8 次插入1 次删除
  1. 5 0
      src/backend/src/modules/core/LogService.js
  2. 3 1
      src/backend/src/services/CommandService.js

+ 5 - 0
src/backend/src/modules/core/LogService.js

@@ -112,6 +112,11 @@ class LogContext {
                 typeof fields[k].toLogFields === 'function'
             ) fields[k] = fields[k].toLogFields();
         }
+        if ( Context.get('injected_logger') ) {
+            Context.get('injected_logger').log(
+                message + (fields ? ('; fields: ' + JSON.stringify(fields)) : ''),
+            );
+        }
         this.logService.log_(
             log_level,
             this.crumbs,

+ 3 - 1
src/backend/src/services/CommandService.js

@@ -134,7 +134,9 @@ class CommandService extends BaseService {
         * @returns {Promise<void>}
         * @throws {Error} If command execution fails
         */
-        await globalThis.root_context.arun(async () => {
+        await globalThis.root_context.sub({
+            injected_logger: log,
+        }).arun(async () => {
             await command.execute(commandArgs, log);
         });
     }