Skip to content

Commit

Permalink
chore: enable log timestamps by default (#32448)
Browse files Browse the repository at this point in the history
### 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 be000a2 (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*
  • Loading branch information
HBobertz authored Dec 18, 2024
1 parent 043f9db commit 2d1e718
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
13 changes: 11 additions & 2 deletions packages/aws-cdk/lib/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions packages/aws-cdk/test/api/logs/cli-logging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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$/),
);
});
});

Expand Down

0 comments on commit 2d1e718

Please sign in to comment.