2016-11-10 44 views

回答

0

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); 
    } 
    } 
相关问题