Bug: child logger with overwritten options clear all other options
See original GitHub issueExpected 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:
- Created 10 months ago
- Comments:5 (5 by maintainers)
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.
⚠️ 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.