@Override
 public CapedwarfLogQueryResult fetch(CapedwarfLogQuery logQuery) {
   CapedwarfLogQueryResult result = fetchCapedwarfRequestLogs(logQuery);
   for (CapedwarfRequestLogs capedwarfRequestLogs : result.getCapedwarfRequestLogs()) {
     if (logQuery.getQuery().getIncludeAppLogs()) {
       fetchAppLogLines(capedwarfRequestLogs.getRequestLogs(), logQuery);
     }
   }
   return result;
 }
 public void requestFinished(ServletRequest servletRequest, int status, int contentLength) {
   CapedwarfRequestLogs capedwarfRequestLogs = getCapedwarfRequestLogs(servletRequest);
   // check if all went well
   if (capedwarfRequestLogs != null) {
     RequestLogs requestLogs = capedwarfRequestLogs.getRequestLogs();
     requestLogs.setEndTimeUsec(System.currentTimeMillis() * 1000);
     requestLogs.setStatus(status);
     requestLogs.setResponseSize(contentLength);
     requestLogs.setFinished(true);
     logWriter.put(capedwarfRequestLogs);
   }
 }
  public void requestStarted(ServletRequest servletRequest, long startTimeMillis) {
    if (ignoreLogging || !isLoggable(servletRequest)) {
      return;
    }

    CapedwarfEnvironment environment = CapedwarfEnvironment.getThreadLocalInstance();
    CapedwarfRequestLogs capedwarfRequestLogs =
        createCapedwarfRequestLogs(servletRequest, startTimeMillis, environment);

    logWriter.put(capedwarfRequestLogs);
    servletRequest.setAttribute(REQUEST_LOGS_REQUEST_ATTRIBUTE, capedwarfRequestLogs);
    environment.getAttributes().put(REQUEST_LOGS_ENV_ATTRIBUTE, capedwarfRequestLogs);
    environment
        .getAttributes()
        .put(REQUEST_LOG_ID, capedwarfRequestLogs.getRequestLogs().getRequestId());
  }
 @Override
 public void put(CapedwarfRequestLogs requestLogs) {
   store.put(requestLogs.getRequestLogs().getRequestId(), requestLogs);
 }
  private CapedwarfRequestLogs createCapedwarfRequestLogs(
      ServletRequest servletRequest, long startTimeMillis, CapedwarfEnvironment environment) {
    long startTimeUsec = startTimeMillis * 1000;

    CapedwarfRequestLogs capedwarfRequestLogs = new CapedwarfRequestLogs();
    RequestLogs requestLogs = capedwarfRequestLogs.getRequestLogs();
    requestLogs.setRequestId(generateId());
    requestLogs.setStartTimeUsec(startTimeUsec);
    requestLogs.setEndTimeUsec(startTimeUsec);
    requestLogs.setFinished(false);

    requestLogs.setAppId(environment.getAppId());
    requestLogs.setModuleId(environment.getModuleId());
    requestLogs.setVersionId(environment.getVersionId());

    if (servletRequest instanceof HttpServletRequest) {
      HttpServletRequest request = (HttpServletRequest) servletRequest;
      String queryString = request.getQueryString();
      requestLogs.setResource(
          request.getRequestURI() + (queryString == null ? "" : ("?" + queryString)));
      requestLogs.setUserAgent(request.getHeader("User-Agent"));
      requestLogs.setMethod(request.getMethod());
      requestLogs.setReferrer(request.getHeader("referer"));
      requestLogs.setHost(
          request.getServerName()
              + (request.getServerPort() == 80 ? "" : (":" + request.getServerPort())));
    }

    requestLogs.setHttpVersion(servletRequest.getProtocol());

    requestLogs.setInstanceKey(""); // TODO
    requestLogs.setReplicaIndex(-1); // TODO

    requestLogs.setPendingTime(0); // TODO
    requestLogs.setLatency(0); // TODO

    //        requestLogs.setUrlMapEntry();

    //        requestLogs.setTaskName();
    //        requestLogs.setTaskQueueName();
    //        requestLogs.setWasLoadingRequest();

    requestLogs.setNickname("");
    requestLogs.setIp(servletRequest.getRemoteAddr());

    requestLogs.setCost(0);
    requestLogs.setMcycles(0);
    requestLogs.setApiMcycles(0);

    // combined='93.103.26.101 - - [17/Jan/2013:08:07:11 -0800] "GET /favicon.ico HTTP/1.1" 404 0 -
    // "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.17 (KHTML, like Gecko)
    // Chrome/24.0.1312.52 Safari/537.17"',
    requestLogs.setCombined(
        requestLogs.getIp()
            + " - "
            + requestLogs.getNickname()
            + (requestLogs.getNickname().isEmpty() ? "" : " ")
            + "- ["
            + DATE_FORMAT.format(requestLogs.getStartTimeUsec() / 1000L)
            + "] \""
            + requestLogs.getMethod()
            + " "
            + requestLogs.getResource()
            + " "
            + requestLogs.getHttpVersion()
            + "\" "
            + requestLogs.getStatus()
            + " "
            + requestLogs.getResponseSize()
            + " - \""
            + requestLogs.getUserAgent()
            + "\"");

    //        requestLogs.setOffset();  TODO
    return capedwarfRequestLogs;
  }