Skip to content

Commit

Permalink
Add ability to replace standard serializers via log config (#17)
Browse files Browse the repository at this point in the history
* Update readme

* Add ability to replace standard serializers via log config

* 0.1.5
  • Loading branch information
taddgiles authored Aug 10, 2018
1 parent fd81000 commit a91ba91
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 23 deletions.
20 changes: 14 additions & 6 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,16 @@ function getMiddlewareConfig (config, logger) {
excludeHeaders: config.excludeHeaders || [],
filter: config.middlewareFilter,
additionalRequestFinishData: (req, res) => {
const extraFields = Object.assign({
event: 'REQUEST',
tenant: req.headers['x-kuali-tenant'],
lane: req.headers['x-kuali-lane']
}, (config.additionalRequestFinishData ? config.additionalRequestFinishData(req, res) : {}))
const extraFields = Object.assign(
{
event: 'REQUEST',
tenant: req.headers['x-kuali-tenant'],
lane: req.headers['x-kuali-lane']
},
config.additionalRequestFinishData
? config.additionalRequestFinishData(req, res)
: {}
)
return extraFields
}
}
Expand All @@ -73,7 +78,10 @@ function getLoggerConfig (config) {
product: config.product,
environment: config.environment,
level: config.level || 'info',
serializers: { err: bunyan.stdSerializers.err },
serializers: Object.assign(
{ err: bunyan.stdSerializers.err },
config.serializers || {}
),
streams: [
{
level: config.level || 'info',
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kuali-logger",
"version": "0.1.4",
"version": "0.1.5",
"description": "Standardized logger for Kuali apps",
"main": "lib/index.js",
"scripts": {
Expand Down Expand Up @@ -52,4 +52,4 @@
"standard": "^10.0.3",
"supertest": "^3.0.0"
}
}
}
65 changes: 50 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
[![bitHound Overall Score](https://www.bithound.io/projects/badges/2258aae0-8f63-11e7-b68c-bd8234fdabb6/score.svg)](https://www.bithound.io/github/KualiCo/kuali-logger)
[![bitHound Dependencies](https://www.bithound.io/projects/badges/2258aae0-8f63-11e7-b68c-bd8234fdabb6/dependencies.svg)](https://www.bithound.io/github/KualiCo/kuali-logger/master/dependencies/npm)
[![CircleCI](https://circleci.com/gh/KualiCo/kuali-logger/tree/master.svg?style=shield&circle-token=b8b3696e50f3bce018f444658e7bd9c738d41751)](https://circleci.com/gh/KualiCo/kuali-logger/tree/master)
[![codecov](https://codecov.io/gh/KualiCo/kuali-logger/branch/master/graph/badge.svg)](https://codecov.io/gh/KualiCo/kuali-logger)

Expand Down Expand Up @@ -43,18 +41,20 @@ const log = require('kuali-logger')(config.get('log'))

These are the supported configuration options:

| Option | Type | Valid values or examples | Default | Required |
| --- | :---: | --- | :---: | :---: |
| `name` | string | res-coi-production | | X |
| `team` | string | res | | X |
| `product` | string | coi | | X |
| `environment` | string | production | | X |
| `level` | string | `trace`, `debug`, `info`, `warn`, `error`, `fatal` | `info` ||
| `format` | string | `short`, `long`, `simple`, `json`, `bunyan`, `pretty` | `json` ||
| `obscureHeaders` | array of strings | `['x-newrelic-id', 'ip']` | `['authorization', 'cookie']` ||
| `excludeHeaders` | array of strings | `['x-real-ip','x-newrelic-transaction']` | `[]` ||
| `stream` | object | ```{ name: 'myStream', stream: process.stdout, level: 'debug', outputFormat: 'json' }``` | bunyan-format stream ||
| `src` | boolean | false, true *(Slows output, don't use in production)* | false ||
| Option | Type | Valid values or examples | Default | Required |
| ------------------ | :------------------------------------------------------------------------: | ---------------------------------------------------------------------------------------- | :---------------------------: | :------: |
| `name` | string | res-coi-production | | X |
| `team` | string | res | | X |
| `product` | string | coi | | X |
| `environment` | string | production | | X |
| `level` | string | `trace`, `debug`, `info`, `warn`, `error`, `fatal` | `info` |
| `format` | string | `short`, `long`, `simple`, `json`, `bunyan`, `pretty` | `json` |
| `obscureHeaders` | array of strings | `['x-newrelic-id', 'ip']` | `['authorization', 'cookie']` |
| `excludeHeaders` | array of strings | `['x-real-ip','x-newrelic-transaction']` | `[]` |
| `stream` | object | ```{ name: 'myStream', stream: process.stdout, level: 'debug', outputFormat: 'json' }``` | bunyan-format stream |
| `src` | boolean | false, true *(Slows output, don't use in production)* | false |
| `serializers` | [object with functions](https://github.com/trentm/node-bunyan#serializers) | valid javascript serializer functions | |
| `middlewareFilter` | function | | valid javascript function | |

### Log Example

Expand Down Expand Up @@ -94,7 +94,7 @@ Adding the logger middleware will automatically log all request and response eve
```js
const express = require('express')
const log = require('kuali-logger')({
...config.get('log'),
...config.get('log'),
additionalRequestFinishData: (req, res) => ({
userId: req.userInfo.id,
userName: req.userInfo.userName
Expand Down Expand Up @@ -198,6 +198,21 @@ if(err) {
}
```
## Filter log output
You can filter log output by creating a function that modifies the `req` and `res` objects and applying it to the log configuration.
```js
function myMiddlewareFilter (req, res) {
const pattern = /(\/readyz$|\/healthz$|\/metrics$|\/health$)/
return req.headers['x-synthetic'] || pattern.test(req.url)
}

module.exports = {
log: {
middlewareFilter: myMiddlewareFilter
}
}
```
## Configure a custom stream
You can override the default stream configuration with your own [stream configuration](https://github.com/trentm/node-bunyan#streams).
Expand All @@ -222,6 +237,26 @@ const logConfig = {
const log = require('kuali-logger')(logConfig)
```
## Replace a standard serializer
You can replace the built-in serializers (`req` and `res`) with your own functions.
```js
function myResSerializer(res) {
return {
statusCode: res.statusCode,
header: res._header
}
}

module.exports = {
log: {
serializers: {
res: myResSerializer
}
}
}
```
## Add a custom serializer
You can use the standard bunyan method to [add a custom serializer](https://github.com/trentm/node-bunyan#serializers).
Expand Down

0 comments on commit a91ba91

Please sign in to comment.