verbose logging switch

This commit is contained in:
Wolfgang Hottgenroth 2018-04-26 16:38:56 +02:00
parent 0eede1309f
commit f536b7a16f
Signed by: wn
GPG Key ID: B586EAFCDF2F65F4

View File

@ -27,6 +27,7 @@ options
.option('-t, --topic [topic to subscribe]', 'Topic to subscribe, can appear multiple times', collect, [])
.option('-e, --encapsulate', 'store current timestamp, topic and payload in document', false)
.option('-p, --parsePayload', 'parse payload when encapsulating (otherwise always)', false)
.option('-v, --verbose', 'log all inserted messages', false)
.parse(process.argv)
@ -79,12 +80,16 @@ class MqttMongo extends Events.EventEmitter {
if (this.dbReady || true) {
while (! this.queue.isEmpty()) {
let msg : MqttMessage.MqttMessage = this.queue.deq()
console.info(`Something in the queue: ${JSON.stringify(msg)}`)
if (this.options.verbose) {
console.info(`Something in the queue: ${JSON.stringify(msg)}`)
}
let coll = this.dbHandle.collection(this.options.collection)
coll.insertOne(msg.getMessage())
.then(
(r) => {
console.success(`Successfully inserted into database ${JSON.stringify(msg.getMessage())}`)
if (this.options.verbose) {
console.success(`Successfully inserted into database ${JSON.stringify(msg.getMessage())}`)
}
},
(err) => {
console.error(`Error when trying to insert into database ${err}`)
@ -122,7 +127,9 @@ class MqttMongo extends Events.EventEmitter {
this.mqttClient.on('message', (topic : string, messageBuf : Buffer) => {
msgCnt++;
let message = messageBuf.toString('UTF-8')
console.info(`Message received ${msgCnt}, topic ${topic}, payload ${message}`)
if (this.options.verbose) {
console.info(`Message received ${msgCnt}, topic ${topic}, payload ${message}`)
}
if (topic == "MqttMongo/Command" && message == "shutdown") {
this.shutdown()