From dd6c1d3d56fcadc1d86e85d47c44085e61978e30 Mon Sep 17 00:00:00 2001 From: Felipe Caputo Date: Wed, 19 Dec 2018 12:15:28 -0200 Subject: [PATCH 1/3] update logging format --- src/main.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main.js b/src/main.js index 6b50c72..b23247b 100644 --- a/src/main.js +++ b/src/main.js @@ -1,15 +1,20 @@ -const { transports, createLogger } = require('winston'); +const { transports, format, createLogger } = require('winston'); const Sentry = require('winston-raven-sentry'); function getLogger(mod) { const path = mod.filename.split('/').slice(-2).join('/'); const winstonTransports = [ - new (transports.Console)({ - timestamp: true, - colorize: false, + new transports.Console({ + format: format.combine( + format.align(), + format.metadata(), + format.label({ label: path }), + format.colorize(), + format.timestamp(), + format.simple(), + ), level: process.env.CONSOLE_LOG_LEVEL || 'info', - label: path, - stderrLevels: ['error'] + stderrLevels: ['error'], }), ]; @@ -21,7 +26,7 @@ function getLogger(mod) { app: process.env.SENTRY_APP, environment: process.env.SENTRY_ENVIRONMENT, }, - config: { + config: { captureUnhandledRejections: process.env.CAPTURE_UNHANDLED_REJECTIONS || false, }, release: process.env.SENTRY_RELEASE, From baa3353ed2f8901cf99dd303dd508b2b1f882fb1 Mon Sep 17 00:00:00 2001 From: Felipe Caputo Date: Wed, 19 Dec 2018 16:17:33 -0200 Subject: [PATCH 2/3] change formatting --- src/customFormats.js | 65 ++++++++++++++++++++++++++++++++++++++++++++ src/main.js | 9 +++--- 2 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 src/customFormats.js diff --git a/src/customFormats.js b/src/customFormats.js new file mode 100644 index 0000000..5a21c33 --- /dev/null +++ b/src/customFormats.js @@ -0,0 +1,65 @@ +const { format } = require('winston'); + +function fillExcept(info, fillExceptKeys, metadataKey) { + const savedKeys = fillExceptKeys.reduce((acc, key) => { + acc[key] = info[key]; + delete info[key]; + return acc; + }, {}); + const metadata = Object.keys(info).reduce((acc, key) => { + acc[key] = info[key]; + delete info[key]; + return acc; + }, {}); + + Object.assign(info, savedKeys, { + [metadataKey]: metadata + }); + return info; +} + +function fillWith(info, fillWithKeys, metadataKey) { + info[metadataKey] = fillWithKeys.reduce((acc, key) => { + acc[key] = info[key]; + delete info[key]; + return acc; + }, {}); + return info; +} + +const extraData = format((info, opts = {}) => { + let metadataKey = 'extra_data'; + if (opts.key) { + metadataKey = opts.key; + } + + let fillExceptKeys = []; + if (!opts.fillExcept && !opts.fillWith) { + fillExceptKeys.push('level'); + fillExceptKeys.push('message'); + } + + if (opts.fillExcept) { + fillExceptKeys = opts.fillExcept; + } + + if (fillExceptKeys.length > 0) { + return fillExcept(info, fillExceptKeys, metadataKey); + } + + if (opts.fillWith) { + return fillWith(info, opts.fillWith, metadataKey); + } + + return info; +}); + +const loggerName = format((info, path) => { + info.logger_name = path; + return info; +}); + +module.exports = { + loggerName, + extraData, +}; diff --git a/src/main.js b/src/main.js index b23247b..21819ba 100644 --- a/src/main.js +++ b/src/main.js @@ -1,17 +1,16 @@ const { transports, format, createLogger } = require('winston'); const Sentry = require('winston-raven-sentry'); +const { loggerName, extraData } = require('./customFormats'); function getLogger(mod) { const path = mod.filename.split('/').slice(-2).join('/'); const winstonTransports = [ new transports.Console({ format: format.combine( - format.align(), - format.metadata(), - format.label({ label: path }), - format.colorize(), + extraData(), + loggerName(path), format.timestamp(), - format.simple(), + format.json(), ), level: process.env.CONSOLE_LOG_LEVEL || 'info', stderrLevels: ['error'], From 5e943b5fdd79c5ea55ef653181deef6cbdeaa361 Mon Sep 17 00:00:00 2001 From: Felipe Caputo Date: Wed, 19 Dec 2018 17:54:46 -0200 Subject: [PATCH 3/3] update readme --- README.md | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1d6eb5f..31731be 100644 --- a/README.md +++ b/README.md @@ -13,19 +13,53 @@ Winston logger with sentry configuration included. Also show the file from which | CONSOLE_LOG_LEVEL | The level of the logs displayed on the console (optional, defaults to info) | | CAPTURE_UNHANDLED_REJECTIONS | A value (true or false) saying if you want these exceptions to be logged in you app | -## Usage +## Setting up ```sh npm install --save git+https://github.com/quintoandar/node-logger.git# ``` + +Or add it on your `package.json` file like: + +```sh +"dependencies": { + "quintoandar-logger": "git+https://github.com/quintoandar/node-logger.git#", + }, +``` + [See releases](https://github.com/quintoandar/node-logger/releases) +## Usage + +### Info + +When logging info you are able to send the string (the info message) plus any other metadata you want as the second parameter. + ```js const logger = require('quintoandar-logger').getLogger(module); -logger.info("Some info"); +const object = { id: 11, someInfo: 'someInfo' } +logger.info(`Some info about processing cool object with id ${object.id}`, { data: object }); ``` +It will be logged as a json: +```sh +{"level":"info","message":"Some info about processing cool object with id 10","extra_data":{"data":{"id":"11","someInfo":"someInfo"}},"logger_name":"path/to/my/file.js","timestamp":"2018-12-19T18:15:57.078Z"} +``` + +### Warning and Error + +With warning and error messages the behaviour is similar, with one differece: both level of logging are submitted to Sentry, so if you want to submit metadata you need to include it on a specific key named `extra`. + +```js +const logger = require('quintoandar-logger').getLogger(module); + +const object = { id: 11, someInfo: 'someInfo' } +logger.error(`Some error while processing cool object with id ${object.id}`, { extra: { data: object } }); +``` + +And then it will be displayed on Sentry under the field `Additional Data`. + ## TODO - Create Express Middleware Request Logger