-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (54 loc) · 1.54 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
/*
* index.js
*
* (C) 2012 Tristan Slominski
*/
var log = function log ( options ) {
options = options || {};
var logLevel = options.logLevel || "debug";
var logLevels = {
"debug" : 0,
"info" : 1,
"warn" : 2,
"error" : 3
};
var _do = function _do ( stream, level, message, data ) {
// don't log if wrong level
if ( logLevels[ level ] < logLevels[ logLevel ] ) {
return;
}
if ( typeof( message ) == 'string' ) {
data = { message : message, data : data };
} else if ( message instanceof Error ) {
data = { message : message.toString(), data : message.stack };
} else if ( typeof( message ) == 'object' && Object.keys( message ).length == 0 ) {
data = { message : message.toString(), data : message.stack };
} else {
data = { data : message };
}
data.level = level;
data.timestamp = data.timestamp || ( new Date() ).toISOString();
stream.write( JSON.stringify( data ) + '\n' );
}; // _do
var debug = function debug ( message, data ) {
_do( process.stdout, "debug", message, data );
};
var info = function info ( message, data ) {
_do( process.stdout, "info", message, data );
};
var warn = function warn ( message, data ) {
_do( process.stdout, "warn", message, data );
};
var error = function error ( message, data ) {
_do( process.stderr, "error", message, data );
};
return {
debug : debug,
error : error,
info : info,
level : logLevel,
log : info,
warn : warn
};
}; // log
module.exports = log;