【问题标题】:How to export the Google App Engine logs logged via System.out.println()?如何导出通过 System.out.println() 记录的 Google App Engine 日志?
【发布时间】:2016-11-10 00:13:28
【问题描述】:
我在 Google App Engine 上部署了一个 Spring 应用程序,并且我通过 System.out.println() 放置了日志。
如何下载包含这些日志的文件?
命令,
appcfg request_logs path_to_war_file path_of_log_file
只给出请求及其各自的响应。
附:我已经设置了日志.level = INFO
【问题讨论】:
标签:
java
google-app-engine
logging
【解决方案1】:
Google App Engine 似乎没有提供下载日志文件的方法。
对于我自己的应用程序,我创建了一个用于发送日志的控制器。根据您的应用需求调整限制。
package edu.pratiksanglikar.cmpe281.controllers;
import org.joda.time.DateTime;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.google.appengine.api.log.AppLogLine;
import com.google.appengine.api.log.LogQuery;
import com.google.appengine.api.log.LogServiceFactory;
import com.google.appengine.api.log.RequestLogs;
@Controller
@RequestMapping(value = "/logs")
public class LogController {
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<String> getLogs() {
String allLogs = "";
// We use this to break out of our iteration loop, limiting record
// display to 5 request logs at a time.
int limit = 10000;
// This retrieves the offset from the Next link upon user click.
//String offset = req.getParameter("offset");
// We want the App logs for each request log
LogQuery query = LogQuery.Builder.withDefaults();
query.includeAppLogs(true);
// Set the offset value retrieved from the Next link click.
/*if (offset != null) {
query.offset(offset);
}*/
// This gets filled from the last request log in the iteration
int count = 0;
// Display a few properties of each request log.
for (RequestLogs record : LogServiceFactory.getLogService().fetch(query)) {
String log = "\n";
log += "REQUEST LOG\n";
DateTime reqTime = new DateTime(record.getStartTimeUsec() / 1000);
log += "IP: " + record.getIp() + "\n";
log += "Method: " + record.getMethod() + "\n";
log += "Resource " + record.getResource() + "\n";
log += String.format("\nDate: %s", reqTime.toString());
allLogs += log;
// Display all the app logs for each request log.
for (AppLogLine appLog : record.getAppLogLines()) {
String appLogLine = "\n";
appLogLine += "\t" + "APPLICATION LOG" + "\n";
DateTime appTime = new DateTime(appLog.getTimeUsec() / 1000);
appLogLine += String.format("\n\tDate: %s", appTime.toString());
appLogLine += "\n\tLevel: " + appLog.getLogLevel() + "";
appLogLine += "\n\tMessage: " + appLog.getLogMessage() + "\n";
allLogs += appLogLine;
}
if (++count >= limit) {
break;
}
}
// When the user clicks this link, the offset is processed in the
// GET handler and used to cycle through to the next 5 request logs.
return new ResponseEntity<String>(allLogs, HttpStatus.OK);
}
}