Skip to content

Commit

Permalink
feat: add logSettings to override log level and default log level to …
Browse files Browse the repository at this point in the history
…error

closes #360
  • Loading branch information
brett-vendia committed Feb 8, 2021
1 parent 2fa39bb commit a0e8bdb
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 47 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions examples/basic-starter-api-gateway-v1/scripts/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
9 changes: 5 additions & 4 deletions src/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -23,7 +24,7 @@ function configure ({
binaryMimeTypes = configureBinaryMimeTypes,
binarySettings = configureBinarySettings,
eventSource = configureEventFns,
log = configureLogger,
log = configureLog,
respondWithErrors = configureRespondWithErrors
} = {}) => proxy({
app: configureProxyApp,
Expand All @@ -48,7 +49,7 @@ function configure ({
return {
proxy: configureProxy,
handler: configureHandler,
log: configureLogger
log: configureLog
}
}

Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ const { getCurrentInvoke } = require('./current-invoke')

module.exports = configure
module.exports.getCurrentInvoke = getCurrentInvoke
module.exports.configure = configure
79 changes: 39 additions & 40 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -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
})
}
}
}

Expand Down

0 comments on commit a0e8bdb

Please sign in to comment.