winston.js
4.15 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* winston.js: Top-level include defining Winston.
*
* (C) 2010 Charlie Robbins
* MIT LICENCE
*
*/
var winston = exports;
//
// use require method for webpack bundle
//
winston.version = require('../package.json').version
//
// Include transports defined by default by winston
//
winston.transports = require('./winston/transports');
//
// Expose utility methods
//
var common = require('./winston/common');
winston.hash = common.hash;
winston.clone = common.clone;
winston.longestElement = common.longestElement;
winston.exception = require('./winston/exception');
winston.config = require('./winston/config');
winston.addColors = winston.config.addColors;
//
// Expose core Logging-related prototypes.
//
winston.Container = require('./winston/container').Container;
winston.Logger = require('./winston/logger').Logger;
winston.Transport = require('./winston/transports/transport').Transport;
//
// We create and expose a default `Container` to `winston.loggers` so that the
// programmer may manage multiple `winston.Logger` instances without any additional overhead.
//
// ### some-file1.js
//
// var logger = require('winston').loggers.get('something');
//
// ### some-file2.js
//
// var logger = require('winston').loggers.get('something');
//
winston.loggers = new winston.Container();
//
// We create and expose a 'defaultLogger' so that the programmer may do the
// following without the need to create an instance of winston.Logger directly:
//
// var winston = require('winston');
// winston.log('info', 'some message');
// winston.error('some error');
//
var defaultLogger = new winston.Logger({
transports: [new winston.transports.Console()]
});
//
// Pass through the target methods onto `winston`.
//
var methods = [
'log',
'query',
'stream',
'add',
'remove',
'clear',
'profile',
'startTimer',
'extend',
'cli',
'handleExceptions',
'unhandleExceptions',
'configure'
];
winston.padLevels = false;
common.setLevels(winston, null, defaultLogger.levels);
methods.forEach(function (method) {
winston[method] = function () {
return defaultLogger[method].apply(defaultLogger, arguments);
};
});
//
// ### function cli ()
// Configures the default winston logger to have the
// settings for command-line interfaces: no timestamp,
// colors enabled, padded output, and additional levels.
//
winston.cli = function () {
winston.padLevels = true;
common.setLevels(winston, defaultLogger.levels, winston.config.cli.levels);
defaultLogger.setLevels(winston.config.cli.levels);
winston.config.addColors(winston.config.cli.colors);
if (defaultLogger.transports.console) {
defaultLogger.transports.console.colorize = true;
defaultLogger.transports.console.timestamp = false;
}
return winston;
};
//
// ### function setLevels (target)
// #### @target {Object} Target levels to use
// Sets the `target` levels specified on the default winston logger.
//
winston.setLevels = function (target) {
common.setLevels(winston, defaultLogger.levels, target);
defaultLogger.setLevels(target);
};
//
// Define getter / setter for the default logger level
// which need to be exposed by winston.
//
Object.defineProperty(winston, 'level', {
get: function () {
return defaultLogger.level;
},
set: function (val) {
defaultLogger.level = val;
Object.keys(defaultLogger.transports).forEach(function(key) {
defaultLogger.transports[key].level = val;
});
}
});
//
// Define getters / setters for appropriate properties of the
// default logger which need to be exposed by winston.
//
['emitErrs', 'exitOnError', 'padLevels', 'levelLength', 'stripColors'].forEach(function (prop) {
Object.defineProperty(winston, prop, {
get: function () {
return defaultLogger[prop];
},
set: function (val) {
defaultLogger[prop] = val;
}
});
});
//
// @default {Object}
// The default transports and exceptionHandlers for
// the default winston logger.
//
Object.defineProperty(winston, 'default', {
get: function () {
return {
transports: defaultLogger.transports,
exceptionHandlers: defaultLogger.exceptionHandlers
};
}
});