我正在为使用Amazon Cloud Watch服务的应用程序构建一个小型日志跟踪器。这个想法不是跟踪文件的日志输出,而是使用aws控制台的搜索引擎来查找日志信息。Amazon Cloud Watch日志 - PutLogEventsRequest - 给定的sequenceToken无效
我使用:
- Eclipse作为IDE
- 的Java 8个
- 依赖关系:AWS-Java的SDK核心/ AWS-Java的SDK-CloudWatch的V 49年1月10日
在另一方面我有以下AWS配置:
- 甲CCESS和私钥
- 地区:加州
- 日志组:demo1的
- 日志流:流1
我在写下面的代码做一个简单的功能测试:
package com.test.pe.cloudwatch;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.logs.AWSLogsClient;
import com.amazonaws.services.logs.model.InputLogEvent;
import com.amazonaws.services.logs.model.PutLogEventsRequest;
import com.amazonaws.services.logs.model.PutLogEventsResult;
import com.test.pe.base.CredentialBuilder;
public class RegisterLog {
private static String LOG_GROUP = "demo1";
private static String LOG_STREAM = "stream1";
public static void main(String[] args) throws ParseException {
// building my credential and calendar instances
AWSCredentials credential = CredentialBuilder.getCredential();
Calendar calendar = Calendar.getInstance();
// building a cloud watch log client
AWSLogsClient cloudWatchlog = new AWSLogsClient(credential);
cloudWatchlog.setRegion(Region.getRegion(Regions.US_WEST_1));
// building a put request log
PutLogEventsRequest request = new PutLogEventsRequest();
request.setLogGroupName(LOG_GROUP);
request.setLogStreamName(LOG_STREAM);
// building my log event
InputLogEvent log = new InputLogEvent();
log.setMessage("Some message for a test");
log.setTimestamp(calendar.getTimeInMillis());
// building the array list log event
ArrayList<InputLogEvent> logEvents = new ArrayList<InputLogEvent>();
logEvents.add(log);
// setting the error array list
request.setLogEvents(logEvents);
// make the request
cloudWatchlog.putLogEvents(request);
System.out.println("done!");
}
}
当我第一次运行代码时,所有的都是okey,消息被成功保存。
然而,当我执行的代码,第二次我得到以下异常:
Exception in thread "main" com.amazonaws.services.logs.model.InvalidSequenceTokenException: The given sequenceToken is invalid. The next expected sequenceToken is: xxxxxxxxxxxxxxxxxxxxxxxxxxx (Service: AWSLogs; Status Code: 400; Error Code: InvalidSequenceTokenException; Request ID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)
at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1389)
XXXXXXXXXXX:是由亚马逊生成的令牌代码。
阅读亚马逊的文档,我发现了以下信息:
请求语法:
{
"LogEvents": [
{
"Message": "string",
"Timestamp": number
}
],
"LogGroupName": "string",
"LogStreamName": "string",
"SequenceToken": "string"
}
SequenceToken
A string token that must be obtained from the response of the previous PutLogEvents request.
Type: String
Length constraints: Minimum length of 1.
Required: No
amazon documentation about cloud watch log REST API
和我决定进行硬编码序列标记在我的代码如下:
request.setSequenceToken("58523.......");
它工作正常。我只是为了测试而做的。
最后,我发现获得序列标记的唯一方法是。
PutLogEventsResult response = cloudWatchlog.putLogEvents(request);
String token = response.getNextSequenceToken();
我如何可以验证并获得序列码提出请求之前?我无法在文档中找到它。
这也是一个好方法 – afym