Finalize logger mock structure and add few lambda log calls

This commit is contained in:
Tomer
2024-09-12 07:28:48 +03:00
parent 75e81f83ee
commit bb2c4be36f
3 changed files with 12 additions and 18 deletions
+4 -5
View File
@@ -56,8 +56,7 @@ export default class Zigbee {
acceptJoiningDeviceHandler: this.acceptJoiningDeviceHandler,
};
const herdsmanSettingsLog = JSON.stringify(herdsmanSettings).replaceAll(JSON.stringify(herdsmanSettings.network.networkKey), '"HIDDEN"');
logger.debug(`Using zigbee-herdsman with settings: '${stringify(herdsmanSettingsLog)}'`);
logger.debug(() => `Using zigbee-herdsman with settings: '${stringify(JSON.stringify(herdsmanSettings).replaceAll(JSON.stringify(herdsmanSettings.network.networkKey), '"HIDDEN"'))}'`);
let startResult;
try {
@@ -112,7 +111,7 @@ export default class Zigbee {
this.herdsman.on('message', async (data: ZHEvents.MessagePayload) => {
const device = this.resolveDevice(data.device.ieeeAddr)!;
await device.resolveDefinition();
logger.debug(
logger.debug(() =>
`Received Zigbee message from '${device.name}', type '${data.type}', ` +
`cluster '${data.cluster}', data '${stringify(data.data)}' from endpoint ${data.endpoint.ID}` +
(data.hasOwnProperty('groupID') ? ` with groupID ${data.groupID}` : ``) +
@@ -123,8 +122,8 @@ export default class Zigbee {
});
logger.info(`zigbee-herdsman started (${startResult})`);
logger.info(`Coordinator firmware version: '${stringify(await this.getCoordinatorVersion())}'`);
logger.debug(`Zigbee network parameters: ${stringify(await this.herdsman.getNetworkParameters())}`);
logger.info(() => `Coordinator firmware version: '${stringify(this.getCoordinatorVersion())}'`);
logger.debug(() => `Zigbee network parameters: ${stringify(this.herdsman.getNetworkParameters())}`);
for (const device of this.devicesIterator(utils.deviceNotCoordinator)) {
// If a passlist is used, all other device will be removed from the network.
+1 -1
View File
@@ -332,7 +332,7 @@ describe('Controller', () => {
logger.debug.mockClear();
await MQTT.events.message('dummytopic', 'dummymessage');
expect(spyEventbusEmitMQTTMessage).toHaveBeenCalledWith({topic: 'dummytopic', message: 'dummymessage'});
expect(logger.callTransports).toHaveBeenCalledWith('debug', "Received MQTT message on 'dummytopic' with data 'dummymessage'", LOG_MQTT_NS);
expect(logger.log).toHaveBeenCalledWith('debug', "Received MQTT message on 'dummytopic' with data 'dummymessage'", LOG_MQTT_NS);
});
it('Skip MQTT messages on topic we published to', async () => {
+7 -12
View File
@@ -1,28 +1,23 @@
let level = 'info';
let debugNamespaceIgnore = '';
let namespacedLevels = {};
let transports = [];
let transportsEnabled = false;
const getMessage = (messageOrLambda) => (messageOrLambda instanceof Function ? messageOrLambda() : messageOrLambda);
const mock = {
callTransports: jest.fn().mockImplementation((level, message, namespace) => {
log: jest.fn().mockImplementation((level, message, namespace = 'z2m') => {
if (transportsEnabled) {
for (const transport of transports) {
transport.log({level, message, namespace}, () => {});
mock.callTransports(level, message, namespace);
}
}
}),
log: (level, messageOrLambda, namespace = 'z2m') => {
const message = messageOrLambda instanceof Function ? messageOrLambda() : messageOrLambda;
mock.callTransports(level, message, namespace);
},
init: jest.fn(),
info: jest.fn().mockImplementation((messageOrLambda, namespace = 'z2m') => mock.log('info', messageOrLambda, namespace)),
warning: jest.fn().mockImplementation((messageOrLambda, namespace = 'z2m') => mock.log('warning', messageOrLambda, namespace)),
error: jest.fn().mockImplementation((messageOrLambda, namespace = 'z2m') => mock.log('error', messageOrLambda, namespace)),
debug: jest.fn().mockImplementation((messageOrLambda, namespace = 'z2m') => mock.log('debug', messageOrLambda, namespace)),
info: jest.fn().mockImplementation((messageOrLambda, namespace = 'z2m') => mock.log('info', getMessage(messageOrLambda), namespace)),
warning: jest.fn().mockImplementation((messageOrLambda, namespace = 'z2m') => mock.log('warning', getMessage(messageOrLambda), namespace)),
error: jest.fn().mockImplementation((messageOrLambda, namespace = 'z2m') => mock.log('error', getMessage(messageOrLambda), namespace)),
debug: jest.fn().mockImplementation((messageOrLambda, namespace = 'z2m') => mock.log('debug', getMessage(messageOrLambda), namespace)),
cleanup: jest.fn(),
logOutput: jest.fn(),
add: (transport) => transports.push(transport),