Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-bunyan-logger

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-bunyan-logger - npm Package Compare versions

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc