-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
159 lines (146 loc) · 3.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
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
var util = require('util')
, colors = require('colors')
, fs = require('fs')
;
var globals = {}
, levels =
{ silly: 10
, verbose: 100
, debug: 200
, info: 300
, warn: 400
, error: 500
}
, rlevels = {}
;
colors.setTheme(
{ silly: 'rainbow'
, input: 'grey'
, verbose: 'green'
, prompt: 'grey'
, info: 'cyan'
, data: 'grey'
, help: 'cyan'
, warn: 'yellow'
, debug: 'green'
, error: 'red'
}
)
function Handler () {}
Handler.prototype.msg = function () {
return util.format.apply(this, arguments)
}
function Console () {
this.filter = -1
}
util.inherits(Console, Handler)
Console.prototype.onLog = function (logger, level, arguments) {
var self = this
if (level < this.filter) return
var msg = arguments.map(function (a) {
var m = self.msg(a)
if (m.split('\n').length > 10) return '[object Object]'
return m
}).join(' ')
msg = msg[rlevels[level]] || msg
msg = '['+logger.name.cyan + '] - '+msg
process.stdout.write(msg+'\n')
}
function File (path) {
this.filter = -1
this.path = path
this.writer = fs.createWriteStream(path, {flags: 'a+'})
}
util.inherits(File, Handler)
File.prototype.onLog = function (logger, level, arguments) {
var self = this
if (level < this.filter) return
var msg = this.msg.apply(this, arguments)
msg = '['+logger.name + '] - '+msg
this.writer.write(msg+'\n')
}
function Logger (name, parent) {
var self = this
self.name = name
self.parent = parent
self._l = false
if (parent) {
self.handlers = parent.handlers
} else {
self.handlers = []
}
}
Logger.prototype.logger = function (name) {
return new Logger(name, this)
}
Logger.prototype._log = function () {
var args = Array.prototype.slice.apply(arguments)
, self = this
;
if (!self._l) {
var level = args.shift()
} else {
var level = self._l
}
self.handlers.forEach(function (h) {
h.onLog(self, level, args)
})
}
for (i in levels) {
(function (i) {
Logger.prototype[i] = function () {
this._l = levels[i]
this._log.apply(this, arguments)
this._l = false
}
rlevels[levels[i]] = i
})(i)
}
Logger.prototype.log = Logger.prototype.info
Logger.prototype.dir = Logger.prototype.log
Logger.prototype.time = function (label) {
this.times = {}
times[label] = Date.now()
}
Logger.prototype.timeEnd = function (label) {
var duration = Date.now() - this.times[label]
this.log('%s: %dms', label, duration)
}
Logger.prototype.trace = function(label) {
// TODO probably can to do this better with V8's debug object once that is
// exposed.
var err = new Error
err.name = 'Trace'
err.message = label || ''
Error.captureStackTrace(err, arguments.callee)
this.error(err.stack)
}
Logger.prototype.assert = function (expression) {
if (!expression) {
var arr = Array.prototype.slice.call(arguments, 1)
require('assert').ok(false, util.format.apply(this, arr))
}
}
var handlerMap =
{ console: Console
, file: File
}
Logger.prototype.addHandler = function (handler, options) {
if (typeof handler === 'string') {
if (!handlerMap[handler]) throw new Error('no handler named '+handler)
handler = new handlerMap[handler](options)
}
this.handlers.push(handler)
}
module.exports = new Logger('process')
module.exports.addHandler('console')
module.exports.enableLogref = function () {
var logref = require('logref')
process.logging = function (name) {
var log = module.exports.logger(name)
var logger = function (str, ctx) {
log.info(logref.formatter(str, ctx))
}
return logger
}
}