Skip to content

Commit b9372bd

Browse files
committed
fix(cli): get logs from all log groups
and delete them
1 parent 8317754 commit b9372bd

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

.github/workflows/test-and-release.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ jobs:
9898
- name: Clean up End-to-End test resources
9999
if: always()
100100
run: |
101+
./cli.sh logs -X
101102
npx cdk destroy -f
102103
npx cdk --app 'npx tsx --no-warnings cdk/test-resources.ts' destroy -f
103104
./cli.sh fake-nrfcloud-account-device --remove

cli/commands/logs.ts

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
} from '@aws-sdk/client-cloudformation'
55
import {
66
CloudWatchLogsClient,
7+
DeleteLogGroupCommand,
78
DescribeLogStreamsCommand,
89
GetLogEventsCommand,
910
} from '@aws-sdk/client-cloudwatch-logs'
@@ -29,18 +30,45 @@ export const logsCommand = ({
2930
flags: '-s, --numLogStreams <numLogStreams>',
3031
description: 'Number of logStreams to consider, default: 100',
3132
},
33+
{
34+
flags: '-f, --filter <filter>',
35+
description: 'string to filter for',
36+
},
37+
{
38+
flags: '-X, --deleteLogGroups',
39+
description: 'delete log groups afterwards',
40+
},
3241
],
33-
action: async ({ numLogGroups, numLogStreams }) => {
42+
action: async ({ numLogGroups, numLogStreams, filter, deleteLogGroups }) => {
3443
const logGroups =
3544
(
3645
await cf.send(
3746
new DescribeStackResourcesCommand({ StackName: stackName }),
3847
)
39-
).StackResources?.filter(
40-
({ ResourceType }) => ResourceType === 'AWS::Logs::LogGroup',
48+
).StackResources?.filter(({ ResourceType }) =>
49+
['AWS::Logs::LogGroup', 'Custom::LogRetention'].includes(
50+
ResourceType ?? '',
51+
),
4152
)?.map(({ PhysicalResourceId }) => PhysicalResourceId as string) ??
4253
([] as string[])
4354

55+
if (deleteLogGroups === true) {
56+
await Promise.all(
57+
logGroups.map(async (logGroupName) => {
58+
console.log(
59+
chalk.gray(`Deleting log group`),
60+
chalk.yellow(logGroupName),
61+
)
62+
return logs.send(
63+
new DeleteLogGroupCommand({
64+
logGroupName,
65+
}),
66+
)
67+
}),
68+
)
69+
return
70+
}
71+
4472
const streams = await Promise.all(
4573
logGroups.map(async (logGroupName) => {
4674
const { logStreams } = await logs.send(
@@ -85,8 +113,24 @@ export const logsCommand = ({
85113
({ message }) =>
86114
!/^(START|END|REPORT) RequestId:/.test(message ?? ''),
87115
)
88-
?.filter(({ message }) => message?.includes('ERROR'))
89-
?.forEach((e) => console.log(e.message?.trim()))
116+
?.filter(({ message }) =>
117+
filter === undefined ? true : message?.includes(filter),
118+
)
119+
?.forEach((e) => {
120+
try {
121+
const parts = (e.message?.trim() ?? '').split('\t')
122+
const message = parts.pop()
123+
const prettified = JSON.stringify(
124+
JSON.parse(message ?? ''),
125+
null,
126+
2,
127+
)
128+
console.log(chalk.gray(parts.join('\t')))
129+
console.log(prettified)
130+
} catch {
131+
console.log(chalk.gray(e.message?.trim()))
132+
}
133+
})
90134
})
91135
}),
92136
)

0 commit comments

Comments
 (0)