public void log(LogRecord record) {
    if (logToFile != null) {
      logToFile(record);
    }

    if (ignoreLogging) return;

    CapedwarfRequestLogs requestLogs = getCurrentRequestLogs();
    if (requestLogs != null) {
      CapedwarfAppLogLine capedwarfAppLogLine =
          new CapedwarfAppLogLine(getCurrentRequestId(), record.getSequenceNumber());
      AppLogLine appLogLine = capedwarfAppLogLine.getAppLogLine();
      appLogLine.setLogLevel(getLogLevel(record));
      appLogLine.setLogMessage(
          record.getSourceClassName()
              + " "
              + record.getSourceMethodName()
              + ": "
              + getFormattedMessage(record)
              + "\n");
      appLogLine.setTimeUsec(record.getMillis() * 1000);
      logWriter.put(capedwarfAppLogLine);

      requestLogs.logLineAdded(appLogLine);
      logWriter.put(requestLogs);
    }
  }
  @SuppressWarnings("unchecked")
  private CapedwarfLogQueryResult fetchCapedwarfRequestLogs(CapedwarfLogQuery logQuery) {
    List<CapedwarfRequestLogs> list;
    int resultCount;
    List<String> requestIds = logQuery.getQuery().getRequestIds();
    if (requestIds.isEmpty()) {
      CacheQuery cacheQuery = createRequestLogsQuery(logQuery);
      list = (List<CapedwarfRequestLogs>) (List) cacheQuery.list();
      resultCount = cacheQuery.getResultSize();
    } else {
      list = new ArrayList<>(requestIds.size());
      for (String requestId : requestIds) {
        CapedwarfRequestLogs requestLogs = (CapedwarfRequestLogs) store.get(requestId);
        if (requestLogs != null) {
          list.add(requestLogs);
        }
      }
      resultCount = list.size();
    }
    for (int i = 0; i < list.size(); i++) {
      CapedwarfRequestLogs capedwarfRequestLogs = list.get(i);
      list.set(i, capedwarfRequestLogs.clone());
    }

    return new CapedwarfLogQueryResult(list, resultCount);
  }
 @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;
  }