express-bunyan-logger
Advanced tools
Comparing version 0.1.0 to 0.1.1
72
index.js
@@ -5,5 +5,5 @@ var bunyan = require('bunyan'), | ||
module.exports = function(opts) { | ||
module.exports = function (opts) { | ||
var logger = module.exports.errorLogger(opts); | ||
return function(req, res, next) { | ||
return function (req, res, next) { | ||
logger(null, req, res, next); | ||
@@ -14,5 +14,5 @@ }; | ||
module.exports.errorLogger = function(opts) { | ||
var logger, opts = opts || {}, format, | ||
immediate = false, | ||
module.exports.errorLogger = function (opts) { | ||
var logger, opts = opts || {}, format, | ||
immediate = false, | ||
parseUA = true, | ||
@@ -22,9 +22,9 @@ levelFn = defaultLevelFn; | ||
// default format | ||
format = opts.format || ":remote-address - :method :url HTTP/:http-version :status-code :res-headers[content-length] :referer :user-agent[family] :user-agent[major].:user-agent[minor] :user-agent[os] :response-time ms"; | ||
format = opts.format || ":remote-address :incoming :method :url HTTP/:http-version :status-code :res-headers[content-length] :referer :user-agent[family] :user-agent[major].:user-agent[minor] :user-agent[os] :response-time ms"; | ||
delete opts.format; // don't pass it to bunyan | ||
(typeof format != 'function') && (format = compile(format)); | ||
opts.hasOwnProperty('parseUA') && (parseUA = opts.parseUA, delete opts.parseUA); | ||
if(opts.immediate) { | ||
if (opts.immediate) { | ||
immediate = opts.immediate; | ||
@@ -34,3 +34,3 @@ delete opts.immediate; | ||
if(opts.levelFn) { | ||
if (opts.levelFn) { | ||
levelFn = opts.levelFn; | ||
@@ -40,8 +40,11 @@ delete opts.levelFn; | ||
return function(err, req, res, next) { | ||
return function (err, req, res, next) { | ||
var startTime = Date.now(); | ||
function logging() { | ||
res.removeListener('finish', logging); | ||
res.removeListener('close', logging); | ||
function logging(incoming) { | ||
if(!incoming) { | ||
res.removeListener('finish', logging); | ||
res.removeListener('close', logging); | ||
} | ||
var app = req.app || res.app, | ||
@@ -53,3 +56,3 @@ status = res.statusCode, | ||
ua = parseUA ? useragent.parse(req.header('user-agent')) : req.header('user-agent'), | ||
httpVersion = req.httpVersionMajor+'.'+req.httpVersionMinor, | ||
httpVersion = req.httpVersionMajor + '.' + req.httpVersionMinor, | ||
responseTime = Date.now() - startTime, | ||
@@ -59,3 +62,3 @@ ip, logFn; | ||
if(!logger) { | ||
if (!logger) { | ||
opts.name = (opts.name || app.settings.shortname || app.settings.name || app.settings.title || 'express'); | ||
@@ -74,3 +77,3 @@ opts.serializers = opts.serializers || {}; | ||
ip = ip || req.ip || req.connection.remoteAddress || | ||
(req.socket && req.socket.remoteAddress) || | ||
(req.socket && req.socket.remoteAddress) || | ||
(req.socket.socket && req.socket.socket.remoteAddresss) || | ||
@@ -80,3 +83,3 @@ '127.0.0.1'; | ||
var meta = { | ||
'remote-address': ip, | ||
'remote-address': ip, | ||
'ip': ip, | ||
@@ -87,5 +90,6 @@ 'method': method, | ||
'user-agent': ua, | ||
'body': req.body && req.body.toString && req.body.toString().substring(0, Math.max(req.body.toString().length, 20)), | ||
'body': req.body, | ||
'short-body':req.body && req.body.toString && req.body.toString().substring(0, Math.max(req.body.toString().length, 20)), | ||
'http-version': httpVersion, | ||
'response-time': responseTime.toString(), | ||
'response-time': responseTime, | ||
"status-code": status, | ||
@@ -95,3 +99,4 @@ 'req-headers': req.headers, | ||
'req': req, | ||
'res': res | ||
'res': res, | ||
'incoming':incoming?'-->':'<--' | ||
}; | ||
@@ -103,10 +108,10 @@ | ||
if(immediate) { | ||
logging(); | ||
}else { | ||
res.on('finish', logging); | ||
res.on('close', logging); | ||
if (immediate) { | ||
logging(true); | ||
} | ||
res.on('finish', logging); | ||
res.on('close', logging); | ||
next(); | ||
@@ -117,9 +122,8 @@ }; | ||
function compile(fmt) { | ||
fmt = fmt.replace(/"/g, '\\"'); | ||
var js = ' return "' + fmt.replace(/:([-\w]{2,})(?:\[([^\]]+)\])?/g, function(_, name, arg){ | ||
if(arg) | ||
return '"\n + (meta["' + name + '"] ? (meta["'+ name + '"]["'+ arg + '"]||"-") : "-") + "'; | ||
return '"\n + ((meta["' + name + '"]) || "-") + "'; | ||
var js = ' return "' + fmt.replace(/:([-\w]{2,})(?:\[([^\]]+)\])?/g, function (_, name, arg) { | ||
if (arg) | ||
return '"\n + (meta["' + name + '"] ? (meta["' + name + '"]["' + arg + '"]|| (typeof meta["' + name + '"]["' + arg + '"] === "number"?"0": "-")) : "-") + "'; | ||
return '"\n + ((meta["' + name + '"]) || (typeof meta["'+name+'"] === "number"?"0": "-")) + "'; | ||
}) + '";'; | ||
@@ -131,5 +135,5 @@ return new Function('meta', js); | ||
function defaultLevelFn(status, err) { | ||
if(err || status >= 500) { // server internal error or error | ||
return "error" | ||
}else if (status >= 400) { // client error | ||
if (err || status >= 500) { // server internal error or error | ||
return "error"; | ||
} else if (status >= 400) { // client error | ||
return "warn"; | ||
@@ -136,0 +140,0 @@ } |
{ | ||
"name": "express-bunyan-logger", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "a bunyan logger middleware for express", | ||
@@ -19,4 +19,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"bunyan": "~0.21.4", | ||
"useragent": "~2.0.7" | ||
"bunyan": "0.22.3", | ||
"useragent": "2.0.8" | ||
}, | ||
@@ -23,0 +23,0 @@ "devDependencies": { |
@@ -45,2 +45,4 @@ # Express-bunyan-logger | ||
Or you can pass a function to _options.format_. This function accept a object as argument and return string. | ||
### options.parseUA | ||
@@ -47,0 +49,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12278
177
97
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedbunyan@0.22.3(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedglob@6.0.4(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedmkdirp@0.5.6(transitive)
+ Addedmv@2.1.1(transitive)
+ Addedncp@2.0.0(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedrimraf@2.4.5(transitive)
+ Addeduseragent@2.0.8(transitive)
+ Addedwrappy@1.0.2(transitive)
- Removedbunyan@0.21.4(transitive)
- Removedmv@0.0.5(transitive)
- Removeduseragent@2.0.10(transitive)
Updatedbunyan@0.22.3
Updateduseragent@2.0.8