From 4c94c206e78beeae6e4ca6d32e7b56b06477027f Mon Sep 17 00:00:00 2001 From: Francis Brito Date: Wed, 9 Oct 2019 15:40:24 -0400 Subject: [PATCH] feat: add support for customizing logger configuration (#207) --- args.js | 8 +++++--- help/start.txt | 4 ++++ start.js | 22 +++++++++++++++++++--- test/args.test.js | 16 ++++++++++++---- test/data/custom-logger.js | 6 ++++++ test/start.test.js | 29 +++++++++++++++++++++++++++++ 6 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 test/data/custom-logger.js diff --git a/args.js b/args.js index 2fccf7ea..2663eb9c 100644 --- a/args.js +++ b/args.js @@ -6,7 +6,7 @@ module.exports = function parseArgs (args) { const parsedArgs = argv(args, { number: ['port', 'inspect-port', 'body-limit', 'plugin-timeout'], boolean: ['pretty-logs', 'options', 'watch', 'debug'], - string: ['log-level', 'address', 'socket', 'prefix', 'ignore-watch'], + string: ['log-level', 'address', 'socket', 'prefix', 'ignore-watch', 'logging-module'], envPrefix: 'FASTIFY_', alias: { port: ['p'], @@ -20,7 +20,8 @@ module.exports = function parseArgs (args) { 'debug-port': ['I'], 'log-level': ['l'], 'pretty-logs': ['P'], - 'plugin-timeout': ['T'] + 'plugin-timeout': ['T'], + 'logging-module': ['L'] }, default: { 'log-level': 'fatal', @@ -48,6 +49,7 @@ module.exports = function parseArgs (args) { logLevel: parsedArgs.logLevel, address: parsedArgs.address, socket: parsedArgs.socket, - prefix: parsedArgs.prefix + prefix: parsedArgs.prefix, + loggingModule: parsedArgs.loggingModule } } diff --git a/help/start.txt b/help/start.txt index 5c2ee70a..6b7084e2 100644 --- a/help/start.txt +++ b/help/start.txt @@ -16,6 +16,10 @@ Usage: fastify start [opts] [env: FASTIFY_LOG_LEVEL] Log level (default to fatal) + -L, --logging-module + [env: FASTIFY_LOGGING_MODULE] + Path to logging configuration module to use + -P, --pretty-logs [env: FASTIFY_PRETTY_LOGS] Prints pretty logs diff --git a/start.js b/start.js index ae68d897..45109ba5 100755 --- a/start.js +++ b/start.js @@ -5,6 +5,8 @@ require('dotenv').config() const assert = require('assert') +const path = require('path') + const updateNotifier = require('update-notifier') const PinoColada = require('pino-colada') const pump = require('pump') @@ -83,10 +85,24 @@ function runFastify (args, cb) { return module.exports.stop(e) } + let logger + if (opts.loggingModule) { + try { + const moduleFilePath = path.resolve(opts.loggingModule) + const moduleFileExtension = path.extname(opts.loggingModule) + const modulePath = moduleFilePath.split(moduleFileExtension)[0] + + logger = require(modulePath) + } catch (e) { + module.exports.stop(e) + } + } + + const defaultLogger = { + level: opts.logLevel + } const options = { - logger: { - level: opts.logLevel - }, + logger: logger || defaultLogger, pluginTimeout: opts.pluginTimeout } diff --git a/test/args.test.js b/test/args.test.js index 6964bc73..d0fed434 100644 --- a/test/args.test.js +++ b/test/args.test.js @@ -19,6 +19,7 @@ test('should parse args correctly', t => { '--body-limit', '5242880', '--debug', 'true', '--debug-port', 1111, + '--logging-module', './custom-logger.js', 'app.js' ] const parsedArgs = parseArgs(argv) @@ -37,7 +38,8 @@ test('should parse args correctly', t => { pluginTimeout: 500, bodyLimit: 5242880, debug: true, - debugPort: 1111 + debugPort: 1111, + loggingModule: './custom-logger.js' }) }) @@ -58,6 +60,7 @@ test('should parse args with = assignment correctly', t => { '--body-limit=5242880', '--debug=true', '--debug-port', 1111, + '--logging-module', './custom-logger.js', 'app.js' ] const parsedArgs = parseArgs(argv) @@ -76,7 +79,8 @@ test('should parse args with = assignment correctly', t => { pluginTimeout: 500, bodyLimit: 5242880, debug: true, - debugPort: 1111 + debugPort: 1111, + loggingModule: './custom-logger.js' }) }) @@ -96,6 +100,7 @@ test('should parse env vars correctly', t => { process.env.FASTIFY_PLUGIN_TIMEOUT = '500' process.env.FASTIFY_DEBUG = 'true' process.env.FASTIFY_DEBUG_PORT = '1111' + process.env.FASTIFY_LOGGING_MODULE = './custom-logger.js' t.teardown(function () { delete process.env.FASTIFY_PORT @@ -111,6 +116,7 @@ test('should parse env vars correctly', t => { delete process.env.FASTIFY_PLUGIN_TIMEOUT delete process.env.FASTIFY_DEBUG delete process.env.FASTIFY_DEBUG_PORT + delete process.env.FASTIFY_LOGGING_MODULE }) const parsedArgs = parseArgs([]) @@ -129,12 +135,13 @@ test('should parse env vars correctly', t => { socket: 'fastify.io.socket:9999', pluginTimeout: 500, debug: true, - debugPort: 1111 + debugPort: 1111, + loggingModule: './custom-logger.js' }) }) test('should respect default values', t => { - t.plan(9) + t.plan(10) const argv = [ 'app.js' @@ -151,4 +158,5 @@ test('should respect default values', t => { t.is(parsedArgs.pluginTimeout, 10000) t.is(parsedArgs.debug, false) t.is(parsedArgs.debugPort, 9320) + t.is(parsedArgs.loggingModule, undefined) }) diff --git a/test/data/custom-logger.js b/test/data/custom-logger.js new file mode 100644 index 00000000..f58b07aa --- /dev/null +++ b/test/data/custom-logger.js @@ -0,0 +1,6 @@ +module.exports = { + name: 'Custom Logger', + customLevels: { + test: 99 + } +} diff --git a/test/start.test.js b/test/start.test.js index 60f95d5b..a587a2b6 100644 --- a/test/start.test.js +++ b/test/start.test.js @@ -545,3 +545,32 @@ test('boolean env are not overridden if no arguments are passed', t => { t.pass('Custom options') } }) + +test('should support custom logger configuration', t => { + t.plan(2) + + const argv = ['-L', './test/data/custom-logger.js', './examples/plugin.js'] + start.start(argv, (err, fastify) => { + t.error(err) + t.ok(fastify.log.test) + + fastify.close() + }) +}) + +test('should throw on logger configuration module not found', t => { + t.plan(2) + + const oldStop = start.stop + t.tearDown(() => { start.stop = oldStop }) + start.stop = function (err) { + t.ok(/Cannot find module/.test(err.message), err.message) + } + + const argv = ['-L', './test/data/missing.js', './examples/plugin.js'] + start.start(argv, (err, fastify) => { + t.error(err) + + fastify.close() + }) +})