Skip to content

Commit

Permalink
fix: broken logs and print stack if present
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando committed Dec 28, 2020
1 parent 35e755e commit 052a043
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function setupSocket (server) {
})

socketManager.on(inboundEvents.zwave, async function (socket, data) {
logger.info(`Zwave api call: ${data.api} ${JSON.stringify(data.args)}`)
logger.log('info', `Zwave api call: ${data.api} %o`, data.args)
if (gw.zwave) {
const result = await gw.zwave.callApi(data.api, ...data.args)
result.api = data.api
Expand Down
34 changes: 19 additions & 15 deletions lib/ZwaveClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ function scanComplete () {
// ---------- CONTROLLER EVENTS -------------------------------

function updateControllerStatus (status) {
logger.info(status)
logger.info(`Controller status: ${status}`)
this.cntStatus = status
this.sendToSocket(socketEvents.controller, status)
}
Expand Down Expand Up @@ -374,8 +374,7 @@ function onNodeDead (zwaveNode, oldStatus) {

function onNodeValueAdded (zwaveNode, args) {
logger.info(
`Node ${zwaveNode.id}: value added: ${getValueID(args)} =>`,
args.newValue
`Node ${zwaveNode.id}: value added: ${getValueID(args)} => ${args.newValue}`
)

// handle node values added 'on fly'
Expand All @@ -395,10 +394,9 @@ function onNodeValueAdded (zwaveNode, args) {
function onNodeValueUpdated (zwaveNode, args) {
updateValue.call(this, zwaveNode, args)
logger.info(
`Node ${zwaveNode.id}: value updated: ${getValueID(args)}`,
args.prevValue,
'=>',
args.newValue
`Node ${zwaveNode.id}: value updated: ${getValueID(args)} ${
args.prevValue
} => ${args.newValue}`
)

this.emit(
Expand All @@ -412,7 +410,7 @@ function onNodeValueUpdated (zwaveNode, args) {

function onNodeValueRemoved (zwaveNode, args) {
removeValue.call(this, zwaveNode, args)
logger.info(`Node ${zwaveNode.id}: value removed: ${args}`)
logger.info(`Node ${zwaveNode.id}: value removed: ${getValueID(args)}`)
this.emit(
'event',
eventEmitter.node,
Expand Down Expand Up @@ -1179,8 +1177,9 @@ ZwaveClient.prototype.removeAssociations = async function (

if (zwaveNode) {
try {
logger.info(
`Assocaitions: Removing associations from Node ${nodeId} Group ${groupId}:`,
logger.log(
'info',
`Assocaitions: Removing associations from Node ${nodeId} Group ${groupId}: %o`,
associations
)
await this.driver.controller.removeAssociations(
Expand Down Expand Up @@ -1282,10 +1281,13 @@ ZwaveClient.prototype.connect = async function () {
if (this.closed) return

// extend options with hidden `options`
const zwaveOptions = Object.assign({
cacheDir: storeDir,
networkKey: this.cfg.networkKey
}, this.cfg.options)
const zwaveOptions = Object.assign(
{
cacheDir: storeDir,
networkKey: this.cfg.networkKey
},
this.cfg.options
)

// transform network key to buffer
if (zwaveOptions.networkKey && zwaveOptions.networkKey.length === 32) {
Expand Down Expand Up @@ -1925,7 +1927,9 @@ ZwaveClient.prototype.writeValue = async function (valueId, value) {
}
} catch (error) {
logger.error(
`Error while writing ${value} on ${getValueID(valueId)}: ${error.message}`,
`Error while writing ${value} on ${getValueID(valueId)}: ${
error.message
}`,
error
)
}
Expand Down
8 changes: 5 additions & 3 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const winston = require('winston')
const { format, transports, addColors } = winston
const { combine, timestamp, label, printf, errors, colorize } = format
const { combine, timestamp, label, printf, colorize, splat } = format
const utils = require('./utils')
const { storeDir } = require('../config/app.js')

Expand All @@ -27,7 +27,7 @@ addColors({
// Custom logging format:
const customFormat = config =>
combine(
errors({ stack: true }),
splat(), // used for formats like: logger.log('Message %s', strinVal)
timestamp({ format: 'YYYY-MM-DD HH:mm:ss.SSS' }),
format(info => {
info.level = info.level.toUpperCase()
Expand All @@ -38,7 +38,9 @@ const customFormat = config =>
printf(info => {
info.timestamp = colorizer.colorize('time', info.timestamp)
info.label = colorizer.colorize('module', info.label || '-')
return `${info.timestamp} ${info.level} ${info.label}: ${info.message}`
return `${info.timestamp} ${info.level} ${info.label}: ${info.message}${
info.stack ? '\n' + info.stack : ''
}`
})
)

Expand Down

0 comments on commit 052a043

Please sign in to comment.