From 0492f13d101e895ba05ad11b78a19995c77342a5 Mon Sep 17 00:00:00 2001 From: Chris Atkin Date: Mon, 25 Oct 2021 11:57:40 +0100 Subject: [PATCH] Use config.logger as a logger constructor --- README.md | 11 +++++++++-- lib/{defaultLogger.js => defaultPinoConfig.js} | 5 ++--- lib/logger.js | 13 +++++++++---- tests/config/next-logger.config.js | 7 ++++++- ...aultLogger.spec.js => defaultPinoConfig.spec.js} | 4 ++-- 5 files changed, 28 insertions(+), 12 deletions(-) rename lib/{defaultLogger.js => defaultPinoConfig.js} (97%) rename tests/{defaultLogger.spec.js => defaultPinoConfig.spec.js} (97%) diff --git a/README.md b/README.md index ba0cb80..d00c2d6 100644 --- a/README.md +++ b/README.md @@ -70,13 +70,20 @@ The following presets are supported: By default, this library uses an instance of Pino with a modified [`logMethod`](https://getpino.io/#/docs/api?id=logmethod), to give reasonable out-the-box behaviour for JSON logging. If you need logs in a different format, for example to change the message field or transform logged objects, you can provide your own instance of Pino to the library. -This is done by creating a `next-logger.config.js` file in the root of your project. The file should be a CommonJS module, and your custom Pino instance should be exported in a field called `logger`. For example: +This is done by creating a `next-logger.config.js` file in the root of your project. The file should be a CommonJS module, and a function returning your custom Pino instance should be exported in a field called `logger`. This function will be called with the library's default Pino configuration, to allow you to extend it's behaviour (or completely replace it). + +For example: ```js // next-logger.config.js const pino = require('pino') -const logger = pino({ messageKey: 'message', mixin: () => ({ name: 'custom-pino-instance' }) }) +const logger = defaultConfig => + pino({ + ...defaultConfig, + messageKey: 'message', + mixin: () => ({ name: 'custom-pino-instance' }), + }) module.exports = { logger, diff --git a/lib/defaultLogger.js b/lib/defaultPinoConfig.js similarity index 97% rename from lib/defaultLogger.js rename to lib/defaultPinoConfig.js index 7f65abb..5fc0c24 100644 --- a/lib/defaultLogger.js +++ b/lib/defaultPinoConfig.js @@ -1,7 +1,6 @@ const { format } = require('util') -const pino = require('pino') -module.exports = pino({ +module.exports = { level: 'debug', hooks: { // https://getpino.io/#/docs/api?id=logmethod @@ -55,4 +54,4 @@ module.exports = pino({ return method.apply(this, [mergingObject, message]) }, }, -}) +} diff --git a/lib/logger.js b/lib/logger.js index 5738fbf..35f4538 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -1,9 +1,9 @@ const { cosmiconfigSync } = require('cosmiconfig') +const pino = require('pino') -const defaultLogger = require('./defaultLogger') +const defaultPinoConfig = require('./defaultPinoConfig') let config = {} -let logger = defaultLogger const explorerSync = cosmiconfigSync('next-logger') const results = explorerSync.search() @@ -12,8 +12,13 @@ if (results && results.config) { config = results.config } -if ('logger' in config && config.logger && typeof config.logger.child === 'function') { +// Set the default logger constructor to Pino. +let logger = pino + +// If logger exists in the config file, and it's a function, use it as the logger constructor. +if ('logger' in config && typeof config.logger === 'function') { logger = config.logger } -module.exports = logger +// Call the logger constructor with the library's default Pino configuration. +module.exports = logger(defaultPinoConfig) diff --git a/tests/config/next-logger.config.js b/tests/config/next-logger.config.js index e1ed533..14f75f5 100644 --- a/tests/config/next-logger.config.js +++ b/tests/config/next-logger.config.js @@ -1,6 +1,11 @@ const pino = require('pino') -const logger = pino({ messageKey: 'message', mixin: () => ({ name: 'custom-pino-instance' }) }) +const logger = defaultConfig => + pino({ + ...defaultConfig, + messageKey: 'message', + mixin: () => ({ name: 'custom-pino-instance' }), + }) module.exports = { logger, diff --git a/tests/defaultLogger.spec.js b/tests/defaultPinoConfig.spec.js similarity index 97% rename from tests/defaultLogger.spec.js rename to tests/defaultPinoConfig.spec.js index c1648bf..d1c5c64 100644 --- a/tests/defaultLogger.spec.js +++ b/tests/defaultPinoConfig.spec.js @@ -3,11 +3,11 @@ const { promisify } = require('util') const execAsync = promisify(exec) -describe('defaultLogger', () => { +describe('defaultPinoConfig', () => { const logArgs = async (args, method = 'info') => { // This is needed to escape double quotes in arguments. For example, turning "Hello World" into \"Hello World\" const safeArgs = args.map(arg => String(arg).replace(/"/g, '\\"')).join(',') - const process = execAsync(`node -e "require('./lib/defaultLogger').${method}(${safeArgs})"`) + const process = execAsync(`node -e "require('pino')(require('./lib/defaultPinoConfig')).${method}(${safeArgs})"`) const { stdout } = await process return JSON.parse(stdout.trim()) }