-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (27 loc) · 1.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"use strict";
const { logger, settings } = require("./globals");
const mqtt = require("./mqtt")(settings.mqtt_host, settings.mqtt_port);
function getFileWriter(filename){
const winston = require("winston");
const { splat, combine, timestamp, printf } = winston.format;
// meta param is ensured by splat()
const myFormat = printf(({ timestamp, level, message, meta }) => {
return `${timestamp}\t${message}`;
});
const fileWriter = winston.createLogger({
level: 'debug',
format: combine(timestamp(), splat(), myFormat),
transports: [
new winston.transports.File({ filename: filename, maxsize: 10*1024*1024})
],
});
return fileWriter;
}
const fileWriter = getFileWriter(settings.logging_file)
logger.info("Starting the MQTT logger.");
logger.info("Settings: " + JSON.stringify(settings));
function logListener(topic, payload, packet){
fileWriter.debug(`${topic}${packet.retain ? ' [R]': ''}\t${payload}\t`);
}
mqtt.registerListener(logListener);
mqtt.subscribe(settings.mqtt_topic_to_log);