From 2d1e71823e6e6fda7e05db0b34458ae61b432633 Mon Sep 17 00:00:00 2001 From: Hogan Bobertz Date: Wed, 18 Dec 2024 11:51:19 -0500 Subject: [PATCH] chore: enable log timestamps by default (#32448) ### Reason for this change timestamps were no longer being printed for debug and trace level logging messages, because as part of the logging changes, I incidentally removed the timestamps from the `debug()` and `trace()` functions. ### Description of changes Re-added timestamps for whenever those 2 methods are called i.e. ``` debug: "message" is now "[09:58:24] message" info: "message" is still "message" ``` which was the previous functionality ### Description of how you validated changes Modified relevant unit tests and also did manual testing by verifying outputs of cdk commands on different cdk versions ``` # running cdk synth from local build of bobertzh/logging-fix [15:08:06] CDK toolkit version: 0.0.0 (build 68b77e7) [15:08:06] Command line arguments: { ... Resources: CDKMetadata: ... ``` ``` # running cdk synth from live CDK toolkit version: 2.173.0 (build b5c2189) Command line arguments: { ... Resources: CDKMetadata: ... ``` ``` # running cdk synth from be000a251b781b0b0870930992793df5a2fc4b01 (parent commit) [15:20:33] CDK toolkit version: 0.0.0 (build be000a2) [15:20:33] Command line arguments: { ... Resources: CDKMetadata: ... ``` ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/aws-cdk/lib/logging.ts | 13 +++++++++++-- packages/aws-cdk/test/api/logs/cli-logging.test.ts | 8 ++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/aws-cdk/lib/logging.ts b/packages/aws-cdk/lib/logging.ts index 52c914d927e0e..fd1b8e5d0f47d 100644 --- a/packages/aws-cdk/lib/logging.ts +++ b/packages/aws-cdk/lib/logging.ts @@ -173,8 +173,17 @@ export const data = (fmt: string, ...args: unknown[]) => log({ message: util.format(fmt, ...args), forceStdout: true, }); -export const debug = (fmt: string, ...args: unknown[]) => log(LogLevel.DEBUG, fmt, ...args); -export const trace = (fmt: string, ...args: unknown[]) => log(LogLevel.TRACE, fmt, ...args); +export const debug = (fmt: string, ...args: unknown[]) => log({ + level: LogLevel.DEBUG, + message: util.format(fmt, ...args), + timestamp: true, +}); + +export const trace = (fmt: string, ...args: unknown[]) => log({ + level: LogLevel.TRACE, + message: util.format(fmt, ...args), + timestamp: true, +}); export const success = (fmt: string, ...args: unknown[]) => log({ level: LogLevel.INFO, diff --git a/packages/aws-cdk/test/api/logs/cli-logging.test.ts b/packages/aws-cdk/test/api/logs/cli-logging.test.ts index 08300a7e64890..bce7a9b35f231 100644 --- a/packages/aws-cdk/test/api/logs/cli-logging.test.ts +++ b/packages/aws-cdk/test/api/logs/cli-logging.test.ts @@ -81,7 +81,9 @@ describe('logging', () => { setLogLevel(LogLevel.DEBUG); debug('debug message'); - expect(mockStderr).toHaveBeenCalledWith('debug message\n'); + expect(mockStderr).toHaveBeenCalledWith( + expect.stringMatching(/^\[\d{2}:\d{2}:\d{2}\] debug message\n$/), + ); }); test('trace messages only show at trace level', () => { @@ -91,7 +93,9 @@ describe('logging', () => { setLogLevel(LogLevel.TRACE); trace('trace message'); - expect(mockStderr).toHaveBeenCalledWith('trace message\n'); + expect(mockStderr).toHaveBeenCalledWith( + expect.stringMatching(/^\[\d{2}:\d{2}:\d{2}\] trace message\n$/), + ); }); });