question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: child logger with overwritten options clear all other options

See original GitHub issue

Expected Behaviour

When creating a child logger (see “Code snippet” section) and passing some options the child logger should retain all the parent properties/options except the ones explicitly overwritten.

Current Behaviour

The child logger won’t have all the same options that the parent has whenever the user creates the logger while also passing some options to overwrite. For example:

const logger = new Logger({
  serviceName: "MyService",
  sampleRateValue: 0.01,
});
const auditLogger = logger.createChild({
  sampleRateValue: 1,
});

From the example above, I would expect auditLogger to have the same serviceName as logger and only differ in sampleRateValue - however as you can see from the logs in the “Debugging logs” section, serviceName is undefined in auditLogger.

Code snippet

import { Logger, injectLambdaContext } from "@aws-lambda-powertools/logger";
import middy from "@middy/core";

const logger = new Logger({
  serviceName: "MyService",
  sampleRateValue: 0.01,
});
const auditLogger = logger.createChild({
  sampleRateValue: 1,
});

export const handler = middy(async () => {
  logger.info("I log sometimes");
  auditLogger.info("I always log");
})
  .use(injectLambdaContext(logger))
  .use(injectLambdaContext(auditLogger));

Possible Solution

Rework the createChild function in Logger to account for passed options and add unit tests to confirm fix & prevent future regressions.

Steps to Reproduce

Deploy a function using the code shared in the “Code snippet” section, then execute it, finally observe the logs (see example in Debugging logs section).

AWS Lambda Powertools for TypeScript version

latest

AWS Lambda function runtime

16.x

Packaging format used

Npm

Debugging logs

{
    "cold_start": false,
    "function_arn": "arn:aws:lambda:eu-west-1:536254204126:function:lambda-powertools-playground-fn",
    "function_memory_size": 128,
    "function_name": "lambda-powertools-playground-fn",
    "function_request_id": "6bb4c509-31d0-4dc7-9e16-f08f22a70b59",
    "level": "INFO",
    "message": "I log sometimes",
    "sampling_rate": 0.01,
    "service": "MyService",
    "timestamp": "2022-11-13T16:47:24.460Z",
    "xray_trace_id": "1-63711f9c-4c1fc3284f97466b7ff88473"
}
{
    "cold_start": false,
    "function_arn": "arn:aws:lambda:eu-west-1:536254204126:function:lambda-powertools-playground-fn",
    "function_memory_size": 128,
    "function_name": "lambda-powertools-playground-fn",
    "function_request_id": "6bb4c509-31d0-4dc7-9e16-f08f22a70b59",
    "level": "INFO",
    "message": "I always log",
    "sampling_rate": 1,
    "service": "MyService",
    "timestamp": "2022-11-13T16:47:24.461Z",
    "xray_trace_id": "1-63711f9c-4c1fc3284f97466b7ff88473"
}

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dreamorosicommented, Nov 16, 2022

Hey @shdq, indeed I copy-pasted the logs wrong. I just checked and it’s like you say (which is the issue I was trying to report).

Thank you for stepping up on this, I’ll assign the issue to you and will be happy to collaborate.

0reactions
github-actions[bot]commented, Nov 23, 2022

⚠️ COMMENT VISIBILITY WARNING ⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

Read more comments on GitHub >

github_iconTop Results From Across the Web

logging — Logging facility for Python — Python 3.11.1 ...
Loggers have the following attributes and methods. ... Loggers that are further down in the hierarchical list are children of loggers higher up...
Read more >
Python Logging Guide - Best Practices and Hands-on Examples
Without overriding the filter method, such a filter will admit records emitted by loggers that have the same name as the filter and...
Read more >
Logs Don't Overwrite - Stack Overflow
RotatingFileHandler instead of logging.FileHandler seems to disable the mode: 'w' option in the YAML configuration file. The asker in the linked ...
Read more >
Logger - loguru documentation - Read the Docs
The Logger is the core object of loguru , every logging configuration and usage pass through a call to one of its methods....
Read more >
Node Logging Basics - The Ultimate Guide To Logging - Loggly
I passed in a number of configuration options to give it a sense of substance. These are: ... You can even set the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found