Way to log null? #4396
-
Hi - is there a way to log If there is no kind of flag for this, it would be cool if I could add some sort of middleware that always performs this check before logging an object. thanks in advance |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @shareefer, by default we remove For example: import { Logger } from '@aws-lambda-powertools/logger';
const logger = new Logger();
const nullVal = null;
logger.info('some val', nullVal); // key: nullVal - value: null -> removed
logger.info('some other val', { nullVal }); // key: extra - value: null -> removed
const comparison = {
before: true,
after: null
};
logger.info('wrapped', { comparison }); // not removed, see below
// {"level":"INFO","message":"wrapped","timestamp":"2025-08-28T22:35:30.694Z","service":"service_undefined","sampling_rate":0,"comparison":{"before":true,"after":null}} If your Extend the import { LogItem } from '@aws-lambda-powertools/logger';
import type { LogAttributes } from '@aws-lambda-powertools/logger/types';
class MyLogItem extends LogItem {
public removeEmptyKeys(attributes: LogAttributes) {
// your logic
return attributes;
}
} Then, use this Finally, use your custom log formatter that uses the modified import { Logger } from '@aws-lambda-powertools/logger';
const logger = new Logger({
logFormatter: new MyLogFormatter(),
// ... other configs
}); I realize that this is kind of verbose, so however far this is the first time we heard someone wanting to log these values at the top level. |
Beta Was this translation helpful? Give feedback.
Hi @shareefer, by default we remove
null
,undefined
, and''
, this however applies only to top-level key/value pairs. If yournull
value is within an object we'll log it.For example: