From 40a557edb15d61f0633899bd783b1baebd873616 Mon Sep 17 00:00:00 2001 From: Daniel Lando Date: Tue, 17 Sep 2024 11:57:16 +0200 Subject: [PATCH] feat: always show driver/app logs on debug UI even when log settings are disabled (#3884) --- api/app.ts | 21 +++------------------ api/lib/ZwaveClient.ts | 11 +++++++++++ api/lib/logger.ts | 12 ++++++++++++ package-lock.json | 18 ++++++++++++++++++ package.json | 1 + src/views/Debug.vue | 39 +++++++-------------------------------- test/lib/logger.test.ts | 18 +++++++++--------- 7 files changed, 61 insertions(+), 59 deletions(-) diff --git a/api/app.ts b/api/app.ts index a3f077c2521..a05cefcd08d 100644 --- a/api/app.ts +++ b/api/app.ts @@ -440,24 +440,9 @@ async function destroyPlugins() { function setupInterceptor() { // intercept logs and redirect them to socket - const interceptor = ( - write: ( - buffer: string | Uint8Array, - cb?: (err?: Error) => void, - ) => void, - ) => { - return function (...args: any[]): boolean { - socketManager.io.emit(socketEvents.debug, args[0]?.toString()) - return write.apply(process.stdout, args) - } - } - - process.stdout.write = interceptor( - process.stdout.write.bind(process.stdout), - ) - process.stderr.write = interceptor( - process.stderr.write.bind(process.stderr), - ) + loggers.logStream.on('data', (chunk) => { + socketManager.io.emit(socketEvents.debug, chunk.toString()) + }) } async function parseDir(dir: string): Promise { diff --git a/api/lib/ZwaveClient.ts b/api/lib/ZwaveClient.ts index ff480b3f894..0fe09eeec77 100644 --- a/api/lib/ZwaveClient.ts +++ b/api/lib/ZwaveClient.ts @@ -16,7 +16,9 @@ import { ZWaveDataRate, ZWaveErrorCodes, Protocols, + createDefaultTransportFormat, } from '@zwave-js/core' +import { JSONTransport } from '@zwave-js/log-transport-json' import { isDocker } from '@zwave-js/shared' import { AssociationAddress, @@ -2259,6 +2261,15 @@ class ZwaveClient extends TypedEventEmitter { utils.parseSecurityKeys(this.cfg, zwaveOptions) + const logTransport = new JSONTransport() + logTransport.format = createDefaultTransportFormat(true, false) + + zwaveOptions.logConfig.transports = [logTransport] + + logTransport.stream.on('data', (data) => { + this.socket.emit(socketEvents.debug, data.message.toString()) + }) + try { // init driver here because if connect fails the driver is destroyed // this could throw so include in the try/catch diff --git a/api/lib/logger.ts b/api/lib/logger.ts index 831e115089a..0d053c0eccc 100644 --- a/api/lib/logger.ts +++ b/api/lib/logger.ts @@ -10,6 +10,7 @@ import * as path from 'path' import { readdir, stat, unlink } from 'fs/promises' import { Stats } from 'fs' import escapeStringRegexp from '@esm2cjs/escape-string-regexp' +import { PassThrough } from 'stream' const { format, transports, addColors } = winston const { combine, timestamp, label, printf, colorize, splat } = format @@ -101,6 +102,9 @@ export function customFormat( return combine(...formats) } + +export const logStream = new PassThrough() + /** * Create the base transports based on settings provided */ @@ -117,6 +121,14 @@ export function customTransports(config: LoggerConfig): winston.transport[] { ) } + const streamTransport = new transports.Stream({ + format: customFormat(config), + level: config.level, + stream: logStream, + }) + + transportsList.push(streamTransport) + if (config.logToFile) { let fileTransport: winston.transport if (process.env.DISABLE_LOG_ROTATION === 'true') { diff --git a/package-lock.json b/package-lock.json index 5e4e3417747..85971160a52 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "@jamescoyle/vue-icon": "^0.1.2", "@kvaster/zwavejs-prom": "^0.0.2", "@mdi/js": "7.4.47", + "@zwave-js/log-transport-json": "^3.0.0", "@zwave-js/server": "^1.38.0", "ansi_up": "^6.0.2", "archiver": "^7.0.1", @@ -4834,6 +4835,23 @@ "url": "https://github.com/sponsors/AlCalzone/" } }, + "node_modules/@zwave-js/log-transport-json": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@zwave-js/log-transport-json/-/log-transport-json-3.0.0.tgz", + "integrity": "sha512-oTUlEmU+LgtxWxxJoEJ6/YY0vsm7yO2CCC1JixtDK2ymq5IjzpGReAmKbIOoHN7jqSN7e27b5GALGtZLsfzYow==", + "engines": { + "node": ">=14.13.0 <15 || >= 16 <16.9.0 || >16.9.0" + }, + "funding": { + "url": "https://github.com/sponsors/AlCalzone/" + }, + "peerDependencies": { + "@zwave-js/core": ">=10.0.0", + "@zwave-js/shared": ">=10.0.0", + "winston": "^3.7.2", + "winston-transport": "^4.5.0" + } + }, "node_modules/@zwave-js/nvmedit": { "version": "13.3.0", "resolved": "https://registry.npmjs.org/@zwave-js/nvmedit/-/nvmedit-13.3.0.tgz", diff --git a/package.json b/package.json index 5eaa424cb04..bd882c16c99 100644 --- a/package.json +++ b/package.json @@ -65,6 +65,7 @@ "@jamescoyle/vue-icon": "^0.1.2", "@kvaster/zwavejs-prom": "^0.0.2", "@mdi/js": "7.4.47", + "@zwave-js/log-transport-json": "^3.0.0", "@zwave-js/server": "^1.38.0", "ansi_up": "^6.0.2", "archiver": "^7.0.1", diff --git a/src/views/Debug.vue b/src/views/Debug.vue index 3e63a8468ab..73874dffe66 100644 --- a/src/views/Debug.vue +++ b/src/views/Debug.vue @@ -1,33 +1,6 @@