我正在尝试使用我的程序实现以下目的:在AWS CloudWatch上创建日志组在上述日志组下创建日志流在上述日志流下放置日志事件
所有这些都用go lang
package main import ( "time" "fmt" "github.com/jcxplorer/cwlogger" "github.com/aws/aws-sdk-go/service/cloudwatchlogs" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/aws" ) func main() { sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) svc := cloudwatchlogs.New(sess) logGroupName := "my-log-group"; logStreamName := "my-log-stream"; logGroupInput := cloudwatchlogs.CreateLogGroupInput{LogGroupName: &logGroupName} svc.CreateLogGroup(&logGroupInput); logStreamInput := cloudwatchlogs.CreateLogStreamInput{LogGroupName: &logGroupName, LogStreamName: &logStreamName} svc.CreateLogStream(&logStreamInput) logevents := make([]*cloudwatchlogs.InputLogEvent, 1) logevents = append(logevents, &cloudwatchlogs.InputLogEvent{ Message: aws.String("Simple log message"), Timestamp: aws.Int64(111), }) p := cloudwatchlogs.PutLogEventsInput{LogEvents: logevents, LogGroupName: &logGroupName, LogStreamName: &logStreamName} resp, err := svc.PutLogEvents(&p) if err != nil { panic(err) } fmt.Print("Next Token: {}", resp.NextSequenceToken) }
现在,当我运行上述代码时,它成功创建了日志组和日志流,并且可以在AWS CloudWatch中进行验证。但是由于某些原因,PutLogEvents失败并显示以下错误:
panic: SerializationException: status code: 400, request id: 0685efcc-47e3-11e9-b528-81f33ec2f468
我不确定这里可能出什么问题。任何建议或指示将非常有帮助。
提前致谢。
出现SerializationException的原因是:logevents := make([]*cloudwatchlogs.InputLogEvent, 1) 之后是append,它在slice中创建了第一个空条目。我用替换了代码, logevents := make([]*cloudwatchlogs.InputLogEvent, 0)它得到解决。
logevents := make([]*cloudwatchlogs.InputLogEvent, 1)
logevents := make([]*cloudwatchlogs.InputLogEvent, 0)
另外,在调试查找未填充日志的原因时,我发现所使用的时间戳值无效。根据AWS文档,每个事件的时间戳不能超过14天,并且将来不能超过2小时。这是链接:https : //docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html
希望对将来遇到类似问题的人有所帮助。