Skip to content

Docs: add notice about mutating attributes in custom log formatters #3588

@VladimirGorobetsWildix

Description

@VladimirGorobetsWildix

Expected Behavior

No change original event fields.

Current Behavior

Original event fields changed in logFormatter after upgrade 2.13.1 -> 2.14.0

Image

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

  1. Run code with @aws-lambda-powertools/logger: 2.13.1
  2. Receive response ==> Event in handler (raw) {"headers":{"authorization":"Bearer aaa.bbb.ccc","content-type":"application/json"}}
  3. 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/shippedloggerThis item relates to the Logger Utilitynot-a-bugNew and existing bug reports incorrectly submitted as bug

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions