我相信com.google.appengine.api.log.dev.LocalLogService是您正在寻找的那个,它具有与com.google.appengine.api.log.LogService不同的类签名的原因是 LocalLogService 只是一个期待或半生不熟的实施。这个类可能会在未来的版本中改变。
如果您查看LogService 源,则 fetch 方法返回形成为 Iterable<RequestLogs> 的日志记录。在 LocalLogService 中,它不是从实时应用服务器获取日志记录,而是简单地从内存中读取日志记录(形成为 LogReadResponse,通常是 Iterable<RequestLogs> 的包装器)。以下示例代码演示了它是如何工作的:
public void foo() {
String requestId = "1234";
localLogService = new LocalLogService();
// Mock a request log record:
localLogService.addRequestInfo("sample-app", "1", requestId, null, null, startTimeUsec, endTimeUsec,
"GET", "", "HTTP/1.1", null, true, 200, null);
// Mock an app log record:
localLogService.addAppLogLine(requestId, startTimeUsec, 2, "this is a sample log message.");
LogReadRequest request = new LogReadRequest();
// Filter request 1234:
request.addRequestId(requestId);
// Version id must match the one you just mocked, do not use dot in-between:
request.addVersionId("1");
request.setIncludeIncomplete(true);
request.setIncludeAppLogs(true);
Status status = new Status();
status.setSuccessful(true);
// Read mocked log records from memory:
LogReadResponse response = localLogService.read(status, request);
for (RequestLog log : response.logs()) {
System.out.println("request log: " + log.getCombined());
for (LogLine line : log.lines()) {
System.out.println("app log: " + line.getLogMessage());
}
}
}
样本输出:
请求日志:- - - [8/Feb/2013:09:51:13 -0800] "GET HTTP/1.1" 200 - - -
应用程序日志:这是一个示例日志消息。
为了更好的代码设计,您可以将 LocalLogService 包装到 LogService 的实现中,以使类签名更加一致。