2019-09-09 10:48:09 -07:00
|
|
|
const tmp = require('tmp');
|
|
|
|
const dir = tmp.dirSync();
|
2019-11-29 15:36:57 -07:00
|
|
|
let settings;
|
2019-09-09 10:48:09 -07:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2019-11-29 15:36:57 -07:00
|
|
|
const data = require('./stub/data');
|
2023-04-15 23:56:31 -07:00
|
|
|
const {rimrafSync} = require('rimraf');
|
2021-01-19 10:14:40 -07:00
|
|
|
const Transport = require('winston-transport');
|
2019-09-09 10:48:09 -07:00
|
|
|
|
|
|
|
describe('Logger', () => {
|
2024-05-09 12:42:28 -07:00
|
|
|
let logger;
|
2024-05-19 06:47:40 -07:00
|
|
|
let consoleWriteSpy;
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
consoleWriteSpy = jest.spyOn(console._stdout, 'write').mockImplementation(() => {});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
consoleWriteSpy.mockRestore();
|
|
|
|
});
|
2024-05-09 12:42:28 -07:00
|
|
|
|
2019-11-29 15:36:57 -07:00
|
|
|
beforeEach(async () => {
|
|
|
|
data.writeDefaultConfiguration();
|
|
|
|
jest.resetModules();
|
|
|
|
settings = require('../lib/util/settings');
|
|
|
|
settings.set(['advanced', 'log_directory'], dir.name + '/%TIMESTAMP%');
|
2021-03-09 11:50:05 -07:00
|
|
|
settings.reRead();
|
2024-05-09 12:42:28 -07:00
|
|
|
logger = require('../lib/util/logger').default;
|
|
|
|
logger.init();
|
2024-05-19 06:47:40 -07:00
|
|
|
consoleWriteSpy.mockClear();
|
2019-11-29 15:36:57 -07:00
|
|
|
});
|
|
|
|
|
2024-06-24 11:58:47 -07:00
|
|
|
afterEach(async () => {});
|
2019-11-29 15:36:57 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Create log directory', () => {
|
|
|
|
const dirs = fs.readdirSync(dir.name);
|
|
|
|
expect(dirs.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should cleanup', () => {
|
2019-11-29 15:36:57 -07:00
|
|
|
for (const d of fs.readdirSync(dir.name)) {
|
2023-04-15 23:56:31 -07:00
|
|
|
rimrafSync(path.join(dir.name, d));
|
2019-11-29 15:36:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < 21; i++) {
|
2019-09-09 10:48:09 -07:00
|
|
|
fs.mkdirSync(path.join(dir.name, `log_${i}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(fs.readdirSync(dir.name).length).toBe(21);
|
|
|
|
logger.cleanup();
|
|
|
|
expect(fs.readdirSync(dir.name).length).toBe(10);
|
2024-06-24 11:58:47 -07:00
|
|
|
});
|
2019-09-09 10:48:09 -07:00
|
|
|
|
|
|
|
it('Should not cleanup when there is no timestamp set', () => {
|
|
|
|
for (let i = 30; i < 40; i++) {
|
|
|
|
fs.mkdirSync(path.join(dir.name, `log_${i}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
settings.set(['advanced', 'log_directory'], dir.name + '/bla');
|
2024-04-02 13:39:33 -07:00
|
|
|
expect(fs.readdirSync(dir.name).length).toBe(21);
|
2019-09-09 10:48:09 -07:00
|
|
|
logger.cleanup();
|
2024-04-02 13:39:33 -07:00
|
|
|
expect(fs.readdirSync(dir.name).length).toBe(21);
|
2024-06-24 11:58:47 -07:00
|
|
|
});
|
2019-09-25 01:55:50 -07:00
|
|
|
|
|
|
|
it('Set and get log level', () => {
|
|
|
|
logger.setLevel('debug');
|
|
|
|
expect(logger.getLevel()).toBe('debug');
|
2024-04-02 13:33:51 -07:00
|
|
|
logger.setLevel('info');
|
|
|
|
expect(logger.getLevel()).toBe('info');
|
|
|
|
logger.setLevel('warning');
|
2024-05-19 06:47:40 -07:00
|
|
|
expect(logger.getLevel()).toBe('warning');
|
2024-04-02 13:33:51 -07:00
|
|
|
logger.setLevel('error');
|
|
|
|
expect(logger.getLevel()).toBe('error');
|
|
|
|
|
2024-05-19 06:47:40 -07:00
|
|
|
// winston level always stays at 'debug', logic handled by custom logger
|
|
|
|
expect(logger.winston.level).toStrictEqual('debug');
|
|
|
|
for (const transport of logger.winston.transports) {
|
|
|
|
expect(transport.level).toStrictEqual(undefined);
|
|
|
|
}
|
2019-09-25 01:55:50 -07:00
|
|
|
});
|
2019-11-29 15:36:57 -07:00
|
|
|
|
2024-04-06 03:44:43 -07:00
|
|
|
it('Add/remove transport', () => {
|
2021-01-19 10:14:40 -07:00
|
|
|
class DummyTransport extends Transport {
|
2024-06-24 11:58:47 -07:00
|
|
|
log(info, callback) {}
|
2021-01-19 10:14:40 -07:00
|
|
|
}
|
|
|
|
|
2024-04-07 11:52:55 -07:00
|
|
|
expect(logger.winston.transports.length).toBe(2);
|
2024-04-06 03:44:43 -07:00
|
|
|
const transport = new DummyTransport();
|
|
|
|
logger.addTransport(transport);
|
2024-04-07 11:52:55 -07:00
|
|
|
expect(logger.winston.transports.length).toBe(3);
|
2024-04-06 03:44:43 -07:00
|
|
|
logger.removeTransport(transport);
|
2024-04-07 11:52:55 -07:00
|
|
|
expect(logger.winston.transports.length).toBe(2);
|
2021-01-19 10:14:40 -07:00
|
|
|
});
|
|
|
|
|
2019-11-29 15:36:57 -07:00
|
|
|
it('Logger should be console and file by default', () => {
|
2024-04-07 11:52:55 -07:00
|
|
|
const pipes = logger.winston._readableState.pipes;
|
2019-11-29 15:36:57 -07:00
|
|
|
expect(pipes.length).toBe(2);
|
|
|
|
expect(pipes[0].constructor.name).toBe('Console');
|
|
|
|
expect(pipes[0].silent).toBe(false);
|
|
|
|
expect(pipes[1].constructor.name).toBe('File');
|
|
|
|
expect(pipes[1].dirname.startsWith(dir.name)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Logger can be file only', () => {
|
|
|
|
settings.set(['advanced', 'log_output'], ['file']);
|
2024-05-09 12:42:28 -07:00
|
|
|
logger.init();
|
2024-04-07 11:52:55 -07:00
|
|
|
const pipes = logger.winston._readableState.pipes;
|
2019-11-29 15:36:57 -07:00
|
|
|
expect(pipes.length).toBe(2);
|
|
|
|
expect(pipes[0].constructor.name).toBe('Console');
|
|
|
|
expect(pipes[0].silent).toBe(true);
|
|
|
|
expect(pipes[1].constructor.name).toBe('File');
|
|
|
|
expect(pipes[1].dirname.startsWith(dir.name)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Logger can be console only', () => {
|
|
|
|
settings.set(['advanced', 'log_output'], ['console']);
|
2024-05-09 12:42:28 -07:00
|
|
|
logger.init();
|
2024-04-07 11:52:55 -07:00
|
|
|
const pipes = logger.winston._readableState.pipes;
|
2019-11-29 15:36:57 -07:00
|
|
|
expect(pipes.constructor.name).toBe('Console');
|
|
|
|
expect(pipes.silent).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Logger can be nothing', () => {
|
|
|
|
settings.set(['advanced', 'log_output'], []);
|
2024-05-09 12:42:28 -07:00
|
|
|
logger.init();
|
2024-04-07 11:52:55 -07:00
|
|
|
const pipes = logger.winston._readableState.pipes;
|
2019-11-29 15:36:57 -07:00
|
|
|
expect(pipes.constructor.name).toBe('Console');
|
|
|
|
expect(pipes.silent).toBe(true);
|
|
|
|
});
|
2020-04-04 10:46:43 -07:00
|
|
|
|
|
|
|
it('Should allow to disable log rotation', () => {
|
|
|
|
settings.set(['advanced', 'log_rotation'], false);
|
2024-05-09 12:42:28 -07:00
|
|
|
logger.init();
|
2024-04-07 11:52:55 -07:00
|
|
|
const pipes = logger.winston._readableState.pipes;
|
2020-04-04 10:46:43 -07:00
|
|
|
expect(pipes[1].constructor.name).toBe('File');
|
|
|
|
expect(pipes[1].maxFiles).toBeNull();
|
|
|
|
expect(pipes[1].tailable).toBeFalsy();
|
|
|
|
expect(pipes[1].maxsize).toBeNull();
|
|
|
|
});
|
2021-04-25 10:21:38 -07:00
|
|
|
|
|
|
|
it('Should allow to symlink logs to current directory', () => {
|
|
|
|
settings.set(['advanced', 'log_symlink_current'], true);
|
2024-05-09 12:42:28 -07:00
|
|
|
logger.init();
|
2024-06-24 11:58:47 -07:00
|
|
|
expect(fs.readdirSync(dir.name).includes('current')).toBeTruthy();
|
2021-04-25 10:21:38 -07:00
|
|
|
|
|
|
|
jest.resetModules();
|
2021-10-02 01:09:38 -07:00
|
|
|
});
|
|
|
|
|
2024-05-19 06:47:40 -07:00
|
|
|
it.each([
|
|
|
|
['debug', {higher: ['info', 'warning', 'error'], lower: []}],
|
|
|
|
['info', {higher: ['warning', 'error'], lower: ['debug']}],
|
|
|
|
['warning', {higher: ['error'], lower: ['debug', 'info']}],
|
|
|
|
['error', {higher: [], lower: ['debug', 'info', 'warning']}],
|
|
|
|
])('Logs relevant levels for %s', (level, otherLevels) => {
|
|
|
|
logger.setLevel(level);
|
2021-10-02 01:09:38 -07:00
|
|
|
|
2024-05-19 06:47:40 -07:00
|
|
|
const logSpy = jest.spyOn(logger.winston, 'log');
|
|
|
|
consoleWriteSpy.mockClear();
|
|
|
|
let i = 1;
|
2021-10-02 01:09:38 -07:00
|
|
|
|
2024-05-19 06:47:40 -07:00
|
|
|
logger[level]('msg');
|
2024-06-04 11:17:35 -07:00
|
|
|
expect(logSpy).toHaveBeenLastCalledWith(level, 'z2m: msg');
|
2024-05-19 06:47:40 -07:00
|
|
|
expect(consoleWriteSpy).toHaveBeenCalledTimes(i++);
|
|
|
|
logger[level]('msg', 'abcd');
|
2024-06-04 11:17:35 -07:00
|
|
|
expect(logSpy).toHaveBeenLastCalledWith(level, 'abcd: msg');
|
2024-05-19 06:47:40 -07:00
|
|
|
expect(consoleWriteSpy).toHaveBeenCalledTimes(i++);
|
2024-04-02 13:33:51 -07:00
|
|
|
|
2024-05-19 06:47:40 -07:00
|
|
|
for (const higherLevel of otherLevels.higher) {
|
|
|
|
logger[higherLevel]('higher msg');
|
2024-06-04 11:17:35 -07:00
|
|
|
expect(logSpy).toHaveBeenLastCalledWith(higherLevel, 'z2m: higher msg');
|
2024-05-19 06:47:40 -07:00
|
|
|
expect(consoleWriteSpy).toHaveBeenCalledTimes(i++);
|
|
|
|
logger[higherLevel]('higher msg', 'abcd');
|
2024-06-04 11:17:35 -07:00
|
|
|
expect(logSpy).toHaveBeenLastCalledWith(higherLevel, 'abcd: higher msg');
|
2024-05-19 06:47:40 -07:00
|
|
|
expect(consoleWriteSpy).toHaveBeenCalledTimes(i++);
|
|
|
|
}
|
2021-10-02 01:09:38 -07:00
|
|
|
|
2024-05-19 06:47:40 -07:00
|
|
|
logSpy.mockClear();
|
|
|
|
consoleWriteSpy.mockClear();
|
2024-04-02 13:33:51 -07:00
|
|
|
|
2024-05-19 06:47:40 -07:00
|
|
|
for (const lowerLevel of otherLevels.lower) {
|
|
|
|
logger[lowerLevel]('lower msg');
|
|
|
|
expect(logSpy).not.toHaveBeenCalled();
|
|
|
|
expect(consoleWriteSpy).not.toHaveBeenCalled();
|
|
|
|
logger[lowerLevel]('lower msg', 'abcd');
|
|
|
|
expect(logSpy).not.toHaveBeenCalled();
|
|
|
|
expect(consoleWriteSpy).not.toHaveBeenCalled();
|
|
|
|
}
|
2021-04-25 10:21:38 -07:00
|
|
|
});
|
2024-05-05 12:52:26 -07:00
|
|
|
|
2024-05-19 06:47:40 -07:00
|
|
|
it('Logs Error object', () => {
|
|
|
|
const logSpy = jest.spyOn(logger.winston, 'log');
|
|
|
|
|
2024-06-24 11:58:47 -07:00
|
|
|
logger.error(new Error('msg')); // test for stack=true
|
2024-06-04 11:17:35 -07:00
|
|
|
expect(logSpy).toHaveBeenLastCalledWith('error', `z2m: ${new Error('msg')}`);
|
2024-05-19 06:47:40 -07:00
|
|
|
expect(consoleWriteSpy).toHaveBeenCalledTimes(1);
|
2024-06-24 11:58:47 -07:00
|
|
|
});
|
2024-05-19 06:47:40 -07:00
|
|
|
|
2024-05-05 12:52:26 -07:00
|
|
|
it.each([
|
|
|
|
[
|
|
|
|
'^zhc:legacy:fz:(tuya|moes)',
|
|
|
|
new RegExp(/^zhc:legacy:fz:(tuya|moes)/),
|
|
|
|
[
|
2024-06-24 11:58:47 -07:00
|
|
|
{ns: 'zhc:legacy:fz:tuya_device12', match: true},
|
|
|
|
{ns: 'zhc:legacy:fz:moes_dimmer', match: true},
|
|
|
|
{ns: 'zhc:legacy:fz:not_moes', match: false},
|
|
|
|
{ns: 'zhc:legacy:fz', match: false},
|
|
|
|
{ns: 'zhc:legacy:fz:', match: false},
|
|
|
|
{ns: '1zhc:legacy:fz:tuya_device12', match: false},
|
|
|
|
],
|
2024-05-05 12:52:26 -07:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'^zhc:legacy:fz:(tuya|moes)|^zh:ember:uart:|^zh:controller',
|
|
|
|
new RegExp(/^zhc:legacy:fz:(tuya|moes)|^zh:ember:uart:|^zh:controller/),
|
|
|
|
[
|
2024-06-24 11:58:47 -07:00
|
|
|
{ns: 'zh:ember:uart:ash', match: true},
|
|
|
|
{ns: 'zh:ember:uart', match: false},
|
|
|
|
{ns: 'zh:controller', match: true},
|
|
|
|
{ns: 'zh:controller:', match: true},
|
|
|
|
{ns: 'azh:controller:', match: false},
|
|
|
|
],
|
2024-05-05 12:52:26 -07:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'',
|
|
|
|
undefined,
|
|
|
|
[
|
2024-06-24 11:58:47 -07:00
|
|
|
{ns: 'zhc:legacy:fz:tuya_device12', match: false},
|
|
|
|
{ns: 'zhc:legacy:fz:moes_dimmer', match: false},
|
|
|
|
{ns: 'zhc:legacy:fz:not_moes', match: false},
|
|
|
|
{ns: 'zhc:legacy:fz', match: false},
|
|
|
|
{ns: 'zhc:legacy:fz:', match: false},
|
|
|
|
{ns: '1zhc:legacy:fz:tuya_device12', match: false},
|
|
|
|
{ns: 'zh:ember:uart:ash', match: false},
|
|
|
|
{ns: 'zh:ember:uart', match: false},
|
|
|
|
{ns: 'zh:controller', match: false},
|
|
|
|
{ns: 'zh:controller:', match: false},
|
|
|
|
{ns: 'azh:controller:', match: false},
|
|
|
|
],
|
2024-05-05 12:52:26 -07:00
|
|
|
],
|
|
|
|
])('Sets namespace ignore for debug level %s', (ignore, expected, tests) => {
|
|
|
|
logger.setLevel('debug');
|
2024-05-19 06:47:40 -07:00
|
|
|
const logSpy = jest.spyOn(logger.winston, 'log');
|
2024-05-05 12:52:26 -07:00
|
|
|
logger.setDebugNamespaceIgnore(ignore);
|
|
|
|
expect(logger.debugNamespaceIgnoreRegex).toStrictEqual(expected);
|
|
|
|
expect(logger.getDebugNamespaceIgnore()).toStrictEqual(ignore);
|
|
|
|
|
|
|
|
for (const test of tests) {
|
|
|
|
logger.debug('Test message', test.ns);
|
|
|
|
|
|
|
|
if (test.match) {
|
2024-05-19 06:47:40 -07:00
|
|
|
expect(logSpy).not.toHaveBeenCalled();
|
2024-05-05 12:52:26 -07:00
|
|
|
} else {
|
2024-06-04 11:17:35 -07:00
|
|
|
expect(logSpy).toHaveBeenLastCalledWith('debug', `${test.ns}: Test message`);
|
2024-05-05 12:52:26 -07:00
|
|
|
}
|
|
|
|
|
2024-05-19 06:47:40 -07:00
|
|
|
logSpy.mockClear();
|
2024-05-05 12:52:26 -07:00
|
|
|
}
|
2024-05-19 06:47:40 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Logs with namespaced levels or default - higher', () => {
|
|
|
|
settings.set(['advanced', 'log_namespaced_levels'], {
|
|
|
|
'z2m:mqtt': 'warning',
|
|
|
|
});
|
|
|
|
logger.init();
|
|
|
|
logger.setLevel('debug');
|
2024-06-24 11:58:47 -07:00
|
|
|
expect(logger.getNamespacedLevels()).toStrictEqual({'z2m:mqtt': 'warning'});
|
2024-05-19 06:47:40 -07:00
|
|
|
expect(logger.getLevel()).toStrictEqual('debug');
|
|
|
|
|
|
|
|
const logSpy = jest.spyOn(logger.winston, 'log');
|
|
|
|
|
|
|
|
consoleWriteSpy.mockClear();
|
|
|
|
logger.info(`MQTT publish: topic 'abcd/efgh', payload '{"my": {"payload": "injson"}}'`, 'z2m:mqtt');
|
|
|
|
expect(logSpy).toHaveBeenCalledTimes(0);
|
|
|
|
expect(consoleWriteSpy).toHaveBeenCalledTimes(0);
|
|
|
|
logger.error(`Not connected to MQTT server!`, 'z2m:mqtt');
|
|
|
|
expect(logSpy).toHaveBeenCalledTimes(1);
|
|
|
|
expect(consoleWriteSpy).toHaveBeenCalledTimes(1);
|
|
|
|
logger.info(`Just another info message`, 'z2m:notmqtt');
|
|
|
|
expect(logSpy).toHaveBeenCalledTimes(2);
|
|
|
|
expect(consoleWriteSpy).toHaveBeenCalledTimes(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Logs with namespaced levels or default - lower', () => {
|
|
|
|
expect(logger.getNamespacedLevels()).toStrictEqual({});
|
2024-06-24 11:58:47 -07:00
|
|
|
logger.setNamespacedLevels({'z2m:mqtt': 'info'});
|
2024-05-19 06:47:40 -07:00
|
|
|
logger.setLevel('warning');
|
2024-06-24 11:58:47 -07:00
|
|
|
expect(logger.getNamespacedLevels()).toStrictEqual({'z2m:mqtt': 'info'});
|
2024-05-19 06:47:40 -07:00
|
|
|
expect(logger.getLevel()).toStrictEqual('warning');
|
|
|
|
|
|
|
|
const logSpy = jest.spyOn(logger.winston, 'log');
|
|
|
|
|
|
|
|
consoleWriteSpy.mockClear();
|
|
|
|
logger.info(`MQTT publish: topic 'abcd/efgh', payload '{"my": {"payload": "injson"}}'`, 'z2m:mqtt');
|
|
|
|
expect(logSpy).toHaveBeenCalledTimes(1);
|
|
|
|
expect(consoleWriteSpy).toHaveBeenCalledTimes(1);
|
|
|
|
logger.error(`Not connected to MQTT server!`, 'z2m:mqtt');
|
|
|
|
expect(logSpy).toHaveBeenCalledTimes(2);
|
|
|
|
expect(consoleWriteSpy).toHaveBeenCalledTimes(2);
|
|
|
|
logger.info(`Just another info message`, 'z2m:notmqtt');
|
|
|
|
expect(logSpy).toHaveBeenCalledTimes(2);
|
|
|
|
expect(consoleWriteSpy).toHaveBeenCalledTimes(2);
|
|
|
|
logger.warning(`Just another warning message`, 'z2m:notmqtt');
|
|
|
|
expect(logSpy).toHaveBeenCalledTimes(3);
|
|
|
|
expect(consoleWriteSpy).toHaveBeenCalledTimes(3);
|
|
|
|
});
|
2024-06-04 11:17:35 -07:00
|
|
|
|
|
|
|
it('Logs with namespaced levels hierarchy', () => {
|
|
|
|
const nsLevels = {'zh:zstack': 'debug', 'zh:zstack:unpi:writer': 'error'};
|
|
|
|
let cachedNSLevels = Object.assign({}, nsLevels);
|
|
|
|
logger.setNamespacedLevels(nsLevels);
|
|
|
|
logger.setLevel('warning');
|
|
|
|
|
|
|
|
consoleWriteSpy.mockClear();
|
|
|
|
logger.debug(`--- parseNext [] debug picked from hierarchy`, 'zh:zstack:unpi:parser');
|
|
|
|
expect(logger.cachedNamespacedLevels).toStrictEqual(Object.assign(cachedNSLevels, {'zh:zstack:unpi:parser': 'debug'}));
|
|
|
|
expect(consoleWriteSpy).toHaveBeenCalledTimes(1);
|
|
|
|
logger.warning(`--> frame [36,15] warning explicitely supressed`, 'zh:zstack:unpi:writer');
|
|
|
|
expect(logger.cachedNamespacedLevels).toStrictEqual(cachedNSLevels);
|
|
|
|
expect(consoleWriteSpy).toHaveBeenCalledTimes(1);
|
|
|
|
logger.warning(`Another supressed warning message in a sub namespace`, 'zh:zstack:unpi:writer:sub:ns');
|
|
|
|
expect(logger.cachedNamespacedLevels).toStrictEqual(Object.assign(cachedNSLevels, {'zh:zstack:unpi:writer:sub:ns': 'error'}));
|
|
|
|
expect(consoleWriteSpy).toHaveBeenCalledTimes(1);
|
|
|
|
logger.error(`but error should go through`, 'zh:zstack:unpi:writer:another:sub:ns');
|
|
|
|
expect(logger.cachedNamespacedLevels).toStrictEqual(Object.assign(cachedNSLevels, {'zh:zstack:unpi:writer:another:sub:ns': 'error'}));
|
|
|
|
expect(consoleWriteSpy).toHaveBeenCalledTimes(2);
|
|
|
|
logger.warning(`new unconfigured namespace warning`, 'z2m:mqtt');
|
|
|
|
expect(logger.cachedNamespacedLevels).toStrictEqual(Object.assign(cachedNSLevels, {'z2m:mqtt': 'warning'}));
|
|
|
|
expect(consoleWriteSpy).toHaveBeenCalledTimes(3);
|
|
|
|
logger.info(`cached unconfigured namespace info should be supressed`, 'z2m:mqtt');
|
|
|
|
expect(logger.cachedNamespacedLevels).toStrictEqual(cachedNSLevels);
|
|
|
|
expect(consoleWriteSpy).toHaveBeenCalledTimes(3);
|
|
|
|
|
|
|
|
logger.setLevel('info');
|
2024-06-24 11:58:47 -07:00
|
|
|
expect(logger.cachedNamespacedLevels).toStrictEqual((cachedNSLevels = Object.assign({}, nsLevels)));
|
2024-06-04 11:17:35 -07:00
|
|
|
logger.info(`unconfigured namespace info should now pass after default level change and cache reset`, 'z2m:mqtt');
|
|
|
|
expect(logger.cachedNamespacedLevels).toStrictEqual(Object.assign(cachedNSLevels, {'z2m:mqtt': 'info'}));
|
|
|
|
expect(consoleWriteSpy).toHaveBeenCalledTimes(4);
|
|
|
|
logger.error(`configured namespace hierachy should still work after the cache reset`, 'zh:zstack:unpi:writer:another:sub:ns');
|
|
|
|
expect(logger.cachedNamespacedLevels).toStrictEqual(Object.assign(cachedNSLevels, {'zh:zstack:unpi:writer:another:sub:ns': 'error'}));
|
|
|
|
expect(consoleWriteSpy).toHaveBeenCalledTimes(5);
|
|
|
|
|
2024-06-24 11:58:47 -07:00
|
|
|
logger.setNamespacedLevels({'zh:zstack': 'warning'});
|
|
|
|
expect(logger.cachedNamespacedLevels).toStrictEqual((cachedNSLevels = {'zh:zstack': 'warning'}));
|
2024-06-04 11:17:35 -07:00
|
|
|
logger.error(`error logged`, 'zh:zstack:unpi:writer');
|
|
|
|
expect(logger.cachedNamespacedLevels).toStrictEqual(Object.assign(cachedNSLevels, {'zh:zstack:unpi:writer': 'warning'}));
|
|
|
|
expect(consoleWriteSpy).toHaveBeenCalledTimes(6);
|
|
|
|
logger.debug(`debug suppressed`, 'zh:zstack:unpi');
|
|
|
|
expect(logger.cachedNamespacedLevels).toStrictEqual(Object.assign(cachedNSLevels, {'zh:zstack:unpi': 'warning'}));
|
|
|
|
expect(consoleWriteSpy).toHaveBeenCalledTimes(6);
|
|
|
|
logger.warning(`warning logged`, 'zh:zstack');
|
|
|
|
expect(logger.cachedNamespacedLevels).toStrictEqual(Object.assign(cachedNSLevels, {'zh:zstack': 'warning'}));
|
|
|
|
expect(consoleWriteSpy).toHaveBeenCalledTimes(7);
|
|
|
|
logger.info(`unconfigured namespace`, 'z2m:mqtt');
|
|
|
|
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}`));
|
|
|
|
});
|
2019-09-09 10:48:09 -07:00
|
|
|
});
|