4
4
} from '@aws-sdk/client-cloudformation'
5
5
import {
6
6
CloudWatchLogsClient ,
7
+ DeleteLogGroupCommand ,
7
8
DescribeLogStreamsCommand ,
8
9
GetLogEventsCommand ,
9
10
} from '@aws-sdk/client-cloudwatch-logs'
@@ -29,18 +30,45 @@ export const logsCommand = ({
29
30
flags : '-s, --numLogStreams <numLogStreams>' ,
30
31
description : 'Number of logStreams to consider, default: 100' ,
31
32
} ,
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
+ } ,
32
41
] ,
33
- action : async ( { numLogGroups, numLogStreams } ) => {
42
+ action : async ( { numLogGroups, numLogStreams, filter , deleteLogGroups } ) => {
34
43
const logGroups =
35
44
(
36
45
await cf . send (
37
46
new DescribeStackResourcesCommand ( { StackName : stackName } ) ,
38
47
)
39
- ) . StackResources ?. filter (
40
- ( { ResourceType } ) => ResourceType === 'AWS::Logs::LogGroup' ,
48
+ ) . StackResources ?. filter ( ( { ResourceType } ) =>
49
+ [ 'AWS::Logs::LogGroup' , 'Custom::LogRetention' ] . includes (
50
+ ResourceType ?? '' ,
51
+ ) ,
41
52
) ?. map ( ( { PhysicalResourceId } ) => PhysicalResourceId as string ) ??
42
53
( [ ] as string [ ] )
43
54
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
+
44
72
const streams = await Promise . all (
45
73
logGroups . map ( async ( logGroupName ) => {
46
74
const { logStreams } = await logs . send (
@@ -85,8 +113,24 @@ export const logsCommand = ({
85
113
( { message } ) =>
86
114
! / ^ ( S T A R T | E N D | R E P O R T ) R e q u e s t I d : / . test ( message ?? '' ) ,
87
115
)
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
+ } )
90
134
} )
91
135
} ) ,
92
136
)
0 commit comments