Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50659 views
1
/*
2
* log.js: Default logging plugin which attachs winston to App instances
3
*
4
* (C) 2011, Nodejitsu Inc.
5
* MIT LICENSE
6
*
7
*/
8
9
var winston = require('winston'),
10
common = require('../common');
11
12
var log = exports;
13
14
//
15
// ### Setup default state for the exceptions plugin
16
//
17
log.name = 'log';
18
log.ignore = ['broadway'];
19
20
//
21
// ### function attach (options)
22
// #### @options {Object} Options for this plugin
23
// Extends `this` (the application) with logging functionality from `winston`.
24
//
25
log.attach = function (options) {
26
options = options || {};
27
28
var app = this,
29
namespaces,
30
logAll;
31
32
if (this.config) {
33
//
34
// Merge options with any pre-existing application config.
35
//
36
options = common.mixin({}, options, this.config.get('log') || {});
37
}
38
39
//
40
// Setup namespaces and then remove them from
41
// `options` so they are not caught by `winston`.
42
//
43
namespaces = options.namespaces || {};
44
delete options.namespaces;
45
46
logAll = options.logAll || false;
47
if (options.logAll) {
48
delete options.logAll;
49
}
50
51
//
52
// Hoist up relelvant logging functions onto the app
53
// if requested.
54
//
55
this.log = new winston.Container(options);
56
this.log.namespaces = namespaces;
57
this.log.get('default').extend(this.log);
58
59
//
60
// Set the default console loglevel to options.level
61
//
62
this.log.get('default').transports.console.level = options.level || 'info';
63
64
Object.defineProperty(this.log, 'logAll', {
65
get: function () {
66
return this._logAll;
67
},
68
set: function (val) {
69
if (val === this._logAll) {
70
//
71
// If the value is identical return
72
//
73
return;
74
}
75
76
if (val) {
77
app.onAny(log.logEvent);
78
app.off(['log'], log.logEvent);
79
app.off(['log', '*'], log.logEvent);
80
app.off(['log', '*', '*'], log.logEvent);
81
}
82
else {
83
app.offAny(log.logEvent);
84
app.on(['log'], log.logEvent);
85
app.on(['log', '*'], log.logEvent);
86
app.on(['log', '*', '*'], log.logEvent);
87
}
88
89
this._logAll = val;
90
}
91
});
92
93
//
94
// Listen to relevant `app` events and
95
// log them appropriately.
96
//
97
this.log.logAll = logAll;
98
99
//
100
// Add any namespaced containers to this App instance.
101
//
102
Object.keys(this.log.namespaces).forEach(function (namespace) {
103
app.log.add(app.log.namespaces[namespace]);
104
});
105
};
106
107
//
108
// ### function logEvent ([level], msg, meta)
109
// #### @msg {string} Message to log
110
// #### @meta {Object} **Optional** Metadata to log
111
// Logs the specified `msg` and `meta` according to
112
// the following conditions:
113
//
114
// #### `log` events
115
// 1. `log` - Logs to the default logger and level.
116
// 2. `log::[level]` - Logs to the default logger.
117
// 3. `log::[level]::[namespace]` - Logs to a namespaced logger.
118
//
119
// ### `[namespaced]` events
120
// If `app.log.logAll` is set, then find a logger at `namespace`,
121
// otherwise the default logger is used.
122
//
123
// 1. `[namespace]::**(level, msg, meta)` - Logs the event as the
124
// message to the logger for the specified namespace and level.
125
// 2. `[namespace]::[level]::**(msg, meta)` - Logs the event and
126
// the message to the logger for the specified namespace and level.
127
//
128
log.logEvent = function (/* level, msg, meta */) {
129
var parts = Array.isArray(this.event) ? this.event : this.event.split(this.delimiter),
130
ev = parts[0],
131
namespace,
132
logger,
133
level,
134
meta,
135
msg;
136
137
if (log.ignore.indexOf(ev) !== -1) {
138
return;
139
}
140
141
//
142
// Determine the `namespace` to log the event to
143
//
144
if (ev === 'log') {
145
namespace = parts[2] || 'default';
146
logger = this.log.get('default');
147
}
148
else if (this.log.logAll) {
149
namespace = this.log.namespaces[ev] ? this.log.namespaces[ev] : 'default';
150
logger = this.log.get(namespace);
151
}
152
else {
153
return;
154
}
155
156
//
157
// Parse arguments now that we have the logger.
158
//
159
Array.prototype.slice.call(arguments).forEach(function (a) {
160
switch (typeof a) {
161
case 'object': {
162
meta = a;
163
break;
164
}
165
case 'string': {
166
if (logger[a]) {
167
level = a;
168
}
169
else {
170
msg = a;
171
}
172
}
173
}
174
});
175
176
if (ev === 'log') {
177
level = parts[1] || level || 'info';
178
}
179
else if (this.log.logAll) {
180
if (logger[parts[1]]) {
181
level = parts[1];
182
parts.splice(1, 1);
183
}
184
}
185
186
if (level in logger.levels === false) {
187
level = 'info';
188
}
189
190
parts = parts.join(this.delimiter);
191
meta = meta || {};
192
meta.event = parts;
193
msg = msg || parts;
194
logger.log(level, msg, meta);
195
this.emit(['broadway', 'logged'], level, msg, meta);
196
};
197
198