-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
119 lines (102 loc) · 2.45 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
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
/**
* Logger facade provides a simplified interface to various logging mechanism.
* This module is used by [mag](https://github.com/mahnunchik/mag) logger
* @module Logger
*/
var slice = Array.prototype.slice;
/**
* According [Spec](https://tools.ietf.org/html/rfc5424#page-11)
*/
var severityLevels = {
EMERGENCY: {
aliases: ['emergency', 'emerg', 'panic'],
code: 0
},
ALERT: {
aliases: ['alert'],
code: 1
},
CRITICAL: {
aliases: ['critical', 'crit'],
code: 2
},
ERROR: {
aliases: ['error', 'err'],
code: 3
},
WARNING: {
aliases: ['warning', 'warn'],
code: 4
},
NOTICE: {
aliases: ['notice'],
code: 5
},
INFORMATIONAL: {
aliases: ['informational', 'info'],
code: 6
},
DEBUG: {
aliases: ['debug'],
code: 7
}
};
/**
* @constructs Logger
* @param {WritableStream} stream - distantion to write log object
* @param {String} namespace - tag for each log object
*/
function Logger (stream, namespace) {
if (!(this instanceof Logger)) {
return new Logger(stream, namespace);
}
if (!stream || typeof stream.write !== 'function') {
throw new TypeError('Logger expects a writable stream instance');
}
namespace = namespace || '';
Object.defineProperty(this, '_stream', {value: stream});
Object.defineProperty(this, '_namespace', {value: namespace});
Object.keys(Logger.prototype).forEach(function(key) {
this[key] = this[key].bind(this);
}, this);
}
function defineMethod(method, severity) {
Logger.prototype[method] = function() {
this._stream.write({
arguments: slice.call(arguments),
severity: severity,
timestamp: new Date(),
namespace: this._namespace
});
};
}
Object.keys(severityLevels).forEach(function(severity){
severityLevels[severity].aliases.forEach(function(alias){
defineMethod(alias, severityLevels[severity].code);
});
});
/**
* @deprecated since version 0.1.1
* @param {string} str
*/
Logger.prototype.write = function(str) {
str = str.replace(/\n$/, '');
this._stream.write({
arguments: [str],
severity: severityLevels.INFORMATIONAL.code,
timestamp: new Date(),
namespace: this._namespace
});
};
/**
* @param {...any} data - data to log
*/
Logger.prototype.log = function() {
this._stream.write({
arguments: slice.call(arguments),
severity: severityLevels.INFORMATIONAL.code,
timestamp: new Date(),
namespace: this._namespace
});
};
module.exports = Logger;