From 53f63298e8ed683b51266a029c220d88c26e7e19 Mon Sep 17 00:00:00 2001 From: kilisamemarisaaa <1798456934@qq.com> Date: Fri, 28 Aug 2026 18:17:55 +0800 Subject: [PATCH] fix: pass logger instances via loggerInstance for fastify v5 fastify v5 throws FST_ERR_LOG_INVALID_LOGGER_CONFIG when a logger instance is passed through the `logger` option (it only accepts a configuration object there). fastify-cli converges user-provided logger instances -- from --logging-module, --options files or server options -- into options.logger, so --logging-module with a real Pino instance crashed at startup. Route instances to loggerInstance after all option merges, and add a regression test with a real Pino instance fixture: the existing tests only used a config-object fixture, so the instance path had zero coverage. Co-Authored-By: EvoX --- start.js | 7 +++++++ test/data/custom-logger-instance.js | 5 +++++ test/start.test.js | 11 +++++++++++ 3 files changed, 23 insertions(+) create mode 100644 test/data/custom-logger-instance.js 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)