Skip to content

Commit

Permalink
Describe options.logger, enable syntax highlighting
Browse files Browse the repository at this point in the history
Fixes #40.
  • Loading branch information
BendingBender authored Sep 15, 2017
1 parent 9586ccf commit b5417c4
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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.
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down

0 comments on commit b5417c4

Please sign in to comment.