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

Ability to exclude paths in input-output-logger #507

Merged
merged 6 commits into from
Apr 18, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/input-output-logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ npm install --save @middy/input-output-logger
## Options

`logger` property accept a function (default `console.log`)
`omitPaths` property accepts an array of paths that will be used to remove particular fields from the logged objects. This could serve as a simple way to redact sensitive data from logs (default []).


## Sample usage
Expand Down
32 changes: 32 additions & 0 deletions packages/input-output-logger/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,36 @@ describe('📦 Middleware Input Output Logger', () => {
expect(logger).toHaveBeenCalledWith({ event: { foo: 'bar', fuu: 'baz' } })
expect(logger).toHaveBeenCalledWith({ response: { message: 'hello world' } })
})
describe('omitPaths', () => {
test('It should omit paths', async () => {
const logger = jest.fn()

const handler = middy((event, context, cb) => {
cb(null, { message: 'hello world', bar: 'bi' })
})

handler
.use(inputOutputLogger({ logger, omitPaths: ['event.foo', 'response.bar'] }))

await invoke(handler, { foo: 'bar', fuu: 'baz' })

expect(logger).toHaveBeenCalledWith({ event: { fuu: 'baz' } })
expect(logger).toHaveBeenCalledWith({ response: { message: 'hello world' } })
})
test('It should skip paths that do not exist', async () => {
const logger = jest.fn()

const handler = middy((event, context, cb) => {
cb(null, 'yo')
})

handler
.use(inputOutputLogger({ logger, omitPaths: ['event.zooloo', 'event.foo.hoo', 'response.bar'] }))

await invoke(handler, { foo: 'bar', fuu: 'baz' })

expect(logger).toHaveBeenCalledWith({ event: { foo: 'bar', fuu: 'baz' } })
expect(logger).toHaveBeenCalledWith({ response: 'yo' })
})
})
})
1 change: 1 addition & 0 deletions packages/input-output-logger/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import middy from '@middy/core';

interface IInputOutputLoggerOptions {
logger?: (message: any) => void;
omitPaths?: string[];
}

declare const inputOutputLogger : middy.Middleware<IInputOutputLoggerOptions, any, any>
Expand Down
20 changes: 14 additions & 6 deletions packages/input-output-logger/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
const omit = require('lodash/omit')

module.exports = (opts) => {
const defaults = {
logger: data => console.log(JSON.stringify(data, null, 2))
logger: data => console.log(JSON.stringify(data, null, 2)),
omitPaths: []
}

const options = Object.assign({}, defaults, opts)
const { logger, omitPaths } = Object.assign({}, defaults, opts)

const omitAndLog = message => {
const redactedMessage = omit(message, omitPaths)
logger(redactedMessage)
}

return ({
before: (handler, next) => {
if (typeof options.logger === 'function') {
options.logger({ event: handler.event })
if (typeof logger === 'function') {
omitAndLog({ event: handler.event })
}

return next()
},
after: (handler, next) => {
if (typeof options.logger === 'function') {
options.logger({ response: handler.response })
if (typeof logger === 'function') {
omitAndLog({ response: handler.response })
}

return next()
Expand Down
20 changes: 16 additions & 4 deletions packages/input-output-logger/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/input-output-logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@
"aws-sdk": ">=2.221.1"
},
"dependencies": {
"@types/node": "^10.0.8"
"lodash": "^4.17.15"
},
"devDependencies": {
"@middy/core": "^1.0.0-beta.10",
"@types/node": "^10.0.8",
lmammino marked this conversation as resolved.
Show resolved Hide resolved
"@types/lodash": "^4.14.149",
"es6-promisify": "^6.0.2"
},
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
Expand Down