Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: move to winston logger #67

Merged
merged 40 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
0658d56
refactor: Add loging module based on winston
ahochsteger Dec 2, 2020
8f43aa6
style: Fix code style
ahochsteger Dec 2, 2020
cda66b9
fix: Removed unused createLogger
ahochsteger Dec 11, 2020
de18970
refactor(logger): Improved logger setup
ahochsteger Dec 11, 2020
20133dc
fix: Use templates for logs in ZwaveClient.js
ahochsteger Dec 11, 2020
0cde5bc
fix: Fix module name "zwc" -> "zwave"
ahochsteger Dec 11, 2020
f797c11
feat: Add stack traces to log output
ahochsteger Dec 11, 2020
7c755bb
fix: Code formatting
ahochsteger Dec 11, 2020
5a3d085
feat: Enable logger runtime configuration
ahochsteger Dec 11, 2020
6ce23e4
docs: Add as contributor
ahochsteger Dec 11, 2020
9e29707
refactor(app): Use logger module for app
ahochsteger Dec 11, 2020
e8f7b99
refactor(app): Use logger module for jsonStore
ahochsteger Dec 11, 2020
f2fec18
refactor(gateway): Use logger module for Gateway
ahochsteger Dec 11, 2020
fabcdaf
refactor(mqtt): Use logger module for MqttClient
ahochsteger Dec 11, 2020
c9b2f96
refactor(socket): Use logger module for SocketManager
ahochsteger Dec 11, 2020
55109c5
test: Add initial logger tests
ahochsteger Dec 11, 2020
e52d14e
cleanup: Remove obsolete debug.js
ahochsteger Dec 11, 2020
acbcf59
refactor(logger): Fully working logger + tests
ahochsteger Dec 13, 2020
6108516
fix: Code formatting
ahochsteger Dec 13, 2020
6dbedb0
feat: Dynamically setup winston logger in app.js
ahochsteger Dec 13, 2020
b96f373
refactor(logging): Add error stack trace
ahochsteger Dec 14, 2020
aa0ecd2
Merge branch 'master' into winston-logging
ahochsteger Dec 14, 2020
c6d9312
Merge branch 'master' into winston-logging
ahochsteger Dec 14, 2020
0317ad4
refactor: Move logging config to gateway section
ahochsteger Dec 14, 2020
e84a88c
fix: Revert removal of zwave log settings
ahochsteger Dec 15, 2020
8fffb71
fix: Show log settings only if logging is enabled
ahochsteger Dec 15, 2020
322c260
fix: UI labels for gateway logging
ahochsteger Dec 15, 2020
596b703
feat: Add gateway logfile name to UI settings
ahochsteger Dec 15, 2020
8bda41c
Merge branch 'master' into winston-logging
ahochsteger Dec 15, 2020
8474ff3
fix: Log setting UI layout
ahochsteger Dec 15, 2020
cceeb2f
fix: Remove useless logFilename setting
ahochsteger Dec 15, 2020
10c20ce
fix: pretty colors on console and use stderr for error logs
robertsLando Dec 15, 2020
2688850
fix: store log file to store folder
robertsLando Dec 15, 2020
ebcf277
fix: lint issues
robertsLando Dec 15, 2020
17310d6
refactor: Simplify color handling + improve tests
ahochsteger Dec 16, 2020
0d5e90f
test: Cleanup tests
ahochsteger Dec 16, 2020
92e07c7
test: More cleanup
ahochsteger Dec 16, 2020
e1ea9c7
refactor: Remove useless logger name prefix
ahochsteger Dec 16, 2020
6fb35a9
refactor: Split logLevel + logToFile into separat v-flex
ahochsteger Dec 16, 2020
1c88af0
style: Fix code style
ahochsteger Dec 16, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,7 @@ Thanks to this people for help with issues tracking and contributions:
- [**Jay**](https://github.com/jshridha)
- [**Thiago Oliveira**](https://github.com/chilicheech)
- [**Vassilis Aretakis**](https://github.com/billiaz)
- [**Andreas Hochsteger**](https://github.com/ahochsteger)

## :bowtie: Author

Expand Down
42 changes: 28 additions & 14 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require('express')
const reqlib = require('app-root-path').require
const logger = require('morgan')
const morgan = require('morgan')
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
const app = express()
Expand All @@ -11,7 +11,8 @@ const ZWaveClient = reqlib('/lib/ZwaveClient')
const MqttClient = reqlib('/lib/MqttClient')
const Gateway = reqlib('/lib/Gateway')
const store = reqlib('config/store.js')
const debug = reqlib('/lib/debug')('App')
const loggers = reqlib('/lib/logger.js')
const logger = loggers.module('App')
const history = require('connect-history-api-fallback')
const SocketManager = reqlib('/lib/SocketManager')
const { inboundEvents, socketEvents } = reqlib('/lib/SocketManager.js')
Expand All @@ -37,12 +38,22 @@ function start (server) {
startGateway()
}

function setupLogging (settings) {
loggers.setupAll({
enabled: settings.gateway.logEnabled,
level: settings.gateway.logLevel,
logToFile: settings.gateway.logToFile
})
}

function startGateway () {
const settings = jsonStore.get(store.settings)

let mqtt
let zwave

setupLogging(settings)

if (settings.mqtt) {
mqtt = new MqttClient(settings.mqtt)
}
Expand Down Expand Up @@ -83,19 +94,21 @@ function printVersion () {
} catch (error) {
// git not installed
}
debug(`Version: ${require('./package.json').version}${rev ? '.' + rev : ''}`)
logger.info(
`Version: ${require('./package.json').version}${rev ? '.' + rev : ''}`
)
}

// ### EXPRESS SETUP

printVersion()
debug('Application path:' + utils.getPath(true))
logger.info('Application path:' + utils.getPath(true))

// view engine setup
app.set('views', utils.joinPath(false, 'views'))
app.set('view engine', 'ejs')

app.use(logger('dev', { stream: { write: msg => debug(msg.trimEnd()) } }))
app.use(morgan('dev', { stream: { write: msg => logger.info(msg.trimEnd()) } }))
app.use(bodyParser.json({ limit: '50mb' }))
app.use(
bodyParser.urlencoded({
Expand Down Expand Up @@ -129,7 +142,7 @@ function setupSocket (server) {
server.on('listening', function () {
const addr = server.address()
const bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port
debug('Listening on', bind)
logger.info(`Listening on ${bind}`)
})

socketManager.bindServer(server)
Expand All @@ -146,7 +159,7 @@ function setupSocket (server) {
})

socketManager.on(inboundEvents.zwave, async function (socket, data) {
debug('Zwave api call:', data.api, data.args)
logger.info(`Zwave api call: ${data.api} ${data.args}`)
if (gw.zwave) {
const result = await gw.zwave.callApi(data.api, ...data.args)
result.api = data.api
Expand All @@ -155,7 +168,7 @@ function setupSocket (server) {
})

socketManager.on(inboundEvents.hass, async function (socket, data) {
debug('Hass api call:', data.apiName)
logger.info(`Hass api call: ${data.apiName}`)
switch (data.apiName) {
case 'delete':
gw.publishDiscovery(data.device, data.nodeId, true, true)
Expand Down Expand Up @@ -225,7 +238,7 @@ app.get('/api/settings', async function (req, res) {
try {
ports = await SerialPort.list()
} catch (error) {
debug(error)
logger.error(error)
}

data.serial_ports = ports ? ports.map(p => p.path) : []
Expand Down Expand Up @@ -266,7 +279,7 @@ app.post('/api/importConfig', async function (req, res) {

res.json({ success: true, message: 'Configuration imported successfully' })
} catch (error) {
debug(error.message)
logger.error(error.message)
return res.json({ success: false, message: error.message })
}
})
Expand All @@ -281,11 +294,12 @@ app.post('/api/settings', async function (req, res) {
}
restarting = true
await jsonStore.put(store.settings, req.body)
setupLogging(req.body)
await gw.close()
startGateway()
res.json({ success: true, message: 'Configuration updated successfully' })
} catch (error) {
debug(error)
logger.error(error)
res.json({ success: false, message: error.message })
}
})
Expand All @@ -305,7 +319,7 @@ app.use(function (err, req, res) {
res.locals.message = err.message
res.locals.error = req.app.get('env') === 'development' ? err : {}

debug(`${req.method} ${req.url} ${err.status} - Error: ${err.message}`)
logger.error(`${req.method} ${req.url} ${err.status} - Error: ${err.message}`)

// render the error page
res.status(err.status || 500)
Expand All @@ -315,10 +329,10 @@ app.use(function (err, req, res) {
process.removeAllListeners('SIGINT')

process.on('SIGINT', function () {
debug('Closing clients...')
logger.info('Closing clients...')
gw.close()
.catch(err => {
debug('Error while closing clients', err)
logger.error('Error while closing clients', err)
})
.finally(() => {
process.exit()
Expand Down
52 changes: 23 additions & 29 deletions lib/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const { AlarmSensorType } = require('zwave-js')
const { CommandClasses } = require('@zwave-js/core')
const { socketEvents } = reqlib('/lib/SocketManager.js')
const Constants = reqlib('/lib/Constants.js')
const debug = reqlib('/lib/debug')('Gateway')
const logger = reqlib('/lib/logger.js').module('Gateway')
const inherits = require('util').inherits
const hassCfg = reqlib('/hass/configurations.js')
const hassDevices = reqlib('/hass/devices.js')
Expand All @@ -26,8 +26,6 @@ const NODE_PREFIX = 'nodeID_'
const CUSTOM_DEVICES = reqlib('config/app.js').storeDir + '/customDevices'
let allDevices = hassDevices // will contain customDevices + hassDevices

debug.color = 2

// watcher initiates a watch on a file. if this fails (e.g., because the file
// doesn't exist), instead watch the directory. If the directory watch
// triggers, cancel it and try to watch the file again. Meanwhile spam `fn()`
Expand Down Expand Up @@ -82,7 +80,7 @@ const loadCustomDevices = () => {
return
}
} catch (error) {
debug('failed to load ' + loaded + ':', error)
logger.error(`failed to load ${loaded}:`, error)
return
}

Expand All @@ -94,15 +92,13 @@ const loadCustomDevices = () => {
return
}

debug('loading custom devices from', loaded)
logger.info(`loading custom devices from ${loaded}`)

lastCustomDevicesLoad = sha

allDevices = Object.assign({}, hassDevices, devices)
debug(
'Loaded',
Object.keys(devices).length,
'custom Hass devices configurations'
logger.info(
`Loaded ${Object.keys(devices).length} custom Hass devices configurations`
)
}

Expand Down Expand Up @@ -163,7 +159,7 @@ Gateway.prototype.start = async function () {
// this is async but doesn't need to be awaited
this.zwave.connect()
} else {
debug('Zwave settings are not valid')
logger.error('Zwave settings are not valid')
}
}

Expand Down Expand Up @@ -395,7 +391,7 @@ function onNodeStatus (node) {
// }
// } catch (error) {
// const op = values[i].verifyChanges ? 'verify changes' : 'enable poll'
// debug('Error while call', op, error.message)
// logger.error(`Error while call ${op} ${error.message}`)
// }
// }
// }
Expand Down Expand Up @@ -425,7 +421,7 @@ function onBrokerStatus (online) {
}

function onHassStatus (online) {
debug('Home Assistant is ' + (online ? 'ONLINE' : 'OFFLINE'))
logger.info(`Home Assistant is ${online ? 'ONLINE' : 'OFFLINE'}`)

if (online) {
this.rediscoverAll()
Expand All @@ -438,7 +434,7 @@ async function onApiRequest (topic, apiName, payload) {
const result = await this.zwave.callApi(apiName, ...args)
this.mqtt.publish(topic, result)
} else {
debug('Requested Zwave api', apiName, "doesn't exist")
logger.error(`Requested Zwave api ${apiName} doesn't exist`)
}
}

Expand Down Expand Up @@ -492,7 +488,7 @@ function evalFunction (code, valueId, value) {
const parseFunc = new Function('value', code)
result = parseFunc(value)
} catch (error) {
debug('Error eval function of value ', valueId.id, error.message)
logger.error(`Error eval function of value ${valueId.id} ${error.message}`)
}

return result
Expand Down Expand Up @@ -718,7 +714,9 @@ Gateway.prototype.parsePayload = function (payload, valueId, valueConf) {
}
}
} catch (error) {
debug('Error while parsing payload', payload, 'for valueID', valueId)
logger.error(
`Error while parsing payload ${payload} for valueID ${valueId}`
)
}

return payload
Expand All @@ -730,7 +728,7 @@ Gateway.prototype.parsePayload = function (payload, valueId, valueConf) {
Gateway.prototype.close = async function () {
this.closed = true

debug('Closing Gateway...')
logger.info('Closing Gateway...')

if (this.mqtt) {
await this.mqtt.close()
Expand Down Expand Up @@ -954,7 +952,7 @@ Gateway.prototype.publishDiscovery = function (
this.zwave.updateDevice(hassDevice, nodeId, deleteDevice)
}
} catch (error) {
debug('Error while publishing node %d: %s', nodeId, error.message)
logger.error(`Error while publishing node ${nodeId}: ${error.message}`)
}
}

Expand Down Expand Up @@ -1146,10 +1144,8 @@ Gateway.prototype.discoverDevice = function (node, hassDevice) {
}
}
} catch (error) {
debug(
'Error while discovering device %s of node %d: %s',
hassID,
node.id,
logger.error(
`Error while discovering device ${hassID} of node ${node.id}: ${error.message}`,
error
)
}
Expand Down Expand Up @@ -1204,7 +1200,7 @@ Gateway.prototype.discoverClimates = function (node) {
const temperatureId = temperatures[0]

if (!temperatureId) {
debug(
logger.warn(
'Unable to discover climate device, there is no valid temperature valueId'
)
return
Expand Down Expand Up @@ -1296,11 +1292,11 @@ Gateway.prototype.discoverClimates = function (node) {
// discovered later when we call `discoverDevice`
nodeDevices.push(config)

debug('New climate device discovered: ' + JSON.stringify(config))
logger.info(`New climate device discovered: ${JSON.stringify(config)}`)

allDevices[node.deviceId] = nodeDevices
} catch (error) {
debug('Unable to discover climate device: ' + error.message)
logger.error(`Unable to discover climate device: ${error.message}`, error)
}
}

Expand Down Expand Up @@ -1633,11 +1629,9 @@ Gateway.prototype.discoverValue = function (node, vId) {

this.publishDiscovery(cfg, node.id)
} catch (error) {
debug(
'Error while discovering value %s of node %d: %s',
valueId.id,
node.id,
error.message
logger.error(
`Error while discovering value ${valueId.id} of node ${node.id}: ${error.message}`,
error
)
}
}
Expand Down
Loading