Skip to content

Commit 26ef565

Browse files
jumNerivec
andauthoredJan 8, 2025··
feat: Add a settings option to log to console in json format (#25649)
Co-authored-by: Nerivec <62446222+Nerivec@users.noreply.github.com>
·
2.6.32.1.0
1 parent 6f3524b commit 26ef565

File tree

6 files changed

+43
-7
lines changed

6 files changed

+43
-7
lines changed
 

‎lib/types/types.d.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ declare global {
168168
device_options: KeyValue;
169169
advanced: {
170170
log_rotation: boolean;
171+
log_console_json: boolean;
171172
log_symlink_current: boolean;
172173
log_output: ('console' | 'file' | 'syslog')[];
173174
log_directory: string;

‎lib/util/logger.ts‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ class Logger {
5656
this.logger.add(
5757
new winston.transports.Console({
5858
silent: consoleSilenced,
59-
// winston.config.syslog.levels sets 'warning' as 'red'
60-
format: winston.format.combine(
61-
winston.format.colorize({colors: {debug: 'blue', info: 'green', warning: 'yellow', error: 'red'}}),
62-
winston.format.printf((info) => {
63-
return `[${info.timestamp}] ${info.level}: \t${info.message}`;
64-
}),
65-
),
59+
format: settings.get().advanced.log_console_json
60+
? winston.format.json()
61+
: winston.format.combine(
62+
// winston.config.syslog.levels sets 'warning' as 'red'
63+
winston.format.colorize({colors: {debug: 'blue', info: 'green', warning: 'yellow', error: 'red'}}),
64+
winston.format.printf((info) => {
65+
return `[${info.timestamp}] ${info.level}: \t${info.message}`;
66+
}),
67+
),
6668
}),
6769
);
6870

‎lib/util/settings.schema.json‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,13 @@
452452
"description": "Log rotation",
453453
"default": true
454454
},
455+
"log_console_json": {
456+
"type": "boolean",
457+
"title": "Console json log",
458+
"requiresRestart": true,
459+
"description": "Console json log",
460+
"default": false
461+
},
455462
"log_symlink_current": {
456463
"type": "boolean",
457464
"title": "Log symlink current",

‎lib/util/settings.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export const defaults: RecursivePartial<Settings> = {
8686
device_options: {},
8787
advanced: {
8888
log_rotation: true,
89+
log_console_json: false,
8990
log_symlink_current: false,
9091
log_output: ['console', 'file'],
9192
log_directory: path.join(data.getPath(), 'log', '%TIMESTAMP%'),

‎test/extensions/bridge.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ describe('Extension: Bridge', () => {
118118
log_level: 'info',
119119
log_namespaced_levels: {},
120120
log_output: ['console', 'file'],
121+
log_console_json: false,
121122
log_rotation: true,
122123
log_symlink_current: false,
123124
log_syslog: {},

‎test/logger.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,28 @@ describe('Logger', () => {
397397
expect(logSpy).toHaveBeenLastCalledWith('debug', `z2m:test: ${splatChars}`);
398398
expect(consoleWriteSpy.mock.calls[1][0]).toMatch(new RegExp(`^.*\tz2m:test: ${splatChars}`));
399399
});
400+
401+
it('Logs to console in JSON when configured', () => {
402+
settings.set(['advanced', 'log_console_json'], true);
403+
logger.init();
404+
405+
consoleWriteSpy.mockClear();
406+
logger.info(`Test JSON message`, 'z2m');
407+
408+
const outputJSON = JSON.parse(consoleWriteSpy.mock.calls[0][0]);
409+
expect(outputJSON).toStrictEqual({
410+
level: 'info',
411+
message: 'z2m: Test JSON message',
412+
timestamp: expect.any(String),
413+
});
414+
415+
settings.set(['advanced', 'log_console_json'], false);
416+
logger.init();
417+
418+
consoleWriteSpy.mockClear();
419+
logger.info(`Test JSON message`, 'z2m');
420+
421+
const outputStr: string = consoleWriteSpy.mock.calls[0][0];
422+
expect(outputStr.trim().endsWith('\u001b[32minfo\u001b[39m: \tz2m: Test JSON message')).toStrictEqual(true);
423+
});
400424
});

0 commit comments

Comments
 (0)
Please sign in to comment.