forked from awsdocs/aws-doc-sdk-examples
/
GetLogEvents.go
91 lines (75 loc) · 2.65 KB
/
GetLogEvents.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[cloudwatch.go.getlogevents]
package main
// snippet-start:[cloudwatch.go.getlogevents.imports]
import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
)
// snippet-end:[cloudwatch.go.getlogevents.imports]
// GetLogEvents retrieves CloudWatchLog events.
// Inputs:
//
// sess is the current session, which provides configuration for the SDK's service clients
// limit is the maximum number of log events to retrieve
// logGroupName is the name of the log group
// logStreamName is the name of the log stream
//
// Output:
//
// If success, a GetLogEventsOutput object containing the events and nil
// Otherwise, nil and an error from the call to GetLogEvents
func GetLogEvents(sess *session.Session, limit *int64, logGroupName *string, logStreamName *string) (*cloudwatchlogs.GetLogEventsOutput, error) {
// snippet-start:[cloudwatch.go.getlogevents.call]
svc := cloudwatchlogs.New(sess)
resp, err := svc.GetLogEvents(&cloudwatchlogs.GetLogEventsInput{
Limit: limit,
LogGroupName: logGroupName,
LogStreamName: logStreamName,
})
// snippet-end:[cloudwatch.go.getlogevents.call]
if err != nil {
return nil, err
}
return resp, nil
}
func main() {
// snippet-start:[cloudwatch.go.getlogevents.args]
limit := flag.Int64("l", 100, "The maximum number of events to retrieve")
logGroupName := flag.String("g", "", "The name of the log group")
logStreamName := flag.String("s", "", "The name of the log stream")
flag.Parse()
if *logGroupName == "" || *logStreamName == "" {
fmt.Println("You must supply a log group name (-g LOG-GROUP) and log stream name (-s LOG-STREAM)")
return
}
// snippet-end:[cloudwatch.go.getlogevents.args]
// snippet-start:[cloudwatch.go.getlogevents.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// snippet-end:[cloudwatch.go.getlogevents.session]
resp, err := GetLogEvents(sess, limit, logGroupName, logStreamName)
if err != nil {
fmt.Println("Got error getting log events:")
fmt.Println(err)
return
}
// snippet-start:[cloudwatch.go.getlogevents.print]
fmt.Println("Event messages for stream " + *logStreamName + " in log group " + *logGroupName)
gotToken := ""
nextToken := ""
for _, event := range resp.Events {
gotToken = nextToken
nextToken = *resp.NextForwardToken
if gotToken == nextToken {
break
}
fmt.Println(" ", *event.Message)
// snippet-end:[cloudwatch.go.getlogevents.print]
}
}
// snippet-end:[cloudwatch.go.getlogevents]