Closed
Description
Bug (logger): logging fails if input contains bigint values
Since JSON.stringify
is unable to serialize bigint values, the logging function raises an exception if the input contains a bigint value.
Expected Behavior
The logger should handle bigints to prevent JSON.stringify
from raising an exception.
Log Request:
import { Logger } from "@aws-lambda-powertools/logger";
const logger = new Logger();
logger.info('Object with bigint value', { myBigInt: BigInt(1234) })
Log Result:
{
"level": "INFO",
"message": "Object with bigint value",
"service": "service_undefined",
"timestamp": "2022-10-12T22:27:17.067Z",
"myBigInt": "1234"
}
Current Behavior
The logger does not handle bigint values which causes JSON.stringify
to raise an exception.
Log Request:
import { Logger } from "@aws-lambda-powertools/logger";
const logger = new Logger();
logger.info('Object with bigint value', { myBigInt: BigInt(1234) })
Raised exception:
TypeError: Do not know how to serialize a BigInt
at JSON.stringify (<anonymous>)
...
Possible Solution
Handling bigint values in the removeCircularDependencies
method of the Logger
class should fix the issue.
Before
private removeCircularDependencies(): (key: string, value: LogAttributes | Error) => void {
const references = new WeakSet();
return (key, value) => {
let item = value;
if (item instanceof Error) {
item = this.getLogFormatter().formatError(item);
}
if (typeof item === 'object' && value !== null) {
if (references.has(item)) {
return;
}
references.add(item);
}
return item;
};
}
After
private removeCircularDependencies(): (key: string, value: LogAttributes | Error | bigint) => void {
const references = new WeakSet();
return (key, value) => {
let item = value;
if (typeof item === 'bigint') {
return item.toString();
}
if (item instanceof Error) {
item = this.getLogFormatter().formatError(item);
}
if (typeof item === 'object' && value !== null) {
if (references.has(item)) {
return;
}
references.add(item);
}
return item;
};
}
Environment
- Powertools version used: 1.2.1
- Packaging format (Layers, npm): npm
- AWS Lambda function runtime: nodejs16.x
- Debugging logs: none