This repository has been archived by the owner on Jul 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (45 loc) · 1.87 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
const defaultSettings = require('./settings.js');
const _ = require('lodash');
const winston = require('winston');
require('winston-logstash-udp');
function Logger(settings) {
if (!(this instanceof Logger)) {
return new Logger(settings);
}
const winstonSettings = _.get(settings, 'winston', {});
this._settings = _.defaultsDeep(_.cloneDeep(winstonSettings), defaultSettings.winston);
this._namedSettings = _.mapKeys(_.omit(settings, 'winston'), (v, k) => {return k.toUpperCase();});
this._container = new winston.Container({
exitOnError: false
});
// Overwrite module, this instance will be included on next require.
module.exports = this;
}
Logger.prototype.get = function (label, level, transportConfig) {
const conf = _.cloneDeep(this._settings.transports);
if (_.isPlainObject(transportConfig)) {
_.merge(conf, transportConfig);
}
// Check for a config specific for the logger we're about to create
const namedSetting = _.get(this._namedSettings, label.toUpperCase());
// Named Config level comes before programmatic set level, as the config can be changed post-deploy
const levelToUse = _.get(namedSetting, 'level', level);
// Filter out falsey values
const transportKeys = _.keys(_.pickBy(this._settings.transports, _.identity));
const transports = _.map(transportKeys, (transport) => {
const transportSettings = conf[transport];
transportSettings.label = label;
transportSettings.level = levelToUse || transportSettings.level;
return new winston.transports[transport](transportSettings);
});
// Create a new logger with the console transport layer by default
return this._container.get(label, {
transports: transports
}).setLevels(this._settings.levels);
};
// Protect against usage without instantiation
Logger.get = () => {
throw new Error('Logger not instantiated');
};
module.exports = Logger;