Skip to content

Commit

Permalink
fix: Use AWS CDK Annotations to log messages
Browse files Browse the repository at this point in the history
Use AWS CDK Annotations to log messages instead of our custom Logger.

Annotations have coloured output (info = white, warning = yellow, error = red).

They also work with the `--trace`, `--strict` and `--ignore-errors` flags
of the CDK CLI.

Note, we don't need to employ the trick in #434 as Annotations (somehow)
do not get piped to disk if one redirects output to disk.

Before:

```console
➜ npx cdk synth --strict --path-metadata false --version-reporting false
 # GuStack has 'migratedFromCloudFormation' set to false. MySnsTopic is a stateful construct, it's logicalId will be auto-generated and AWS will create a new resource.
Parameters:
  Stage:
    Type: String
    Default: CODE
    AllowedValues:
      - CODE
      - PROD
    Description: Stage name
```

After:

```console
➜ npx cdk synth --strict --path-metadata false --version-reporting false
[Info at /CdkPlayground/MySnsTopic] GuStack has 'migratedFromCloudFormation' set to false. MySnsTopic is a stateful construct, it's logicalId will be auto-generated and AWS will create a new resource.
Parameters:
  Stage:
    Type: String
    Default: CODE
    AllowedValues:
      - CODE
      - PROD
    Description: Stage name
```

See:
  - https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.Annotations.html
  • Loading branch information
akash1810 committed May 7, 2021
1 parent 859ef3e commit 48f66d1
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 67 deletions.
6 changes: 3 additions & 3 deletions src/constructs/core/migrating.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "@aws-cdk/assert/jest";
import { SynthUtils } from "@aws-cdk/assert";
import type { BucketProps } from "@aws-cdk/aws-s3";
import { Bucket } from "@aws-cdk/aws-s3";
import { Logger } from "../../utils/logger";
import { Annotations } from "@aws-cdk/core";
import type { GuStatefulConstruct } from "../../utils/mixin";
import type { SynthedStack } from "../../utils/test";
import { simpleGuStackForTesting } from "../../utils/test";
Expand All @@ -24,8 +24,8 @@ We're calling it here to test the function in isolation.
*/

describe("GuMigratingResource", () => {
const info = jest.spyOn(Logger, "info");
const warn = jest.spyOn(Logger, "warn");
const info = jest.spyOn(Annotations.prototype, "addInfo");
const warn = jest.spyOn(Annotations.prototype, "addWarning");

afterEach(() => {
warn.mockReset();
Expand Down
8 changes: 4 additions & 4 deletions src/constructs/core/migrating.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CfnElement, IConstruct } from "@aws-cdk/core";
import { Logger } from "../../utils/logger";
import { Annotations } from "@aws-cdk/core";
import { isGuStatefulConstruct } from "../../utils/mixin";

export interface GuMigratingStack {
Expand Down Expand Up @@ -55,19 +55,19 @@ export const GuMigratingResource = {
}

if (isStateful) {
Logger.warn(
Annotations.of(construct).addWarning(
`GuStack has 'migratedFromCloudFormation' set to true. ${id} is a stateful construct and 'existingLogicalId' has not been set. ${id}'s logicalId will be auto-generated and consequently AWS will create a new resource rather than inheriting an existing one. This is not advised as downstream services, such as DNS, will likely need updating.`
);
}
} else {
if (existingLogicalId) {
Logger.warn(
Annotations.of(construct).addWarning(
`GuStack has 'migratedFromCloudFormation' set to false. ${id} has an 'existingLogicalId' set to ${existingLogicalId}. This will have no effect - the logicalId will be auto-generated. Set 'migratedFromCloudFormation' to true for 'existingLogicalId' to be observed.`
);
}

if (isStateful) {
Logger.info(
Annotations.of(construct).addInfo(
`GuStack has 'migratedFromCloudFormation' set to false. ${id} is a stateful construct, it's logicalId will be auto-generated and AWS will create a new resource.`
);
}
Expand Down
58 changes: 0 additions & 58 deletions src/utils/logger.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/utils/mixin/migratable-construct-stateful.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { BucketProps } from "@aws-cdk/aws-s3";
import "../test/jest";
import { Bucket } from "@aws-cdk/aws-s3";
import { Annotations } from "@aws-cdk/core";
import type { GuStack } from "../../constructs/core";
import type { GuMigratingResource } from "../../constructs/core/migrating";
import { Logger } from "../logger";
import { simpleGuStackForTesting } from "../test";
import { GuStatefulMigratableConstruct } from "./migratable-construct-stateful";

Expand All @@ -16,7 +16,7 @@ class StatefulTestGuMigratableConstruct extends GuStatefulMigratableConstruct(Bu
}

describe("The GuStatefulMigratableConstruct mixin", () => {
const info = jest.spyOn(Logger, "info");
const info = jest.spyOn(Annotations.prototype, "addInfo");

beforeEach(() => {
info.mockReset();
Expand Down

0 comments on commit 48f66d1

Please sign in to comment.