-
Notifications
You must be signed in to change notification settings - Fork 181
Description
Use case
When running my projects locally, I have lots of log messages and it can be hard to see the message I'm looking for. To fix this, I created styled-json-console, which creates a custom Console object that adds syntax highlighting (ANSI colors) to the JSON output.
In order for me to use my custom Console with Powertools Logger, I have to use Reflect.set() to get around the private access modifier.
Example:
import { Logger } from "@aws-lambda-powertools/logger";
const logger = new Logger({
// Options omitted for brevity
});
if (process.env.IS_OFFLINE === "true") {
const { createConsole } = await import("styled-json-console");
const styledConsole = createConsole({
// Options omitted for brevity
});
Reflect.set(logger, "console", styledConsole);
}This works, but I noticed that when doing this, the console.trace() patch gets undone.
powertools-lambda-typescript/packages/logger/src/Logger.ts
Lines 1110 to 1115 in e055e70
| /** | |
| * Patch `console.trace` to avoid printing a stack trace and aligning with AWS Lambda behavior - see #2902 | |
| */ | |
| this.console.trace = (message: string, ...optionalParams: unknown[]) => { | |
| this.console.log(message, ...optionalParams); | |
| }; |
It would be nice if there was an official way to do this instead of using Reflect.set().
Solution/User Experience
Add a devConsole option to Logger and use it in setConsole().
Alternative solutions
Remove `private` from `setConsole()` and add an argument for the `Console` I want to use.
Example:
setConsole(devConsole = console): void {
this.console = isDevMode() ? devConsole : new Console({
stdout: process.stdout,
stderr: process.stderr,
});
/**
* Patch `console.trace` to avoid printing a stack trace and aligning with AWS Lambda behavior - see #2902
*/
this.console.trace = (message: string, ...optionalParams: unknown[]) => {
this.console.log(message, ...optionalParams);
};
}Acknowledgment
- This feature request meets Powertools for AWS Lambda (TypeScript) Tenets
- Should this be considered in other Powertools for AWS Lambda languages? i.e. Python, Java, and .NET
Future readers
Please react with 👍 and your use case to help us understand customer demand.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status