|
@@ -0,0 +1,138 @@
|
|
|
+package com.xy.utils;
|
|
|
+
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import com.xy.config.SpringConfigs;
|
|
|
+import com.xy.enums.LogEnum;
|
|
|
+import com.xy.producer.LogProducer;
|
|
|
+import com.xy.producer.LogProducerConfiguration;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.net.InetAddress;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class LogUtils {
|
|
|
+
|
|
|
+ private static SpringConfigs springConfigs;
|
|
|
+
|
|
|
+ private static LogProducer logProducer;
|
|
|
+
|
|
|
+ @Autowired(required = false)
|
|
|
+ public void setSpringConfigs(SpringConfigs springConfigs) {
|
|
|
+ LogUtils.springConfigs = springConfigs;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired(required = false)
|
|
|
+ public void setMqttProducer(LogProducer logProducer) {
|
|
|
+ LogUtils.logProducer = logProducer;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 操作日志
|
|
|
+ *
|
|
|
+ * @param logType 日志类型
|
|
|
+ * @param optType 操作类型
|
|
|
+ * @param logContent 日志内容
|
|
|
+ * @param optTime 操作耗时
|
|
|
+ */
|
|
|
+ public static void operate(LogEnum.LogType logType, LogEnum.OptType optType, String logContent, int optTime) {
|
|
|
+ JSONObject jsonObject = new JSONObject()
|
|
|
+ .set("logType", logType.getValue())
|
|
|
+ .set("logContent", logContent)
|
|
|
+ .set("optType", optType.getValue())
|
|
|
+ .set("createTime", DataTime.getSring())
|
|
|
+ .set("optTime", optTime)
|
|
|
+ .set("clientType", AuthorizeUtils.getClientType())
|
|
|
+ .set("sysId", AuthorizeUtils.getSysId())
|
|
|
+ .set("optUserId", AuthorizeUtils.getLoginId(Long.class))
|
|
|
+ .set("clientIp", getIp())
|
|
|
+ .set("serviceName", springConfigs.getApplication().getName())
|
|
|
+ .set("type", "operate");
|
|
|
+ logProducer.sendToMqtt(jsonObject.toString(), LogProducerConfiguration.TOPIC, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 系统事件
|
|
|
+ *
|
|
|
+ * @param eventName 事件名称
|
|
|
+ * @param eventLevel 事件等级
|
|
|
+ * @param eventDescript 事件描述
|
|
|
+ */
|
|
|
+ public static void envents(String eventName, LogEnum.EventLevel eventLevel, String eventDescript) {
|
|
|
+ JSONObject jsonObject = new JSONObject()
|
|
|
+ .set("eventName", eventName)
|
|
|
+ .set("eventLevel", eventLevel.getValue())
|
|
|
+ .set("eventDescript", eventDescript)
|
|
|
+ .set("serviceName", springConfigs.getApplication().getName())
|
|
|
+ .set("createTime", DataTime.getSring())
|
|
|
+ .set("type", "envents");
|
|
|
+ logProducer.sendToMqtt(jsonObject.toString(), LogProducerConfiguration.TOPIC, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 定时任务日志
|
|
|
+ *
|
|
|
+ * @param taskType 定时任务类型,中文
|
|
|
+ * @param taskTitle 任务标题
|
|
|
+ * @param taskEvent 任务事件,中文
|
|
|
+ * @param taskDescript 事件描述
|
|
|
+ */
|
|
|
+ public static void tasks(String taskType, String taskTitle, String taskEvent, String taskDescript) {
|
|
|
+ JSONObject jsonObject = new JSONObject()
|
|
|
+ .set("taskType", taskType)
|
|
|
+ .set("taskTitle", taskTitle)
|
|
|
+ .set("taskEvent", taskEvent)
|
|
|
+ .set("taskDescript", taskDescript)
|
|
|
+ .set("serviceName", springConfigs.getApplication().getName())
|
|
|
+ .set("createTime", DataTime.getSring())
|
|
|
+ .set("type", "tasks");
|
|
|
+ logProducer.sendToMqtt(jsonObject.toString(), LogProducerConfiguration.TOPIC, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取ip
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static String getIp() {
|
|
|
+ HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
|
|
+ String ipAddress;
|
|
|
+ try {
|
|
|
+ ipAddress = request.getHeader("x-forwarded-for");
|
|
|
+ if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
|
|
+ ipAddress = request.getHeader("Proxy-Client-IP");
|
|
|
+ }
|
|
|
+ if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
|
|
+ ipAddress = request.getHeader("WL-Proxy-Client-IP");
|
|
|
+ }
|
|
|
+ if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
|
|
+ ipAddress = request.getRemoteAddr();
|
|
|
+ if (ipAddress.equals("127.0.0.1")) {
|
|
|
+ // 根据网卡取本机配置的IP
|
|
|
+ InetAddress inet = null;
|
|
|
+ try {
|
|
|
+ inet = InetAddress.getLocalHost();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ ipAddress = inet.getHostAddress();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
|
|
|
+ if (ipAddress != null && ipAddress.length() > 15) {
|
|
|
+ // "***.***.***.***".length()
|
|
|
+ // = 15
|
|
|
+ if (ipAddress.indexOf(",") > 0) {
|
|
|
+ ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ ipAddress = "";
|
|
|
+ }
|
|
|
+ return ipAddress;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|