GetLogEvents.go - AWS Code Sample


/* Copyright, Inc. or its affiliates. All Rights Reserved. This file is licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package main import ( "flag" "fmt" "" "" ) // 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) { svc := cloudwatchlogs.New(sess) resp, err := svc.GetLogEvents(&cloudwatchlogs.GetLogEventsInput{ Limit: limit, LogGroupName: logGroupName, LogStreamName: logStreamName, }) if err != nil { return nil, err } return resp, nil } func main() { 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 } sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) resp, err := GetLogEvents(sess, limit, logGroupName, logStreamName) if err != nil { fmt.Println("Got error getting log events:") fmt.Println(err) return } 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) } }