-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcantina-log.js
201 lines (180 loc) · 5.1 KB
/
cantina-log.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
var jog = require('jog')
, StdStore = require('jog-stdstore')
, url = require('url')
, http = require('http')
, format = require('util').format;
module.exports = function (app) {
// Default conf.
app.conf.add({
log: {
trace: true,
req: {
enable: true,
exclude: /(\.js$)|(\.css$)|(\/images.*)|(favicon.ico)|(\.hbs$)/
}
}
});
// Create jog logger. Defaults to StdStore but allows app to override.
app.loggerStore = app.loggerStore || new StdStore();
app.logger = jog(app.loggerStore);
/**
* Logging that optionally includes default meta-data such as stack-trace info.
*
* See http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
*/
function log (type, data) {
var self = this
, args = Array.prototype.slice.call(arguments, 0)
, stack
, call;
if (typeof type !== 'string') {
data = {
data: type
};
type = 'dump';
}
// If single argument is a string, treat this as the log message
else if (args.length === 1 && typeof type === 'string') {
data = {
msg: type
};
type = 'msg';
}
data || (data = {});
// Support printf style string formatting instead of jog type/data style.
if (args.length > 2 || !data.constructor || data.constructor !== Object) {
type = 'msg';
data = {
msg: format.apply(null, args)
};
}
// If data has its own toJSON() method, call it now so that properties
// we add don't get lost, which they will if toJSON() is called later.
if ({}.hasOwnProperty.call(data, 'toJSON')) {
data = data.toJSON();
}
// Optionally, find caller from stack.
if (app.conf.get('log:trace')) {
stack = getStackTrace(4);
stack.shift();
while (call = stack.shift()) {
var name = call.getFunctionName();
if (name && name.match(/^app\.log/)) continue;
break;
}
data.src = {
file: call.getFileName() ? call.getFileName().replace(app.root + '/', '') : '',
line: call.getLineNumber()
};
}
//Include the amino id if present
if (typeof app.amino !== 'undefined') {
data.amino_id = app.amino.id;
}
// Run data serializers.
app.hook('log:serialize').run(data, function (err) {
if (err) return app.logger.error('log serialize', data);
app.logger[self.level](type, data);
});
};
// Create macros for different log levels.
app.log = log.bind({level: 'info'});
app.log.debug = log.bind({level: 'debug'});
app.log.info = log.bind({level: 'info'});
app.log.warn = log.bind({level: 'warn'});
app.log.error = log.bind({level: 'error'});
/**
* Add middleware that logs all requests. (If cantina-web was required).
*/
if (app.middleware && app.conf.get('log:req:enable')) {
app.middleware.add(-700, function logRequest (req, res, next) {
if (!req.href.pathname.match(app.conf.get('log:req:exclude'))) {
var orig = res.end;
res.end = function() {
app.log('request', {
status: res.statusCode,
req: req
});
orig.apply(res, arguments);
};
res.on('close', function () {
app.log.warn('request closed', {
status: res.statusCode,
req: req
});
});
}
next();
});
}
/**
* Store references to original console methods.
*/
app.log.console = {
log: console.log,
info: console.info,
warn: console.warn,
error: console.error,
dir: console.dir
};
/**
* Override console methods to use app.log instead.
*/
app.log.replaceConsole = function () {
console.log = app.log.info;
console.info = app.log.info;
console.warn = app.log.warn;
console.error = app.log.error;
console.dir = app.log.debug;
};
/**
* Resore the original console methods.
*/
app.log.restoreConsole = function () {
console.log = app.log.console.info;
console.info = app.log.console.info;
console.warn = app.log.console.warn;
console.error = app.log.console.error;
console.dir = app.log.console.dir;
};
/**
* Default serialization.
*/
app.hook('log:serialize').add(function (data, next) {
// Serialize req objects.
if (data.req && data.req.href) {
var req = data.req;
data.req = {
method: req.method,
url: {
href: req.href.href,
protocol: req.href.protocol,
host: req.href.host,
pathname: req.href.pathname,
port: req.href.port,
query: req.href.query,
hash: req.href.hash
},
headers: req.headers
};
}
next();
});
};
/**
* Get a stack trace N levels deep.
*/
function getStackTrace (depth) {
var origTrace = Error.prepareStackTrace
, origLimit = Error.stackTraceLimit
, err
, trace;
Error.stackTraceLimit = depth;
Error.prepareStackTrace = function(_, stack){ return stack; };
err = new Error();
Error.captureStackTrace(err, arguments.callee);
trace = err.stack;
Error.stackTraceLimit = origLimit;
Error.prepareStackTrace = origTrace;
return trace;
}