Comparing version 1.0.3 to 2.0.0
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
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
0
9
89
0
6869
6
132
1
+ Addedcolors@^1.1.2
+ Addedrotatelog-stream@^1.0.2
+ Addedcolors@1.4.0(transitive)
+ Addeddebug@2.6.9(transitive)
+ Addedms@2.0.0(transitive)
+ Addedrotatelog-stream@1.0.3(transitive)
+ Addedutil-asyncflow@0.2.8(transitive)
- Removedutils-merge@^1.0.0
- Removedutils-merge@1.0.1(transitive)
Updatedbunyan@^1.8.10
Updatedmkdirp@^0.5.1
Updatednode-uuid@^1.4.8