Skip to content

Commit

Permalink
Use config.logger as a logger constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
atkinchris committed Oct 25, 2021
1 parent 9015cd3 commit 0492f13
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions lib/defaultLogger.js → lib/defaultPinoConfig.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -55,4 +54,4 @@ module.exports = pino({
return method.apply(this, [mergingObject, message])
},
},
})
}
13 changes: 9 additions & 4 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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)
7 changes: 6 additions & 1 deletion tests/config/next-logger.config.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down

0 comments on commit 0492f13

Please sign in to comment.