microservice-chain-logger
Advanced tools
Comparing version 1.0.9 to 1.1.0
36
index.js
@@ -11,2 +11,4 @@ const {format} = require('util'); | ||
module.exports = { | ||
maxMessageLength: 8000, | ||
getCorrelationId, | ||
@@ -33,2 +35,5 @@ assignCorrelationId, | ||
delete entry.isAccessLog; | ||
if (typeof entry.message === 'string') { | ||
entry.message = entry.message.substring(0, module.exports.maxMessageLength); | ||
} | ||
return JSON.stringify(entry); | ||
@@ -193,2 +198,3 @@ } | ||
opts = opts || {}; | ||
module.exports.maxMessageLength = opts.maxMessageLength || module.exports.maxMessageLength; | ||
if (opts.useTextTransformer !== undefined && !opts.useTextTransformer) { | ||
@@ -201,2 +207,8 @@ opts.useJsonTransformer = true; | ||
return function(req, res, next) { | ||
if (opts.injectIntoReq) { | ||
req.logger = bindRequest(req); | ||
} | ||
if (opts.assignCorrelationId) { | ||
module.exports.assignCorrelationId(req); | ||
} | ||
const startTime = Date.now(); | ||
@@ -215,1 +227,25 @@ onFinished(res, () => { | ||
} | ||
/** | ||
* create a new logger object which already has | ||
* a req-parameter preset for all methods expecting it | ||
* (naive curring) | ||
*/ | ||
function bindRequest(req) { | ||
return [ | ||
'getCorrelationId', | ||
'assignCorrelationId', | ||
'makeEntry', | ||
'info', | ||
'error', | ||
'debug', | ||
'warn', | ||
'infoSource' | ||
].reduce( | ||
(logger, methodName) => { | ||
logger[methodName] = (...args) => module.exports[methodName](req, ...args); | ||
return logger; | ||
}, | ||
{} | ||
); | ||
} |
{ | ||
"name": "microservice-chain-logger", | ||
"version": "1.0.9", | ||
"version": "1.1.0", | ||
"description": "JSON logger for microservices with bundled Correlation ID and http-server access logging", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -119,5 +119,7 @@ [![build status](https://travis-ci.org/jochen-schweizer/microservice-chain-logger.png)](https://travis-ci.org/jochen-schweizer/microservice-chain-logger) [![Coverage Status](https://coveralls.io/repos/github/jochen-schweizer/microservice-chain-logger/badge.svg?branch=master)](https://coveralls.io/github/jochen-schweizer/microservice-chain-logger?branch=master) [![license](https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000)](https://www.tldrlegal.com/l/mit) [![NPM version](https://badge.fury.io/js/microservice-chain-logger.png)](http://badge.fury.io/js/microservice-chain-logger) | ||
The only supported option | ||
is **useJsonTransformer**, which is a shortcut for replacing | ||
**transformEntry** with **jsonTransformer** function. | ||
Options: | ||
* **useJsonTransformer** - replace default **textTransformer** with **jsonTransformer** function | ||
* **maxMessageLength** - override default `maxMessageLength=8000` for **jsonTransformer** | ||
* **injectIntoReq** - automatically create **req.logger** bound to the request | ||
* **assignCorrelationId** - automatically assign a new **correlationId**, if none was provided in headers | ||
@@ -128,3 +130,4 @@ ```javascript | ||
app.use(logger.initAccessLog({ | ||
useJsonTransformer: process.env.NODE_ENV === 'production' | ||
useJsonTransformer: process.env.NODE_ENV === 'production', | ||
assignCorrelationId: true | ||
})); | ||
@@ -131,0 +134,0 @@ ``` |
18300
217
313
4