-
Notifications
You must be signed in to change notification settings - Fork 0
/
globals.js
60 lines (50 loc) · 1.42 KB
/
globals.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
function getLogger() {
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};${level};${message};${
meta ? JSON.stringify(meta) : ""
}`;
});
const logger = winston.createLogger({
level: "info",
format: combine(timestamp(), splat(), myFormat),
transports: [new winston.transports.Console()],
});
return logger;
}
function getSettings() {
const commandLineArgs = require("command-line-args");
const argsDefinitions = [
{ name: "mqtt_host", alias: "h", type: String, defaultValue: "localhost" },
{ name: "mqtt_port", alias: "p", type: Number, defaultValue: 1883 },
{
name: "mqtt_topic_to_log",
alias: "t",
type: String,
defaultValue: "#",
},
{
name: "logging_file",
alias: "f",
type: String,
defaultValue: "./mqtt.log",
}
];
const args = commandLineArgs(argsDefinitions);
//override with environment variables if available
argsDefinitions.forEach((def) => {
if (process.env[def.name]) {
args[def.name] = process.env[def.name];
}
});
if(args["mqtt_topic_to_log"]) {
args["mqtt_topic_to_log"] = args["mqtt_topic_to_log"].split(",");
}
return args;
}
module.exports = {
logger: getLogger(),
settings: getSettings(),
};