LogUtils.java 5.8 KB

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