-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog-io-logger.js
67 lines (60 loc) · 2.17 KB
/
log-io-logger.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
61
62
63
64
65
66
67
module.exports = function(RED) {
function LogIOLoggerNode(config) {
RED.nodes.createNode(this, config);
const hash = require('object-hash');
const wLogger = require('./lib/winstonLogger');
const isFileOutput = config.logOutput.split(',').includes('file');
const loggerInstances = new Map();
const node = this;
let primaryLogger;
node.config = config;
node.isError = false;
node.log = (...args) => {
if (node.isError) {
node.isError = false;
node.handleLoggerUpdate();
}
const _o = args[2]._o;
const logger = getLoggerInstance(config, _o);
return logger.log(...args);
}
node.close = (...args) => {
primaryLogger?.close(...args);
primaryLogger = null;
loggerInstances.forEach((logger) => logger.close(...args));
loggerInstances.clear();
}
node.handleLoggerUpdate =({ setError = false } = {}) => {
node.isError = setError;
const nodesUsingThisLogger = node.config?._users || [];
nodesUsingThisLogger.forEach((node) => {
RED.nodes.getNode(node)?.handleLoggerUpdate?.();
});
}
function getLoggerInstance(config, options) {
if (!options?._logIO_) {
primaryLogger = primaryLogger || wLogger({ config, RED, node });
return primaryLogger;
}
const dynamicConfiguration = options._logIO_;
const cConfig = {
...JSON.parse(JSON.stringify(config)),
...(isFileOutput && dynamicConfiguration.fileName) && { fileFileName: dynamicConfiguration.fileName },
...(isFileOutput && dynamicConfiguration.dirName) && { fileDirName: dynamicConfiguration.dirName },
}
const loggerKey = hash(cConfig, { algorithm: 'md5' });
if (loggerInstances.has(loggerKey)) {
return loggerInstances.get(loggerKey);
}
const logger = wLogger({ config: cConfig, RED, node });
loggerInstances.set(loggerKey, logger);
return logger;
}
}
RED.nodes.registerType("logIO-logger", LogIOLoggerNode, {
credentials: {
elkUsername: { type: "text" },
elkPassword: { type: "password" },
}
});
}