Skip to content

Commit

Permalink
fix: intercept logs using stream transports
Browse files Browse the repository at this point in the history
Fixes #3875
  • Loading branch information
robertsLando committed Sep 12, 2024
1 parent 211857c commit c93dfa7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 deletions.
20 changes: 3 additions & 17 deletions api/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,24 +440,10 @@ 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)
}
loggers.logStream._write = function (data, _, next) {
socketManager.io.emit(socketEvents.debug, data.toString())
next()
}

process.stdout.write = interceptor(
process.stdout.write.bind(process.stdout),
)
process.stderr.write = interceptor(
process.stderr.write.bind(process.stderr),
)
}

async function parseDir(dir: string): Promise<StoreFileEntry[]> {
Expand Down
9 changes: 9 additions & 0 deletions api/lib/ZwaveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -2259,6 +2261,13 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {

utils.parseSecurityKeys(this.cfg, zwaveOptions)

const logTransport = new JSONTransport()
logTransport.format = createDefaultTransportFormat(true, false)

zwaveOptions.logConfig.transports = [logTransport]

logTransport.stream.pipe(LogManager.logStream)

try {
// init driver here because if connect fails the driver is destroyed
// this could throw so include in the try/catch
Expand Down
15 changes: 15 additions & 0 deletions api/lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { Writable } from 'stream'

const { format, transports, addColors } = winston
const { combine, timestamp, label, printf, colorize, splat } = format
Expand Down Expand Up @@ -101,6 +102,12 @@ export function customFormat(

return combine(...formats)
}

export const logStream = new Writable()
logStream._write = function (chunk, encoding, done) {
// noop
done()
}
/**
* Create the base transports based on settings provided
*/
Expand All @@ -117,6 +124,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') {
Expand Down
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"@zwave-js/winston-daily-rotate-file": "^4.5.6-1",
"ansi_up": "^6.0.2",
Expand Down

0 comments on commit c93dfa7

Please sign in to comment.