-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlogger.js
84 lines (74 loc) · 2.16 KB
/
logger.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
var dateFormat = require('dateformat'),
colors = require('colors'),
util = require('util');
var events = { };
var options = {
level: 0,
format: "%timestamp% - %event%:%padding% %message%",
timestamp: "HH:MM:ss",
messageFormatting: false
};
function log_event( options ) {
this.event = options.event;
this.level = options.level || 0;
this.color = options.color || 'white';
};
log_event.prototype.config = function( config ) {
for(var key in config) {
this[key] = config[key];
}
return this;
}
log_event.prototype.__defineGetter__ ('padding', function() {
var length = 0,
padding = '';
for(var key in events) {
if(events.hasOwnProperty(key))
length = length < events[key].event.length ? events[key].event.length : length;
}
for(var i=0;i<length-this.event.length;i++) { padding += ' ' }
return padding;
});
log_event.prototype.output = function(input) {
if(options.level <= this.level ) {
var message = '';
if (options.messageFormatting) {
message = " " + util.format.apply(util, input);
} else {
for(var i in input) {
message += " " + ( typeof input[i] === "object" ? JSON.stringify( input[i], null ) : input[i] );
}
}
var format = this.format || options.format;
var output = format
.replace( '%timestamp%', dateFormat( new Date(), this.timestamp || options.timestamp ) ) //timestamp
.replace( '%event%', this.event[ this.color ] ) //log event & color
.replace( '%padding%', this.padding )
.replace( '%message%', message );
console.log( output );
}
}
exports.config = function( config ) {
for(var key in config) {
if(options.hasOwnProperty(key)) {
options[key] = config[key];
}
}
return this;
}
var nFn = function(e) {
return function() { if(arguments.length==0) { return events[e] } else { events[e].output(arguments) } }
}
exports.new = function(newEvents) {
for(event in newEvents) {
events[event] = new log_event( newEvents[event] );
this[event] = nFn(event);
}
return this;
}
exports.new({
debug: { color: 'grey', level: 0, event: 'debug' },
info: { color: 'green', level: 1, event: 'info' },
warn: { color: 'yellow', level: 2, event: 'warning' },
error: { color: 'red', level: 3, event: 'error' }
});