我想实现我的使用计划如下:创建日志组在AWS上 的CloudWatch上文创建上面日志组Put日志事件日志流 日志流
所有这些都用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)它得到解决。
SerializationException
logevents := make([]*cloudwatchlogs.InputLogEvent
logevents := make([]*cloudwatchlogs.InputLogEvent, 0)
另外,在调试查找未填充日志的原因 时,我发现所使用的时间戳值无效。根据AWS 文档,每个事件的时间戳不能超过14天 ,将来不能超过2小时。这是链接:https ://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html
https ://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html
希望对将来遇到类似问题的人有所帮助。