Exemple #1
0
  /**
   * 记录页面错误 错误日志记录
   * [page/eception][username][statusCode][errorMessage][servletName][uri][exceptionName][ip][exception]
   *
   * @param request http请求
   */
  public static void logPageError(HttpServletRequest request) {
    String username = getUsername();

    Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
    String message = (String) request.getAttribute("javax.servlet.error.message");
    String uri = (String) request.getAttribute("javax.servlet.error.request_uri");
    Throwable t = (Throwable) request.getAttribute("javax.servlet.error.exception");

    if (statusCode == null) {
      statusCode = 0;
    }

    StringBuilder s = new StringBuilder();
    s.append(getBlock(t == null ? "page" : "exception"));
    s.append(getBlock(username));
    s.append(getBlock(statusCode));
    s.append(getBlock(message));
    s.append(getBlock(IpUtils.getIpAddr(request)));

    s.append(getBlock(uri));
    s.append(getBlock(request.getHeader("Referer")));
    StringWriter sw = new StringWriter();

    while (t != null) {
      t.printStackTrace(new PrintWriter(sw));
      t = t.getCause();
    }
    s.append(getBlock(sw.toString()));
    getErrorLog().error(s.toString());
  }
Exemple #2
0
  /**
   * 记录访问日志 [username][jsessionid][ip][accept][UserAgent][url][params][Referer]
   *
   * @param request http请求
   */
  public static void logAccess(HttpServletRequest request) {
    String username = getUsername();
    String jsessionId = request.getRequestedSessionId();
    String ip = IpUtils.getIpAddr(request);
    String accept = request.getHeader("accept");
    String userAgent = request.getHeader("User-Agent");
    String url = request.getRequestURI();
    String params = getParams(request);
    String headers = getHeaders(request);

    StringBuilder s = new StringBuilder();
    s.append(getBlock(username));
    s.append(getBlock(jsessionId));
    s.append(getBlock(ip));
    s.append(getBlock(accept));
    s.append(getBlock(userAgent));
    s.append(getBlock(url));
    s.append(getBlock(params));
    s.append(getBlock(headers));
    s.append(getBlock(request.getHeader("Referer")));
    getAccessLog().info(s.toString());
  }
  @Override
  public String getLocation(String ip) {
    if (!IpUtils.isIPv4(ip)) {
      throw new IllegalArgumentException("Invalid ip address: " + ip);
    }

    HttpClient client = new HttpClient();
    String content = client.getContent(getUrl(ip));
    if (StringUtils.isBlank(content)) {
      LOGGER.warn("Location data is empty: {}", content);
      return null;
    }

    String location = getDataParser().parse(content);
    if (location != null) {
      location = location.trim();
    }

    if (this.ipLocationSaver != null) {
      this.ipLocationSaver.save(ip, location);
    }

    return location;
  }