generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 181
Closed
Labels
completedThis item is complete and has been merged/shippedThis item is complete and has been merged/shippedloggerThis item relates to the Logger UtilityThis item relates to the Logger Utilitynot-a-bugNew and existing bug reports incorrectly submitted as bugNew and existing bug reports incorrectly submitted as bug
Description
Expected Behavior
No change original event fields.
Current Behavior
Original event fields changed in logFormatter after upgrade 2.13.1 -> 2.14.0
Code snippet
import middy from '@middy/core'
import {LogFormatter, Logger, LogItem} from "@aws-lambda-powertools/logger";
import type {LogAttributes, UnformattedAttributes} from '@aws-lambda-powertools/logger/types';
import {injectLambdaContext} from "@aws-lambda-powertools/logger/middleware";
class MyCompanyLogFormatter extends LogFormatter {
private hideFields = [
'authorization',
'password',
'secret',
'key',
'token',
]
processObject(obj: Record<string, any>) {
for (const key in obj) {
if (typeof obj[key] === 'object') {
this.processObject(obj[key])
} else if (typeof obj[key] === 'string' && obj[key].length) {
if (this.hideFields.includes(key)) {
obj[key] = '**** hidden ****'; // Maybe incorrect solution, but it worked in 2.13.1
}
}
}
}
public formatAttributes(
attributes: UnformattedAttributes,
additionalLogAttributes: LogAttributes
): LogItem {
const {message, ...rest} = structuredClone(attributes);
try {
this.processObject(rest);
this.processObject(additionalLogAttributes);
} catch (e) {
}
const logItem = new LogItem({
attributes: {
message,
...rest,
},
})
logItem.addAttributes(additionalLogAttributes);
return logItem;
}
}
(async () => {
const logger = new Logger({
logFormatter: new MyCompanyLogFormatter()
})
const lambdaHandler = async (event: Event) => {
logger.info('==> Event in handler (secure)', {event});
console.log('==> Event in handler (raw)', JSON.stringify(event));
return {success: true}
}
const handler = middy()
.use(injectLambdaContext(logger, {logEvent: true}))
.handler(lambdaHandler)
const rawEvent = {
headers: {
authorization: 'Bearer aaa.bbb.ccc',
'content-type': 'application/json',
}
}
// @ts-ignore
await handler(rawEvent, {}, {});
})()Steps to Reproduce
- Run code with
@aws-lambda-powertools/logger: 2.13.1 - Receive response
==> Event in handler (raw) {"headers":{"authorization":"Bearer aaa.bbb.ccc","content-type":"application/json"}} - Run code with
@aws-lambda-powertools/logger: 2.14.0
4 Receive response==> Event in handler (raw) {"headers":{"authorization":"**** hidden ****","content-type":"application/json"}}
Possible Solution
No response
Powertools for AWS Lambda (TypeScript) version
2.14.0
AWS Lambda function runtime
22.x
Packaging format used
npm
Execution logs
Metadata
Metadata
Assignees
Labels
completedThis item is complete and has been merged/shippedThis item is complete and has been merged/shippedloggerThis item relates to the Logger UtilityThis item relates to the Logger Utilitynot-a-bugNew and existing bug reports incorrectly submitted as bugNew and existing bug reports incorrectly submitted as bug
Type
Projects
Status
Shipped