From a0e8bdb60687946cf36fb1157b8c23aea1c0dae9 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 8 Feb 2021 10:34:24 +1000 Subject: [PATCH] feat: add logSettings to override log level and default log level to error closes #360 --- README.md | 12 +++ .../scripts/local.js | 5 +- src/configure.js | 9 ++- src/index.js | 1 + src/logger.js | 79 +++++++++---------- 5 files changed, 59 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 7e53f9b8..e67f3715 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,18 @@ serverlessExpress({ }) ``` +### logSettings + +Specify log settings that are passed to the default logger. Currently, you can only set the log `level`. + +```js +{ + logSettings: { + level: 'debug' // default: 'error' + } +} +``` + ### log Provide a custom `log` object with `info`, `debug` and `error` methods. For example, you could override the default with a [Winston log](https://www.npmjs.com/package/winston) instance. diff --git a/examples/basic-starter-api-gateway-v1/scripts/local.js b/examples/basic-starter-api-gateway-v1/scripts/local.js index 1d2c95cb..3b8e27bd 100644 --- a/examples/basic-starter-api-gateway-v1/scripts/local.js +++ b/examples/basic-starter-api-gateway-v1/scripts/local.js @@ -7,12 +7,11 @@ const context = { process.exit(0) } } -const callback = (e, v) => { +const server = lambdaFunction.handler(apiGatewayEvent, context).then((e, v) => { if (e) console.error(e) if (v) console.info(v) process.exit(0) -} -const server = lambdaFunction.handler(apiGatewayEvent, context, callback) +}) process.stdin.resume() diff --git a/src/configure.js b/src/configure.js index 8cafc561..89fdb202 100644 --- a/src/configure.js +++ b/src/configure.js @@ -4,8 +4,9 @@ const proxy = require('./proxy') function configure ({ app: configureApp, - log: configureLogger = logger, - framework: configureFramework = getFramework({ app: configureApp, log: configureLogger }), + logSettings, + log: configureLog = logger(logSettings), + framework: configureFramework = getFramework({ app: configureApp, log: configureLog }), binaryMimeTypes: configureBinaryMimeTypes, binarySettings: configureBinarySettings, resolutionMode: configureResolutionMode = 'PROMISE', @@ -23,7 +24,7 @@ function configure ({ binaryMimeTypes = configureBinaryMimeTypes, binarySettings = configureBinarySettings, eventSource = configureEventFns, - log = configureLogger, + log = configureLog, respondWithErrors = configureRespondWithErrors } = {}) => proxy({ app: configureProxyApp, @@ -48,7 +49,7 @@ function configure ({ return { proxy: configureProxy, handler: configureHandler, - log: configureLogger + log: configureLog } } diff --git a/src/index.js b/src/index.js index 8728904e..b46bfedb 100644 --- a/src/index.js +++ b/src/index.js @@ -3,3 +3,4 @@ const { getCurrentInvoke } = require('./current-invoke') module.exports = configure module.exports.getCurrentInvoke = getCurrentInvoke +module.exports.configure = configure diff --git a/src/logger.js b/src/logger.js index 258370a3..458165b0 100644 --- a/src/logger.js +++ b/src/logger.js @@ -1,43 +1,42 @@ -const { - LOG_LEVEL = process.env.NODE_ENV === 'development' ? 'debug' : 'error' -} = process.env -const logger = { - // TODO: allow users to set log level without having to provide the other log methods - level: LOG_LEVEL, - error (message, additional) { - if (!logger.level.includes('debug', 'verbose', 'info', 'warn', 'error')) return - console.error({ - message, - ...additional - }) - }, - warn (message, additional) { - if (!logger.level.includes('debug', 'verbose', 'info', 'warn')) return - console.warn({ - message, - ...additional - }) - }, - info (message, additional) { - if (!logger.level.includes('debug', 'verbose', 'info')) return - console.info({ - message, - ...additional - }) - }, - verbose (message, additional) { - if (!logger.level.includes('debug', 'verbose')) return - console.debug({ - message, - ...additional - }) - }, - debug (message, additional) { - if (logger.level !== 'debug') return - console.debug({ - message, - ...additional - }) +function logger ({ + level = 'error' +} = {}) { + return { + error (message, additional) { + if (!level.includes('debug', 'verbose', 'info', 'warn', 'error')) return + console.error({ + message, + ...additional + }) + }, + warn (message, additional) { + if (!level.includes('debug', 'verbose', 'info', 'warn')) return + console.warn({ + message, + ...additional + }) + }, + info (message, additional) { + if (!level.includes('debug', 'verbose', 'info')) return + console.info({ + message, + ...additional + }) + }, + verbose (message, additional) { + if (!level.includes('debug', 'verbose')) return + console.debug({ + message, + ...additional + }) + }, + debug (message, additional) { + if (level !== 'debug') return + console.debug({ + message, + ...additional + }) + } } }