Skip to content

Commit

Permalink
feat: always show driver/app logs on debug UI even when log settings …
Browse files Browse the repository at this point in the history
…are disabled (#3884)
  • Loading branch information
robertsLando authored Sep 17, 2024
1 parent 1362782 commit 40a557e
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 59 deletions.
21 changes: 3 additions & 18 deletions api/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<StoreFileEntry[]> {
Expand Down
11 changes: 11 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,15 @@ 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.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
Expand Down
12 changes: 12 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 { PassThrough } from 'stream'

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

return combine(...formats)
}

export const logStream = new PassThrough()

/**
* Create the base transports based on settings provided
*/
Expand All @@ -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') {
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",
"ansi_up": "^6.0.2",
"archiver": "^7.0.1",
Expand Down
39 changes: 7 additions & 32 deletions src/views/Debug.vue
Original file line number Diff line number Diff line change
@@ -1,33 +1,6 @@
<template>
<v-container grid-list-md>
<v-row>
<v-col cols="12">
<v-alert
class="mb-0"
v-if="
!zwave.logEnabled ||
!gateway.logEnabled ||
zwave.logToFile
"
dense
text
type="warning"
>
<p class="ma-1" v-if="!zwave.logEnabled">
• ZwaveJS Logs are disabled. Please enable it on
"Settings > Z-Wave" in order to see Application logs
</p>
<p class="ma-1" v-if="!gateway.logEnabled">
• Application Logs are disabled. Please enable it on
"Settings > General" in order to see ZwaveJS logs
</p>
<p class="ma-1" v-if="zwave.logToFile">
• ZwaveJS "Log to file" is enabled. Disable it in order
to see ZwaveJS logs
</p>
</v-alert>
</v-col>

<v-col style="max-width: 220px; margin-top: -2px">
<v-btn-toggle dense multiple>
<v-tooltip
Expand Down Expand Up @@ -89,7 +62,7 @@
import { socketEvents } from '@server/lib/SocketEvents'
import { AnsiUp } from 'ansi_up'
import { mapState, mapActions } from 'pinia'
import { mapActions } from 'pinia'
import useBaseStore from '../stores/base.js'
import { isPopupWindow, openInWindow } from '../lib/utils'
Expand All @@ -104,10 +77,6 @@ export default {
},
watch: {},
computed: {
...mapState(useBaseStore, ['zwave', 'gateway']),
logDisabled() {
return !this.zwave.logEnabled || !this.gateway.logEnabled
},
filteredLogs() {
if (!this.filter) {
return this.debug
Expand Down Expand Up @@ -188,6 +157,12 @@ export default {
if (this.debugActive) {
data = ansiUp.ansi_to_html(data)
data = data.replace(/\n/g, '</br>')
if (!data.endsWith('</br>')) {
data += '</br>'
}
// remove background colors styles
data = data.replace(/background-color:rgb\([0-9, ]+\)/g, '')
// \b[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}Z\b
this.debug.push(data)
Expand Down
18 changes: 9 additions & 9 deletions test/lib/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('logger.js', () => {
describe('customTransports()', () => {
it('should have one transport by default', () => {
const transports = customTransports(sanitizedConfig('-', {}))
return expect(transports.length).to.equal(1)
return expect(transports.length).to.equal(2)
})
})

Expand All @@ -73,7 +73,7 @@ describe('logger.js', () => {
it('should have the default log level', () =>
expect(logger1.level).to.equal('info'))
it('should have one transport only', () =>
expect(logger1.transports.length).to.be.equal(1))
expect(logger1.transports.length).to.be.equal(2))
})

describe('setup() (init)', () => {
Expand All @@ -93,7 +93,7 @@ describe('logger.js', () => {
it('should change the log level', () =>
expect(logger1.level).to.equal('warn'))
it('should have 2 transports', () =>
expect(logger1.transports.length).to.be.equal(2))
expect(logger1.transports.length).to.be.equal(3))
})

describe('setup() (reconfigure)', () => {
Expand All @@ -108,7 +108,7 @@ describe('logger.js', () => {
// Test pre-conditions:
expect(logger1.module).to.equal('mod')
expect(logger1.level).to.equal('warn')
expect(logger1.transports.length).to.be.equal(1)
expect(logger1.transports.length).to.be.equal(2)
// Change logger configuration:
logger1.setup({
logEnabled: false,
Expand All @@ -118,7 +118,7 @@ describe('logger.js', () => {
// Test post-conditions:
expect(logger1.module).to.equal('mod')
expect(logger1.level).to.equal('error')
expect(logger1.transports.length).to.be.equal(2)
expect(logger1.transports.length).to.be.equal(3)
})
})

Expand All @@ -137,10 +137,10 @@ describe('logger.js', () => {
// Test pre-conditions:
expect(logger1.module).to.equal('mod1')
expect(logger1.level).to.equal('warn')
expect(logger1.transports.length).to.be.equal(1)
expect(logger1.transports.length).to.be.equal(2)
expect(logger2.module).to.equal('mod2')
expect(logger2.level).to.equal('warn')
expect(logger2.transports.length).to.be.equal(1)
expect(logger2.transports.length).to.be.equal(2)
// Change logger configuration:
setupAll({
logEnabled: false,
Expand All @@ -150,10 +150,10 @@ describe('logger.js', () => {
// Test post-conditions:
expect(logger1.module).to.equal('mod1')
expect(logger1.level).to.equal('error')
expect(logger1.transports.length).to.be.equal(2)
expect(logger1.transports.length).to.be.equal(3)
expect(logger2.module).to.equal('mod2')
expect(logger2.level).to.equal('error')
expect(logger2.transports.length).to.be.equal(2)
expect(logger2.transports.length).to.be.equal(3)
})
it('should not change the logger config of non-zwave-js-ui loggers', () => {
logger1 = module('mod1').setup({
Expand Down

0 comments on commit 40a557e

Please sign in to comment.