PageRenderTime 65ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/common/src/main/java/com/sishuok/es/common/utils/LogUtils.java

https://gitlab.com/0072016/es
Java | 150 lines | 104 code | 27 blank | 19 comment | 7 complexity | 2188633bc5a26c4cba26cfeaab20d1b1 MD5 | raw file
  1. package com.sishuok.es.common.utils;
  2. import com.alibaba.fastjson.JSON;
  3. import com.google.common.collect.Lists;
  4. import com.google.common.collect.Maps;
  5. import org.apache.shiro.SecurityUtils;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import javax.servlet.http.HttpServletRequest;
  9. import java.io.PrintWriter;
  10. import java.io.StringWriter;
  11. import java.util.Enumeration;
  12. import java.util.List;
  13. import java.util.Map;
  14. public class LogUtils {
  15. public static final Logger ERROR_LOG = LoggerFactory.getLogger("es-error");
  16. public static final Logger ACCESS_LOG = LoggerFactory.getLogger("es-access");
  17. /**
  18. * 记录访问日志
  19. * [username][jsessionid][ip][accept][UserAgent][url][params][Referer]
  20. *
  21. * @param request
  22. */
  23. public static void logAccess(HttpServletRequest request) {
  24. String username = getUsername();
  25. String jsessionId = request.getRequestedSessionId();
  26. String ip = IpUtils.getIpAddr(request);
  27. String accept = request.getHeader("accept");
  28. String userAgent = request.getHeader("User-Agent");
  29. String url = request.getRequestURI();
  30. String params = getParams(request);
  31. String headers = getHeaders(request);
  32. StringBuilder s = new StringBuilder();
  33. s.append(getBlock(username));
  34. s.append(getBlock(jsessionId));
  35. s.append(getBlock(ip));
  36. s.append(getBlock(accept));
  37. s.append(getBlock(userAgent));
  38. s.append(getBlock(url));
  39. s.append(getBlock(params));
  40. s.append(getBlock(headers));
  41. s.append(getBlock(request.getHeader("Referer")));
  42. getAccessLog().info(s.toString());
  43. }
  44. /**
  45. * 记录异常错误
  46. * 格式 [exception]
  47. *
  48. * @param message
  49. * @param e
  50. */
  51. public static void logError(String message, Throwable e) {
  52. String username = getUsername();
  53. StringBuilder s = new StringBuilder();
  54. s.append(getBlock("exception"));
  55. s.append(getBlock(username));
  56. s.append(getBlock(message));
  57. ERROR_LOG.error(s.toString(), e);
  58. }
  59. /**
  60. * 记录页面错误
  61. * 错误日志记录 [page/eception][username][statusCode][errorMessage][servletName][uri][exceptionName][ip][exception]
  62. *
  63. * @param request
  64. */
  65. public static void logPageError(HttpServletRequest request) {
  66. String username = getUsername();
  67. Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
  68. String message = (String) request.getAttribute("javax.servlet.error.message");
  69. String uri = (String) request.getAttribute("javax.servlet.error.request_uri");
  70. Throwable t = (Throwable) request.getAttribute("javax.servlet.error.exception");
  71. if (statusCode == null) {
  72. statusCode = 0;
  73. }
  74. StringBuilder s = new StringBuilder();
  75. s.append(getBlock(t == null ? "page" : "exception"));
  76. s.append(getBlock(username));
  77. s.append(getBlock(statusCode));
  78. s.append(getBlock(message));
  79. s.append(getBlock(IpUtils.getIpAddr(request)));
  80. s.append(getBlock(uri));
  81. s.append(getBlock(request.getHeader("Referer")));
  82. StringWriter sw = new StringWriter();
  83. while (t != null) {
  84. t.printStackTrace(new PrintWriter(sw));
  85. t = t.getCause();
  86. }
  87. s.append(getBlock(sw.toString()));
  88. getErrorLog().error(s.toString());
  89. }
  90. public static String getBlock(Object msg) {
  91. if (msg == null) {
  92. msg = "";
  93. }
  94. return "[" + msg.toString() + "]";
  95. }
  96. protected static String getParams(HttpServletRequest request) {
  97. Map<String, String[]> params = request.getParameterMap();
  98. return JSON.toJSONString(params);
  99. }
  100. private static String getHeaders(HttpServletRequest request) {
  101. Map<String, List<String>> headers = Maps.newHashMap();
  102. Enumeration<String> namesEnumeration = request.getHeaderNames();
  103. while(namesEnumeration.hasMoreElements()) {
  104. String name = namesEnumeration.nextElement();
  105. Enumeration<String> valueEnumeration = request.getHeaders(name);
  106. List<String> values = Lists.newArrayList();
  107. while(valueEnumeration.hasMoreElements()) {
  108. values.add(valueEnumeration.nextElement());
  109. }
  110. headers.put(name, values);
  111. }
  112. return JSON.toJSONString(headers);
  113. }
  114. protected static String getUsername() {
  115. return (String) SecurityUtils.getSubject().getPrincipal();
  116. }
  117. public static Logger getAccessLog() {
  118. return ACCESS_LOG;
  119. }
  120. public static Logger getErrorLog() {
  121. return ERROR_LOG;
  122. }
  123. }