LogUtils.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package com.xy.utils;
  2. import cn.hutool.json.JSONObject;
  3. import com.xy.config.SpringConfigs;
  4. import com.xy.enums.LogEnum;
  5. import com.xy.producer.LogProducer;
  6. import com.xy.producer.LogProducerConfiguration;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Component;
  9. import org.springframework.web.context.request.RequestContextHolder;
  10. import org.springframework.web.context.request.ServletRequestAttributes;
  11. import javax.servlet.http.HttpServletRequest;
  12. import java.net.InetAddress;
  13. @Component
  14. public class LogUtils {
  15. private static SpringConfigs springConfigs;
  16. private static LogProducer logProducer;
  17. @Autowired(required = false)
  18. public void setSpringConfigs(SpringConfigs springConfigs) {
  19. LogUtils.springConfigs = springConfigs;
  20. }
  21. @Autowired(required = false)
  22. public void setMqttProducer(LogProducer logProducer) {
  23. LogUtils.logProducer = logProducer;
  24. }
  25. /**
  26. * 操作日志
  27. *
  28. * @param logType 日志类型
  29. * @param optType 操作类型
  30. * @param logContent 日志内容
  31. * @param optTime 操作耗时
  32. */
  33. public static void operate(LogEnum.LogType logType, LogEnum.OptType optType, String logContent, int optTime) {
  34. JSONObject jsonObject = new JSONObject()
  35. .set("logType", logType.getValue())
  36. .set("logContent", logContent)
  37. .set("optType", optType.getValue())
  38. .set("createTime", DataTime.getSring())
  39. .set("optTime", optTime)
  40. .set("clientType", AuthorizeUtils.getClientType())
  41. .set("sysId", AuthorizeUtils.getSysId())
  42. .set("optUserId", AuthorizeUtils.getLoginId(Long.class))
  43. .set("clientIp", getIp())
  44. .set("serviceName", springConfigs.getApplication().getName())
  45. .set("type", "operate");
  46. logProducer.sendToMqtt(jsonObject.toString(), LogProducerConfiguration.TOPIC, 0);
  47. }
  48. /**
  49. * 系统事件
  50. *
  51. * @param eventName 事件名称
  52. * @param eventLevel 事件等级
  53. * @param eventDescript 事件描述
  54. */
  55. public static void envents(String eventName, LogEnum.EventLevel eventLevel, String eventDescript) {
  56. JSONObject jsonObject = new JSONObject()
  57. .set("eventName", eventName)
  58. .set("eventLevel", eventLevel.getValue())
  59. .set("eventDescript", eventDescript)
  60. .set("serviceName", springConfigs.getApplication().getName())
  61. .set("createTime", DataTime.getSring())
  62. .set("type", "envents");
  63. logProducer.sendToMqtt(jsonObject.toString(), LogProducerConfiguration.TOPIC, 0);
  64. }
  65. /**
  66. * 定时任务日志
  67. *
  68. * @param taskType 定时任务类型,中文
  69. * @param taskTitle 任务标题
  70. * @param taskEvent 任务事件,中文
  71. * @param taskDescript 事件描述
  72. */
  73. public static void tasks(String taskType, String taskTitle, String taskEvent, String taskDescript) {
  74. JSONObject jsonObject = new JSONObject()
  75. .set("taskType", taskType)
  76. .set("taskTitle", taskTitle)
  77. .set("taskEvent", taskEvent)
  78. .set("taskDescript", taskDescript)
  79. .set("serviceName", springConfigs.getApplication().getName())
  80. .set("createTime", DataTime.getSring())
  81. .set("type", "tasks");
  82. logProducer.sendToMqtt(jsonObject.toString(), LogProducerConfiguration.TOPIC, 0);
  83. }
  84. /**
  85. * 获取ip
  86. *
  87. * @return
  88. */
  89. private static String getIp() {
  90. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  91. String ipAddress;
  92. try {
  93. ipAddress = request.getHeader("x-forwarded-for");
  94. if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
  95. ipAddress = request.getHeader("Proxy-Client-IP");
  96. }
  97. if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
  98. ipAddress = request.getHeader("WL-Proxy-Client-IP");
  99. }
  100. if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
  101. ipAddress = request.getRemoteAddr();
  102. if (ipAddress.equals("127.0.0.1")) {
  103. // 根据网卡取本机配置的IP
  104. InetAddress inet = null;
  105. try {
  106. inet = InetAddress.getLocalHost();
  107. } catch (Exception e) {
  108. e.printStackTrace();
  109. }
  110. ipAddress = inet.getHostAddress();
  111. }
  112. }
  113. // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
  114. if (ipAddress != null && ipAddress.length() > 15) {
  115. // "***.***.***.***".length()
  116. // = 15
  117. if (ipAddress.indexOf(",") > 0) {
  118. ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
  119. }
  120. }
  121. } catch (Exception e) {
  122. ipAddress = "";
  123. }
  124. return ipAddress;
  125. }
  126. }