diff --git a/start.js b/start.js index 28442fbb..11c34aab 100755 --- a/start.js +++ b/start.js @@ -164,6 +164,10 @@ async function runFastify (args, additionalOptions, serverOptions, serverModule) options = deepmerge(options, file.options) } + if (options.loggerInstance) { + delete options.logger + } + if (opts.trustProxy) { options.trustProxy = opts.trustProxy } diff --git a/test/data/plugin-with-logger-instance.js b/test/data/plugin-with-logger-instance.js new file mode 100644 index 00000000..876ffbf0 --- /dev/null +++ b/test/data/plugin-with-logger-instance.js @@ -0,0 +1,25 @@ +'use strict' + +const messages = [] + +function noop () {} + +const loggerInstance = { + level: 'info', + messages, + info (msg) { messages.push(msg) }, + error: noop, + debug: noop, + fatal: noop, + warn: noop, + trace: noop, + child () { return this } +} + +module.exports = function (fastify, options, next) { + next() +} + +module.exports.options = { + loggerInstance +} diff --git a/test/helper.test.js b/test/helper.test.js index fc2b91c5..45ea178e 100644 --- a/test/helper.test.js +++ b/test/helper.test.js @@ -173,6 +173,37 @@ test('should merge the CLI and FILE configs', async t => { t.assert.strictEqual(lines[0].foo, '***') }) +test('should accept a loggerInstance in the server options', async t => { + const argv = ['./examples/plugin.js'] + const messages = [] + function noop () {} + const loggerInstance = { + level: 'info', + info (msg) { messages.push(msg) }, + error: noop, + debug: noop, + fatal: noop, + warn: noop, + trace: noop, + child () { return this } + } + + const app = await helper.build(argv, {}, { loggerInstance }) + t.after(() => app.close()) + app.log.info('hello') + t.assert.deepStrictEqual(messages, ['hello']) +}) + +test('should accept a loggerInstance in the plugin options', async t => { + const argv = ['./test/data/plugin-with-logger-instance.js', '--options'] + const { options } = require('./data/plugin-with-logger-instance') + + const app = await helper.build(argv, {}) + t.after(() => app.close()) + app.log.info('hello') + t.assert.deepStrictEqual(options.loggerInstance.messages, ['hello']) +}) + test('should ensure can access all decorators', async t => { const argv = ['./examples/plugin.js'] const app = await helper.build(argv, { skipOverride: true })