From d7031d259b178de36f06747f4f702941b113c65a Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Sat, 14 Apr 2018 16:17:25 +0200 Subject: [PATCH] Use winston logger. --- index.js | 74 ++++++++++++++++++++++++--------------------- npm-shrinkwrap.json | 53 +++++++++++++++++++++++++++++--- package.json | 1 + 3 files changed, 88 insertions(+), 40 deletions(-) diff --git a/index.js b/index.js index af40ae1e1..17873d278 100644 --- a/index.js +++ b/index.js @@ -7,8 +7,23 @@ const parsers = require('./parsers'); const deviceMapping = require('./devices'); const config = require('yaml-config'); const configFile = `${__dirname}/data/configuration.yaml` +const winston = require('winston'); let settings = config.readConfig(configFile, 'user'); +const logger = new (winston.Logger)({ + transports: [ + new (winston.transports.Console)({ + timestamp: () => new Date().toLocaleString(), + formatter: function(options) { + return options.timestamp() + ' ' + + winston.config.colorize(options.level, options.level.toUpperCase()) + ' ' + + (options.message ? options.message : '') + + (options.meta && Object.keys(options.meta).length ? '\n\t'+ JSON.stringify(options.meta) : '' ); + } + }) + ] +}); + // Create empty device array if not set yet. if (!settings.devices) { settings.devices = {}; @@ -16,7 +31,7 @@ if (!settings.devices) { } // Setup client -console.log(`Connecting to MQTT server at ${settings.mqtt.server}`) +logger.info(`Connecting to MQTT server at ${settings.mqtt.server}`) const options = {}; if (settings.mqtt.user && settings.mqtt.password) { @@ -42,32 +57,30 @@ process.on('SIGINT', handleQuit); // Check every interval if connected to MQTT server. setInterval(() => { if (client.reconnecting) { - console.error(` - ERROR: Not connected to MQTT server!` - ); + logger.error('Not connected to MQTT server!'); } }, 10 * 1000); // seconds * 1000. // Start server -console.log(`Starting zigbee-shepherd with device ${settings.serial.port}`) +logger.info(`Starting zigbee-shepherd with device ${settings.serial.port}`) shepherd.start((err) => { if (err) { - console.error('Error while starting zigbee-shepherd'); - console.error(err); + logger.error('Error while starting zigbee-shepherd'); + logger.error(err); } else { - console.error('zigbee-shepherd started'); + logger.info('zigbee-shepherd started'); } }); function handleReady() { - console.log('zigbee-shepherd ready'); + logger.info('zigbee-shepherd ready'); const devices = shepherd.list().filter((device) => { return device.manufId === 4151 && device.type === 'EndDevice' }); - console.log(`Currently ${devices.length} devices are joined:`); - devices.forEach((device) => console.log(getDeviceLogMessage(device))); + logger.info(`Currently ${devices.length} devices are joined:`); + devices.forEach((device) => logger.info(getDeviceLogMessage(device))); // Set all Xiaomi devices to be online, so shepherd won't try // to query info from devices (which would fail because they go tosleep). @@ -80,16 +93,14 @@ function handleReady() { // Allow or disallow new devices to join the network. if (settings.allowJoin) { - console.log(` - WARNING: allowJoin set to true in configuration.yaml. - Allowing new devices to join. - Remove this parameter once you joined all devices. - `); + logger.warn('allowJoin set to true in configuration.yaml.') + logger.warn('Allowing new devices to join.'); + logger.warn('Remove this parameter once you joined all devices.'); } shepherd.permitJoin(settings.allowJoin ? 255 : 0, (err) => { if (err) { - console.log(err); + logger.info(err); } }); } @@ -107,7 +118,7 @@ function handleMessage(msg) { // New device! if (!settings.devices[device.ieeeAddr]) { - console.log(`New device with address ${device.ieeeAddr} connected!`); + logger.info(`New device with address ${device.ieeeAddr} connected!`); settings.devices[device.ieeeAddr] = { friendly_name: device.ieeeAddr @@ -126,10 +137,8 @@ function handleMessage(msg) { const mappedModel = deviceMapping[modelID]; if (!mappedModel) { - console.log(` - WARNING: Device with modelID '${modelID}' is not supported. - Please create an issue on https://github.com/Koenkk/xiaomi-zb2mqtt/issues - to add support for your device`); + logger.error(`Device with modelID '${modelID}' is not supported.`); + logger.error('Please create an issue on https://github.com/Koenkk/xiaomi-zb2mqtt/issues to add support for your device'); } // Find a parser for this modelID and cid. @@ -137,10 +146,8 @@ function handleMessage(msg) { const _parsers = parsers.filter((p) => p.devices.includes(mappedModel.model) && p.cid === cid); if (!_parsers.length) { - console.log(` - WARNING: No parser available for '${mappedModel.model}' with cid '${cid}' - Please create an issue on https://github.com/Koenkk/xiaomi-zb2mqtt/issues - with this message.`); + logger.error(`No parser available for '${mappedModel.model}' with cid '${cid}'`); + logger.error('Please create an issue on https://github.com/Koenkk/xiaomi-zb2mqtt/issues with this message.'); return; } @@ -155,7 +162,7 @@ function handleMessage(msg) { const topic = `${settings.mqtt.base_topic}/${friendlyName}/${parser.topic}`; const publish = (payload) => mqttPublish(topic, payload.toString()); const payload = parser.parse(msg, publish); - + if (payload) { publish(payload); } @@ -165,9 +172,9 @@ function handleMessage(msg) { function handleQuit() { shepherd.stop((err) => { if (err) { - console.error('Error while stopping zigbee-shepherd'); + logger.error('Error while stopping zigbee-shepherd'); } else { - console.error('zigbee-shepherd stopped') + logger.error('zigbee-shepherd stopped') } mqttPublish(`${settings.mqtt.base_topic}/bridge/state`, 'offline'); @@ -177,15 +184,12 @@ function handleQuit() { function mqttPublish(topic, payload) { if (client.reconnecting) { - console.log(` - ERROR: Not connected to MQTT server! - Cannot send message: topic: '${topic}', payload: '${payload}' - `); - + logger.error(`Not connected to MQTT server!`); + logger.error(`Cannot send message: topic: '${topic}', payload: '${payload}`); return; } - console.log(`MQTT publish, topic: '${topic}', payload: '${payload}'`); + logger.info(`MQTT publish, topic: '${topic}', payload: '${payload}'`); client.publish(topic, payload); } diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 183d5accc..1c967cebf 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -19,9 +19,9 @@ } }, "async": { - "version": "0.2.10", - "resolved": "https://registry.npmjs.org/async/-/async-0.2.10.tgz", - "integrity": "sha1-trvgsGdLnXGXCMo43owjfLUmw9E=" + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/async/-/async-1.0.0.tgz", + "integrity": "sha1-+PwEyjoTeErenhZBr5hXjPvWR6k=" }, "async-limiter": { "version": "1.0.0", @@ -102,6 +102,11 @@ "unpi": "1.1.0" } }, + "colors": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/colors/-/colors-1.0.3.tgz", + "integrity": "sha1-BDP0TYCWgP3rYO0mDxsMJi6CpAs=" + }, "commander": { "version": "2.15.1", "resolved": "https://registry.npmjs.org/commander/-/commander-2.15.1.tgz", @@ -142,6 +147,11 @@ "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" }, + "cycle": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/cycle/-/cycle-1.0.3.tgz", + "integrity": "sha1-IegLK+hYD5i0aPN5QwZisEbDStI=" + }, "debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -257,6 +267,11 @@ "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.1.tgz", "integrity": "sha1-p1Xqe8Gt/MWjHOfnYtuq3F5jZEQ=" }, + "eyes": { + "version": "0.1.8", + "resolved": "https://registry.npmjs.org/eyes/-/eyes-0.1.8.tgz", + "integrity": "sha1-Ys8SAjTGg3hdkCNIqADvPgzCC8A=" + }, "foreach": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/foreach/-/foreach-2.0.5.tgz", @@ -404,6 +419,11 @@ "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" }, + "isstream": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", + "integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo=" + }, "js-yaml": { "version": "2.1.3", "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-2.1.3.tgz", @@ -543,6 +563,11 @@ "underscore": "1.4.4" }, "dependencies": { + "async": { + "version": "0.2.10", + "resolved": "https://registry.npmjs.org/async/-/async-0.2.10.tgz", + "integrity": "sha1-trvgsGdLnXGXCMo43owjfLUmw9E=" + }, "underscore": { "version": "1.4.4", "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.4.4.tgz", @@ -1612,6 +1637,11 @@ "through2": "2.0.3" } }, + "stack-trace": { + "version": "0.0.10", + "resolved": "https://registry.npmjs.org/stack-trace/-/stack-trace-0.0.10.tgz", + "integrity": "sha1-VHxws0fo0ytOEI6hoqFZ5f3eGcA=" + }, "stream-browserify": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/stream-browserify/-/stream-browserify-2.0.1.tgz", @@ -1722,6 +1752,19 @@ "xtend": "4.0.1" } }, + "winston": { + "version": "2.4.1", + "resolved": "https://registry.npmjs.org/winston/-/winston-2.4.1.tgz", + "integrity": "sha512-k/+Dkzd39ZdyJHYkuaYmf4ff+7j+sCIy73UCOWHYA67/WXU+FF/Y6PF28j+Vy7qNRPHWO+dR+/+zkoQWPimPqg==", + "requires": { + "async": "1.0.0", + "colors": "1.0.3", + "cycle": "1.0.3", + "eyes": "0.1.8", + "isstream": "0.1.2", + "stack-trace": "0.0.10" + } + }, "wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", @@ -1761,7 +1804,7 @@ } }, "zcl-packet": { - "version": "git+https://github.com/kirovilya/zcl-packet.git#a2b39c1b5239317bf8cd58ebf665319a5c039456", + "version": "git+https://github.com/koenkk/zcl-packet.git#a2b39c1b5239317bf8cd58ebf665319a5c039456", "requires": { "concentrate": "0.2.3", "dissolve-chunks": "1.3.0", @@ -1791,7 +1834,7 @@ "proving": "0.1.0", "q": "1.5.1", "zcl-id": "0.3.2", - "zcl-packet": "git+https://github.com/kirovilya/zcl-packet.git#a2b39c1b5239317bf8cd58ebf665319a5c039456", + "zcl-packet": "git+https://github.com/koenkk/zcl-packet.git#a2b39c1b5239317bf8cd58ebf665319a5c039456", "ziee": "0.3.0", "zive": "0.2.2", "zstack-constants": "0.2.0" diff --git a/package.json b/package.json index 68d3bf407..fafe95cd4 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "homepage": "https://github.com/Koenkk/xiaomi-zb2mqtt/wiki", "dependencies": { "mqtt": "*", + "winston": "*", "zigbee-shepherd": "*", "zcl-packet": "git+https://github.com/koenkk/zcl-packet.git", "yaml-config": "*"