Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 52e270d

Browse files
committedFeb 4, 2025
fix(logger): simplify log creation logic
1 parent e2449d0 commit 52e270d

File tree

1 file changed

+36
-39
lines changed

1 file changed

+36
-39
lines changed
 

‎packages/logger/src/Logger.ts

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ import type {
2525
LogLevel,
2626
LoggerInterface,
2727
} from './types/Logger.js';
28-
import type { PowertoolsLogData } from './types/logKeys.js';
28+
import type {
29+
PowertoolsLogData,
30+
UnformattedAttributes,
31+
} from './types/logKeys.js';
2932

3033
/**
3134
* The Logger utility provides an opinionated logger with output structured as JSON for AWS Lambda.
@@ -718,59 +721,53 @@ class Logger extends Utility implements LoggerInterface {
718721
input: LogItemMessage,
719722
extraInput: LogItemExtraInput
720723
): LogItem {
721-
let message = '';
722-
let otherInput: { [key: string]: unknown } = {};
723-
if (typeof input === 'string') {
724-
message = input;
725-
} else {
726-
const { message: inputMessage, ...rest } = input;
727-
message = inputMessage;
728-
for (const key of Object.keys(rest)) {
729-
if (this.#checkReservedKeyAndWarn(key)) {
730-
delete rest[key];
731-
}
732-
}
733-
otherInput = rest;
734-
}
735-
736-
// create base attributes
737-
const unformattedBaseAttributes = {
724+
const unformattedBaseAttributes: UnformattedAttributes = {
738725
logLevel: this.getLogLevelNameFromNumber(logLevel),
739726
timestamp: new Date(),
740-
message,
741727
xRayTraceId: this.envVarsService.getXrayTraceId(),
742728
...this.getPowertoolsLogData(),
729+
message: '',
743730
};
744731

745732
const additionalAttributes: LogAttributes = {};
746-
// gradually add additional attributes picking only the last added for each key
733+
// Add additional attributes from persistent and temporary keys
747734
for (const [key, type] of this.#keys) {
748-
if (type === 'persistent') {
749-
additionalAttributes[key] = this.persistentLogAttributes[key];
750-
} else {
751-
additionalAttributes[key] = this.temporaryLogAttributes[key];
735+
if (!this.#checkReservedKeyAndWarn(key)) {
736+
additionalAttributes[key] =
737+
type === 'persistent'
738+
? this.persistentLogAttributes[key]
739+
: this.temporaryLogAttributes[key];
752740
}
753741
}
754742

755-
// if the main input is not a string, then it's an object with additional attributes, so we merge it
756-
merge(additionalAttributes, otherInput);
757-
// then we merge the extra input attributes (if any)
743+
// Handle input message
744+
if (typeof input === 'string') {
745+
unformattedBaseAttributes.message = input;
746+
} else {
747+
const { message, ...rest } = input;
748+
unformattedBaseAttributes.message = message;
749+
750+
// Add remaining input properties if they're not reserved
751+
for (const [key, value] of Object.entries(rest)) {
752+
if (!this.#checkReservedKeyAndWarn(key)) {
753+
additionalAttributes[key] = value;
754+
}
755+
}
756+
}
757+
758+
// Handle extra input attributes
758759
for (const item of extraInput) {
759-
if (!(item instanceof Error) && !(typeof item === 'string')) {
760-
for (const key of Object.keys(item)) {
761-
if (this.#checkReservedKeyAndWarn(key)) {
762-
delete item[key];
760+
if (item instanceof Error) {
761+
additionalAttributes.error = item;
762+
} else if (typeof item === 'string') {
763+
additionalAttributes.extra = item;
764+
} else {
765+
for (const [key, value] of Object.entries(item)) {
766+
if (!this.#checkReservedKeyAndWarn(key)) {
767+
additionalAttributes[key] = value;
763768
}
764769
}
765770
}
766-
const attributes: LogAttributes =
767-
item instanceof Error
768-
? { error: item }
769-
: typeof item === 'string'
770-
? { extra: item }
771-
: item;
772-
773-
merge(additionalAttributes, attributes);
774771
}
775772

776773
return this.getLogFormatter().formatAttributes(

0 commit comments

Comments
 (0)
Please sign in to comment.