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.0.2 to 0.0.3

115

index.js

@@ -14,59 +14,82 @@ var bunyan = require('bunyan'),

module.exports.errorLogger = function(opts) {
var logger, opts = opts || {};
var logger, opts = opts || {
// default format
format: ":remote-address - :method :url HTTP/:http-version :status-code :content-length :referer :user-agent[family] :user-agent[major].:user-agent[minor] :user-agent[os]"
};
(typeof opts.format != 'function') && (opts.format = compile(opts.format));
return function(err, req, res, next) {
if(err) console.log(err.stack);
var app = req.app || res.app,
status = res.statusCode,
method = req.method,
url = req.url || '-',
referer = req.header('referer') || '-',
ua = useragent.parse(req.header('user-agent')),
httpVersion = req.httpVersionMajor+'.'+req.httpVersionMinor,
ip, logFn;
function logging() {
res.removeListener('finish', logging);
res.removeListener('close', logging);
var app = req.app || res.app,
status = res.statusCode,
method = req.method,
url = req.url || '-',
referer = req.header('referer') || '-',
ua = useragent.parse(req.header('user-agent')),
httpVersion = req.httpVersionMajor+'.'+req.httpVersionMinor,
ip, logFn;
if(!logger) {
opts.name = (opts.name || app.settings.shortname || app.settings.name || app.settings.title || 'express');
opts.serializers = opts.serializers || {};
opts.serializers.req = opts.serializers.req || bunyan.stdSerializers.req;
opts.serializers.res = opts.serializers.res || bunyan.stdSerializers.res;
err && ( opts.serializers.err = opts.serializers.err || bunyan.stdSerializers.err);
logger = bunyan.createLogger(opts);
}
if(err || status >= 500) { // server internal error or error
logFn = logger.error;
}else if (status >= 400) { // client error
logFn = logger.warn;
}else { // redirect/success
logFn = logger.info;
}
if(!logger) {
opts.name = (opts.name || app.settings.shortname || app.settings.name || app.settings.title || 'express');
opts.serializers = opts.serializers || {};
opts.serializers.req = opts.serializers.req || bunyan.stdSerializers.req;
opts.serializers.res = opts.serializers.res || bunyan.stdSerializers.res;
err && ( opts.serializers.err = opts.serializers.err || bunyan.stdSerializers.err);
logger = bunyan.createLogger(opts);
}
ip = ip || req.ip || req.connection.remoteAddress ||
(req.socket && req.socket.remoteAddress) ||
(req.socket.socket && req.socket.socket.remoteAddresss) ||
'127.0.0.1';
if(err || status >= 500) { // server internal error or error
logFn = logger.error;
}else if (status >= 400) { // client error
logFn = logger.warn;
}else { // redirect/success
logFn = logger.info;
}
var meta = {
'remoteAddress': ip,
'method': method,
'url': url,
'referer': referer,
'user-agent': ua,
'body': req.body && req.body.toString && req.body.toString().substring(0, Math.max(req.body.toString().length, 20)),
'http-version': httpVersion,
"statusCode": status,
'req': req,
'res': res
};
ip = ip || req.ip || req.connection.remoteAddress ||
(req.socket && req.socket.remoteAddress) ||
(req.socket.socket && req.socket.socket.remoteAddresss) ||
'127.0.0.1';
err && (meta.err = err);
var meta = {
'remote-address': ip,
'ip': ip,
'method': method,
'url': url,
'referer': referer,
'user-agent': ua,
'body': req.body && req.body.toString && req.body.toString().substring(0, Math.max(req.body.toString().length, 20)),
'http-version': httpVersion,
"status-code": status,
'content-length': req.get('Content-Length'),
'req': req,
'res': res
};
logFn.call(logger, meta, [
ip, '- -', method, url, 'HTTP/'+httpVersion, status,
res.get('Content-Length'), referer, ua.family, ua.major+'.'+ua.minor, ua.os,
err ? "error occurs" : ""].join(' '));
err && (meta.err = err);
logFn.call(logger, meta, opts.format(meta));
}
res.on('finish', logging);
res.on('close', logging);
next();
};
};
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 + '"]) || "-") + "';
}) + '";'
return new Function('meta', js);
};

2

package.json
{
"name": "express-bunyan-logger",
"version": "0.0.2",
"version": "0.0.3",
"description": "a bunyan logger middleware for express",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -24,4 +24,17 @@ # Express-bunyan-logger

app.use(require('express-bunyan-logger')({name: 'logger', streams : [ {level :'info', stream: process.stdout}]}));
app.use(require('express-bunyan-logger')({
name: 'logger',
streams: [{
level: 'info',
stream: process.stdout
}]
}));
Change default format:
app.use(require('express-bunyan-logger')({
format: ":remote-address - :user-agent[major] custom logger"
});
## License

@@ -28,0 +41,0 @@

var express = require('express'),
request = require('supertest'),
bunyanLogger = require('../')
;
request = require('supertest'),
bunyanLogger = require('../');

@@ -10,3 +9,2 @@

var app = express();
app.use(app.router);
app.use(bunyanLogger());

@@ -28,2 +26,13 @@

it('test 404 statusCode', function(done) {
var app = express();
app.use(bunyanLogger());
request(app)
.get('/missing')
.end(function(err, res) {
done();
});
});
it('test errorLogger', function(done) {

@@ -30,0 +39,0 @@ var app = express();

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