Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for customizing logger configuration #207

Merged
merged 1 commit into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions args.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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',
Expand Down Expand Up @@ -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
}
}
4 changes: 4 additions & 0 deletions help/start.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ Usage: fastify start [opts] <file>
[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
Expand Down
22 changes: 19 additions & 3 deletions start.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
}
Expand Down
16 changes: 12 additions & 4 deletions test/args.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -37,7 +38,8 @@ test('should parse args correctly', t => {
pluginTimeout: 500,
bodyLimit: 5242880,
debug: true,
debugPort: 1111
debugPort: 1111,
loggingModule: './custom-logger.js'
})
})

Expand All @@ -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)
Expand All @@ -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'
})
})

Expand All @@ -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
Expand All @@ -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([])
Expand All @@ -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'
Expand All @@ -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)
})
6 changes: 6 additions & 0 deletions test/data/custom-logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
name: 'Custom Logger',
customLevels: {
test: 99
}
}
29 changes: 29 additions & 0 deletions test/start.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})