diff --git a/start.js b/start.js index ef0d614c..b2ddb479 100755 --- a/start.js +++ b/start.js @@ -171,6 +171,13 @@ async function runFastify (args, additionalOptions, serverOptions, serverModule) options.trustProxy = opts.trustProxy } + // fastify v5 only accepts a configuration object in `logger`; + // logger instances must be passed via `loggerInstance` + if (options.logger && typeof options.logger.child === 'function') { + options.loggerInstance = options.logger + delete options.logger + } + const fastify = Fastify(options) if (opts.prefix) { diff --git a/test/data/custom-logger-instance.js b/test/data/custom-logger-instance.js new file mode 100644 index 00000000..0f54c2bd --- /dev/null +++ b/test/data/custom-logger-instance.js @@ -0,0 +1,5 @@ +'use strict' + +const pino = require('pino') + +module.exports = pino({ level: 'warn' }) diff --git a/test/start.test.js b/test/start.test.js index 35300171..b50a5c77 100644 --- a/test/start.test.js +++ b/test/start.test.js @@ -981,6 +981,17 @@ test('should support custom logger configuration in ESM', async t => { t.pass('server closed') }) +test('should support a logger instance from a logging module', async t => { + t.plan(2) + + const argv = ['-L', './test/data/custom-logger-instance.js', './examples/plugin.js'] + const fastify = await start.start(argv) + t.equal(fastify.log.level, 'warn') + + await fastify.close() + t.pass('server closed') +}) + test('preloading a built-in module works', async t => { t.plan(1)