Skip to content

Commit

Permalink
Fix #22935
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerivec committed Jun 4, 2024
1 parent 8cce8a3 commit 827e18e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
9 changes: 3 additions & 6 deletions lib/util/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Logger {
format: winston.format.combine(
winston.format.colorize({colors: {debug: 'blue', info: 'green', warning: 'yellow', error: 'red'}}),
winston.format.printf(/* istanbul ignore next */(info) => {
return `[${info.timestamp}] ${info.level}: \t${info.namespace}: ${info.message}`;
return `[${info.timestamp}] ${info.level}: \t${info.message}`;
}),
),
}));
Expand All @@ -84,7 +84,7 @@ class Logger {
const transportFileOptions: winston.transports.FileTransportOptions = {
filename: path.join(this.directory, logFilename),
format: winston.format.printf(/* istanbul ignore next */(info) => {
return `[${info.timestamp}] ${info.level}: \t${info.namespace}: ${info.message}`;
return `[${info.timestamp}] ${info.level}: \t${info.message}`;
}),
};

Expand All @@ -106,9 +106,6 @@ class Logger {

const options: KeyValue = {
app_name: 'Zigbee2MQTT',
format: winston.format.printf(/* istanbul ignore next */(info) => {
return `${info.namespace}: ${info.message}`;
}),
...settings.get().advanced.log_syslog,
};

Expand Down Expand Up @@ -188,7 +185,7 @@ class Logger {
const nsLevel = this.cacheNamespacedLevel(namespace);

if (settings.LOG_LEVELS.indexOf(level) <= settings.LOG_LEVELS.indexOf(nsLevel)) {
this.logger.log(level, message, {namespace});
this.logger.log(level, `${namespace}: ${message}`);
}
}

Expand Down
28 changes: 22 additions & 6 deletions test/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,18 @@ describe('Logger', () => {
let i = 1;

logger[level]('msg');
expect(logSpy).toHaveBeenLastCalledWith(level, 'msg', {namespace: 'z2m'});
expect(logSpy).toHaveBeenLastCalledWith(level, 'z2m: msg');
expect(consoleWriteSpy).toHaveBeenCalledTimes(i++);
logger[level]('msg', 'abcd');
expect(logSpy).toHaveBeenLastCalledWith(level, 'msg', {namespace: 'abcd'});
expect(logSpy).toHaveBeenLastCalledWith(level, 'abcd: msg');
expect(consoleWriteSpy).toHaveBeenCalledTimes(i++);

for (const higherLevel of otherLevels.higher) {
logger[higherLevel]('higher msg');
expect(logSpy).toHaveBeenLastCalledWith(higherLevel, 'higher msg', {namespace: 'z2m'});
expect(logSpy).toHaveBeenLastCalledWith(higherLevel, 'z2m: higher msg');
expect(consoleWriteSpy).toHaveBeenCalledTimes(i++);
logger[higherLevel]('higher msg', 'abcd');
expect(logSpy).toHaveBeenLastCalledWith(higherLevel, 'higher msg', {namespace: 'abcd'});
expect(logSpy).toHaveBeenLastCalledWith(higherLevel, 'abcd: higher msg');
expect(consoleWriteSpy).toHaveBeenCalledTimes(i++);
}

Expand All @@ -193,7 +193,7 @@ describe('Logger', () => {
const logSpy = jest.spyOn(logger.winston, 'log');

logger.error(new Error('msg'));// test for stack=true
expect(logSpy).toHaveBeenLastCalledWith('error', new Error('msg'), {namespace: 'z2m'});
expect(logSpy).toHaveBeenLastCalledWith('error', `z2m: ${new Error('msg')}`);
expect(consoleWriteSpy).toHaveBeenCalledTimes(1);
})

Expand Down Expand Up @@ -251,7 +251,7 @@ describe('Logger', () => {
if (test.match) {
expect(logSpy).not.toHaveBeenCalled();
} else {
expect(logSpy).toHaveBeenLastCalledWith('debug', 'Test message', {namespace: test.ns});
expect(logSpy).toHaveBeenLastCalledWith('debug', `${test.ns}: Test message`);
}

logSpy.mockClear();
Expand Down Expand Up @@ -355,4 +355,20 @@ describe('Logger', () => {
expect(logger.cachedNamespacedLevels).toStrictEqual(Object.assign(cachedNSLevels, {'z2m:mqtt': 'info'}));
expect(consoleWriteSpy).toHaveBeenCalledTimes(8);
});

it('Ignores SPLAT chars', () => {
logger.setLevel('debug');

const logSpy = jest.spyOn(logger.winston, 'log');
consoleWriteSpy.mockClear();

let net_map = '%d';
logger.debug(net_map, 'z2m:mqtt');
expect(logSpy).toHaveBeenLastCalledWith('debug', `z2m:mqtt: ${net_map}`);
expect(consoleWriteSpy.mock.calls[0][0]).toMatch(new RegExp(`^.*\tz2m:mqtt: ${net_map}`));
net_map = 'anything %s goes here';
logger.debug(net_map, 'z2m:test');
expect(logSpy).toHaveBeenLastCalledWith('debug', `z2m:test: ${net_map}`);
expect(consoleWriteSpy.mock.calls[1][0]).toMatch(new RegExp(`^.*\tz2m:test: ${net_map}`));
});
});

0 comments on commit 827e18e

Please sign in to comment.