-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
91 lines (70 loc) · 1.6 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
var defaults = require('defaults');
var transports = require('./transports');
var winston = require('winston');
/**
* Expose `create`.
*/
module.exports = create;
/**
* Create a logger.
*
* @param {Object} options
* @return {Logger}
*/
function create (options) {
if (!options) throw new Error('Winston logger must be initialized with options.');
setColors(); // set global winston colors
setLevels(winston); // set global winston levels
var enabled = [];
Object.keys(options).forEach(function (key) {
var opts = options[key];
if (!opts) return; // if the provider is disabled
if (typeof opts !== 'object') opts = {};
if (!transports[key]) {
throw new Error('Failed to find transport ' + key);
}
var Transport = transports[key];
var transport = Transport(opts);
if (transport) enabled.push(transport);
});
var logger = new (winston.Logger)({ transports: enabled });
setLevels(logger);
return logger;
}
/**
* Set Winston levels.
*
* @param {Logger} logger
* @param {Object} levels
*/
function setLevels (logger, levels) {
levels = defaults(levels, {
silly: 0,
verbose: 1,
debug: 2,
info: 3,
warn: 4,
error: 5,
critical: 6,
fatal: 7
});
logger.setLevels(levels);
}
/**
* Set global Winston levels.
*
* @param {Object} colors
*/
function setColors (colors) {
colors = defaults(colors, {
silly : 'magenta',
verbose : 'blue',
debug : 'cyan',
info : 'green',
warn : 'yellow',
error : 'red',
critical : 'red',
fatal : 'red'
});
winston.addColors(colors);
}