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

bunlogger

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bunlogger - npm Package Compare versions

Comparing version 1.0.3 to 2.0.0

.jshintrc

128

index.js

@@ -1,127 +0,9 @@

const path = require('path');
const bunyan = require('bunyan');
const uuid = require('node-uuid');
const merge = require('utils-merge');
const mkdirp = require('mkdirp');
const errserial = require('./lib/error-serializer');
'use strict';
module.exports = function(options){
exports = module.exports = require('./lib/bunlogger');
options = options || {};
var streams = Array.isArray(options.streams) ? options.streams : [];
exports.serialErr = require('./lib/serialErr');
// if options.path, convert `path` into bunyan path stream
if (options.path) {
streams.push({
path: options.path,
type: 'file',
level: options.level && bunyan[options.level.toUpperCase()] || bunyan.INFO
});
exports.printErr = require('./lib/printErr');
mkdirp.sync(path.dirname(options.path));
}
// accept writable stream
if (options.stream) streams.push({
stream: options.stream,
type: 'stream',
level: options.level && bunyan[options.level.toUpperCase()] || bunyan.INFO
});
// default to process.stdout stream, if no stream
if (streams.length == 0) streams.push({
stream: process.stdout,
type: 'stream',
level: options.level && bunyan[options.level.toUpperCase()] || bunyan.INFO
});
// bind req serializer
bunyan.stdSerializers.req = function req(req) {
if (!req || !req.connection)
return req;
return {
method: req.method,
url: req.originalUrl || req.url, // in express req.url is changed
headers: req.headers,
remoteAddress: req.connection.remoteAddress,
remotePort: req.connection.remotePort
};
};
// create logger
var logger;
logger = bunyan.createLogger({
name : options.name || 'express app',
streams: streams,
serializers:merge(bunyan.stdSerializers, {
err: errserial,
clietReq: client_req,
clietRes: client_res
})
});
logger.connect = function(){
return function(req, res, next){
req.log = logger.child( {cor_id: uuid.v4()} );
req._starttime = new Date();
res.on('finish', logging);
res.on('close', logging);
return next();
function logging(){
res.removeListener('finish', logging);
res.removeListener('close', logging);
req.log.info({
req: req,
res: res,
elapsed: new Date - req._starttime
}, 'access log');
}
};
};
logger.error = function (){
return function(err, req, res, next){
req.log.warn(err, 'error log: %s', err);
next(err);
};
};
return logger;
};
// serializers
function client_req(req) {
if (!req) return req;
var host;
try {
host = req.host.split(':')[0];
}
catch (er) {
host = false;
}
return {
method: req ? req.method : false,
url: req ? req.path : false,
address: host,
port: req ? req.port : false,
headers: req ? req.headers : false
};
}
function client_res(res) {
if (!res || !res.statusCode) return res;
return {
statusCode: res.statusCode,
headers: res.headers
};
}
exports.Logger = require('./lib/logger');
{
"name": "bunlogger",
"version": "1.0.3",
"description": "bunyan logger middleware for express/connect",
"main": "index.js",
"scripts": {
"test": "make test"
},
"version": "2.0.0",
"description": "General logger for all my projects, a better version than mint-robot/util-logger",
"author": "Ben P.P. Tung",
"license": "MIT",
"repository": {

@@ -13,17 +11,14 @@ "type": "git",

},
"author": "Ben P.P. Tung",
"license": "MIT",
"main": "index.js",
"scripts": {
"test": "mocha test"
},
"dependencies": {
"bunyan": "^1.0.0",
"mkdirp": "^0.5.0",
"node-uuid": "^1.4.1",
"util-superagent-serializer": "^1.0.0",
"utils-merge": "^1.0.0"
},
"devDependencies": {
"express": "^4.4.4",
"mocha": "^1.20.1",
"should": "^4.0.4",
"supertest": "^0.13.0"
"bunyan": "^1.8.10",
"colors": "^1.1.2",
"mkdirp": "^0.5.1",
"node-uuid": "^1.4.8",
"rotatelog-stream": "^1.0.2",
"util-superagent-serializer": "^1.0.1"
}
}
bunlogger
=========
express middleware powered by bunyan
logging utilities built on Bunyan and RotateLog-Stream

@@ -17,18 +17,17 @@ Installation

3. `elapsed` show the time spent in this `req`.
4. Use [rotatelog-stream](https://www.npmjs.com/package/rotatelog-stream) for rotating logs.
5. Better error information for debugging.
Usage
Quick Start
==========
###app.js
### express
```
const express = require('express');
const config = require('config');
const Bunlog = require('bunlogger');
// set up app
//===============
var app = express();
var logger = Bunlog(config.bunlog);
var logger = new Bunlog();

@@ -38,18 +37,54 @@ // as a middleware

// as an error middleware
// as an error handler
app.use(logger.error());
```
###config/index.js
### Create logger
logger is using [rotatelog-stream](https://www.npmjs.com/package/rotatelog-stream) for rotating logs.
```
const Logger = require('..').Logger;
const join = require('path').join;
const pkgname = require('../package.json').name;
const production = process.env.NODE_ENV == 'production';
var log = new Logger({logDir: join(__dirname, 'log')});
// logging config
var bunlog = {name: pkgname};
if (production) bunlog.path = join(__dirname, '..', 'logs', pkgname + '.log');
log.info('hi info');
log.warn('hi warn');
log.error('hi error');
exports.bunlog = bunlog;
```
### Print Error
```
const print = require('bunlogger').printErr;
try {
new NotExist();
} catch (er) {
print(er)
}
```
### cli
make sure bunyan is installed globally
```
$ DEBUG=* node bin/www | bunyan
```
# Options
- name: {String},
- logDir: 'path/to/log/directory' - directory to save log files
- keep: {Number} - how many log files keep. See [rotatelog-stream](https://www.npmjs.com/package/rotatelog-stream)
- maxsize: {Number} - max filesize allowed. See [rotatelog-stream](https://www.npmjs.com/package/rotatelog-stream)
- dev: {Boolean} - Enable `process.stdout` for development
- streams: {Array} - additional streams for Bunyan

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