Skip to content

Implement OutputLogFormatter to Enhance JSON Response Privacy #2243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions packages/logger/src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {
LogItemMessage,
LoggerInterface,
PowertoolsLogData,
OutputFormatter,
} from './types/Logger.js';

/**
Expand Down Expand Up @@ -162,6 +163,10 @@ class Logger extends Utility implements LoggerInterface {

private powertoolsLogData: PowertoolsLogData = <PowertoolsLogData>{};

private outputFormatter: OutputFormatter = (str: string): string => {
return str;
};

/**
* Log level used by the current instance of Logger.
*
Expand Down Expand Up @@ -798,13 +803,21 @@ class Logger extends Utility implements LoggerInterface {
'critical'
>);

this.console[consoleMethod](
JSON.stringify(
log.getAttributes(),
this.getReplacer(),
this.logIndentation
)
let result = JSON.stringify(
log.getAttributes(),
this.getReplacer(),
this.logIndentation
);

const formatted = this.outputFormatter(result);
try {
JSON.parse(formatted);
result = formatted;
} catch (e) {
// console.warn('Incorrect formatted JSON. Use original data.');
}

this.console[consoleMethod](result);
}

/**
Expand Down Expand Up @@ -999,6 +1012,7 @@ class Logger extends Utility implements LoggerInterface {
customConfigService,
persistentLogAttributes,
environment,
outputFormatter,
} = options;

// order is important, EnvVarsService() is used by other methods
Expand All @@ -1012,6 +1026,7 @@ class Logger extends Utility implements LoggerInterface {
this.setLogEvent();
this.setLogIndentation();
this.addPersistentLogAttributes(persistentLogAttributes);
this.setOutputFormatter(outputFormatter);

return this;
}
Expand Down Expand Up @@ -1044,6 +1059,12 @@ class Logger extends Utility implements LoggerInterface {
});
this.addPersistentLogAttributes(persistentLogAttributes);
}

private setOutputFormatter(formatter?: OutputFormatter): void {
if (formatter) {
this.outputFormatter = formatter;
}
}
}

export { Logger };
4 changes: 4 additions & 0 deletions packages/logger/src/types/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type InjectLambdaContextOptions = {
clearState?: boolean;
};

type OutputFormatter = (jsonString: string) => string;

type ConstructorOptions = {
logLevel?: LogLevel;
serviceName?: string;
Expand All @@ -29,6 +31,7 @@ type ConstructorOptions = {
customConfigService?: ConfigServiceInterface;
persistentLogAttributes?: LogAttributes;
environment?: Environment;
outputFormatter?: OutputFormatter;
};

type LambdaFunctionContext = Pick<
Expand Down Expand Up @@ -96,4 +99,5 @@ export type {
PowertoolsLogData,
ConstructorOptions,
InjectLambdaContextOptions,
OutputFormatter,
};