From b5417c4241713a2f1988920b72ed035bae63bb5b Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Sat, 16 Sep 2017 00:19:46 +0200 Subject: [PATCH] Describe options.logger, enable syntax highlighting Fixes #40. --- README.md | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 1da8817..db5b503 100644 --- a/README.md +++ b/README.md @@ -17,31 +17,39 @@ This year as work content change, I have no spare time to maintaining the node m To use the logger: - app.use(require('express-bunyan-logger')()); +```js +app.use(require('express-bunyan-logger')()); +``` To use the errorLogger: - app.use(require('express-bunyan-logger').errorLogger()); +```js +app.use(require('express-bunyan-logger').errorLogger()); +``` And you can also pass bunyan logger options to the logger middleware: - app.use(require('express-bunyan-logger')({ - name: 'logger', - streams: [{ - level: 'info', - stream: process.stdout - }] - })); +```js +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" - }); +```js +app.use(require('express-bunyan-logger')({ + format: ":remote-address - :user-agent[major] custom logger" +}); +``` And a child logger will be attached to each request object: -```javascript +```js app.use(require('express-bunyan-logger')(); app.use(function(req, res, next) { req.log.debug('this is debug in middleware'); @@ -51,6 +59,10 @@ app.use(function(req, res, next) { ## Configuration +### options.logger + +A custom instance of `bunyan` logger to use in the middware. Useful if you already do all of the logging with `bunyan` and don't want this middleware to create a new unconfigured instance. Also useful if you want to pass a child logger into this middleware. + ### options.format Format string, please go the source code to the metadata. ":name" will print out meta.name; ":name[key]" will print out the property 'key' of meta.name. @@ -65,7 +77,7 @@ Whether to parse _user-agent_ in logger, default is =true=. Function that translate statusCode into log level. The `meta` argument is an object consisting of all the fields gathered by bunyan-express-logger, before exclusions are applied. -``` +```js function(status, err /* only will work in error logger */, meta) { // return string of level if (meta["response-time"] > 30000) { @@ -80,7 +92,7 @@ function(status, err /* only will work in error logger */, meta) { Function that is passed `req` and `res`, and returns an object whose properties will be added to the meta object passed to bunyan -```javascript +```js function(req, res) { if (req.user) { return { @@ -110,7 +122,7 @@ Default is `[HIDDEN]`. An object of [bunyan serializers](https://github.com/trentm/node-bunyan#serializers). They are passed on to bunyan. The default serializers are defined as follows: -``` +```js { req: bunyan.stdSerializers.req, res: bunyan.stdSerializers.res, @@ -128,7 +140,7 @@ By default, `express-bunyan-logger` will generate an unique id for each request, If you have already use other middleware/framework to generate request id, you can pass a function to retrieve it: -```javascript +```js // suppose connect-requestid middleware is already added. app.use(require('express-bunyan-logger')({ genReqId: function(req) {