-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger-middleware.js
43 lines (35 loc) · 1.1 KB
/
logger-middleware.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
'use strict'
/**
* Local modules.
*/
var logger = require('./logger')
module.exports = function LoggerMiddleware() {
return function handle(req, res, next) {
var end = res.end
res.end = function logRequest() {
// log this with every request
var requestData =
[ '"' + req.method + ' ' + req.url + '"'
, res.statusCode
, '"' + req.headers['user-agent'] + '"'
].join(' ')
// use different levels for different status codes
if (res.statusCode >= 500) {
logger.error(requestData)
} else if (res.statusCode == 400) {
logger.warn(requestData)
} else {
logger.info(requestData)
}
// useful information when building against this server
var contentType = req.headers['content-type']
if (!contentType || (contentType.split(';')
.indexOf('application/json') === -1)) {
logger.debug('Request with incorrect content-type: "' + contentType +
'" does not contain "application/json".')
}
end.apply(res, arguments)
}
next()
}
}