Skip to content

bug(logger): standardize extra input logging  #565

Closed
@dreamorosi

Description

@dreamorosi

Bug description

As described in the docs Logger accepts a second parameter that allows to append additional keys and values in a single log item.

This works when passing an object, for example logger.info('my-message', { data: 'my-value' }); results in:

{
  "cold_start": true,
  "function_arn": "arn:aws:lambda:eu-west-1:123456789101:function:myFunction",
  "function_memory_size": 128,
  "function_name": "AwsLambdaPowertoolsDemoSt-HelloWorldFunctionB2AB6E-sYstm6IvDAEB",
  "function_request_id": "b5fe0e01-91f5-43f2-8ef9-d572df88b61c",
  "level": "INFO",
  "message": "my-message",
  "service": "service_undefined",
  "timestamp": "2022-02-21T11:15:18.493Z",
  "data": "my-value"
}

But when passing directly a string, like: logger.info('my-message', 'my-value'); it results in this:

{
  "0": "m",
  "1": "y",
  "2": "-",
  "3": "v",
  "4": "a",
  "5": "l",
  "6": "u",
  "7": "e",
  "cold_start": false,
  "function_arn": "arn:aws:lambda:eu-west-1:123456789101:function:myFunction",
  "function_memory_size": 128,
  "function_name": "AwsLambdaPowertoolsDemoSt-HelloWorldFunctionB2AB6E-sYstm6IvDAEB",
  "function_request_id": "a0c5ab8f-c0f0-40fa-8c75-347020065880",
  "level": "INFO",
  "message": "my-message",
  "service": "service_undefined",
  "timestamp": "2022-02-21T11:08:19.611Z"
}

Likewise, when passing an array, logger.info('my-message', ['my-value', 'my-value2']); it results in:

{
  "0": "my-value",
  "1": "my-value2",
  "cold_start": true,
  "function_arn": "arn:aws:lambda:eu-west-1:123456789101:function:myFunction",
  "function_memory_size": 128,
  "function_name": "AwsLambdaPowertoolsDemoSt-HelloWorldFunctionB2AB6E-sYstm6IvDAEB",
  "function_request_id": "b5fe0e01-91f5-43f2-8ef9-d572df88b61c",
  "level": "INFO",
  "message": "my-message",
  "service": "service_undefined",
  "timestamp": "2022-02-21T11:15:18.493Z",
}

Expected Behavior

Have a consistent behaviour across data types, potentially using a default key when a string or array are passed, i.e. logger.info('my-message', 'my-value');

{
  "extra_data": "my-value",
  "cold_start": false,
  "function_arn": "arn:aws:lambda:eu-west-1:123456789101:function:myFunction",
  "function_memory_size": 128,
  "function_name": "AwsLambdaPowertoolsDemoSt-HelloWorldFunctionB2AB6E-sYstm6IvDAEB",
  "function_request_id": "a0c5ab8f-c0f0-40fa-8c75-347020065880",
  "level": "INFO",
  "message": "my-message",
  "service": "service_undefined",
  "timestamp": "2022-02-21T11:08:19.611Z"
}

Current Behavior

{
  "0": "m",
  "1": "y",
  "2": "-",
  "3": "v",
  "4": "a",
  "5": "l",
  "6": "u",
  "7": "e",
  "cold_start": false,
  "function_arn": "arn:aws:lambda:eu-west-1:123456789101:function:myFunction",
  "function_memory_size": 128,
  "function_name": "AwsLambdaPowertoolsDemoSt-HelloWorldFunctionB2AB6E-sYstm6IvDAEB",
  "function_request_id": "a0c5ab8f-c0f0-40fa-8c75-347020065880",
  "level": "INFO",
  "message": "my-message",
  "service": "service_undefined",
  "timestamp": "2022-02-21T11:08:19.611Z"
}

Expand below to see full CloudWatch log output

```log 2022-02-21T11:08:19.611Z a0c5ab8f-c0f0-40fa-8c75-347020065880 INFO {"0":"m","1":"y","2":"-","3":"v","4":"a","5":"l","6":"u","7":"e","cold_start":false,"function_arn":"arn:aws:lambda:eu-west-1:123456789101:function:AwsLambdaPowertoolsDemoSt-HelloWorldFunctionB2AB6E-sYstm6IvDAEB","function_memory_size":128,"function_name":"AwsLambdaPowertoolsDemoSt-HelloWorldFunctionB2AB6E-sYstm6IvDAEB","function_request_id":"a0c5ab8f-c0f0-40fa-8c75-347020065880","level":"INFO","message":"my-key","service":"service_undefined","timestamp":"2022-02-21T11:08:19.611Z"} ```

Possible Solution

Check data types and treat them accordingly.

Steps to Reproduce

  1. Use any version of @aws-lambda-powertools/logger in your Lambda
  2. Emit a lot entry and pass a string or array as second parameter
  3. Check logs

Environment

  • Powertools version used: any
  • Packaging format (Layers, npm): N/A
  • AWS Lambda function runtime: nodejs14.x
  • Debugging logs: (see above)

Related issues, RFCs

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcompletedThis item is complete and has been merged/shippedgood-first-issueSomething that is suitable for those who want to start contributingloggerThis item relates to the Logger Utility

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions