123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- package com.xy.utils;
- import cn.hutool.json.JSONObject;
- import com.xy.config.SpringConfigs;
- import com.xy.dto.MqttDto;
- import com.xy.entity.SysCodeConfigureRedis;
- import com.xy.enums.LogEnum;
- import com.xy.service.SysMqttSendService;
- 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 SysMqttSendService sysMqttSendService;
- @Autowired(required = false)
- public void setSpringConfigs(SpringConfigs springConfigs) {
- LogUtils.springConfigs = springConfigs;
- }
- @Autowired(required = false)
- public void setSysMqttSendService(SysMqttSendService sysMqttSendService) {
- LogUtils.sysMqttSendService = sysMqttSendService;
- }
- /**
- * 操作日志
- *
- * @param logType 日志类型
- * @param optType 操作类型
- * @param logContent 日志内容
- * @param optTime 操作耗时
- */
- public static void operate(LogEnum.LogType logType, LogEnum.OptType optType, String logContent, int optTime) {
- Long loginId = null;
- try {
- loginId = AuthorizeUtils.getLoginId(Long.class);
- } catch (Exception e) {
- }
- Long sysId = null;
- try {
- sysId = AuthorizeUtils.getSysId();
- } catch (Exception e) {
- }
- 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", sysId)
- .set("optUserId", loginId)
- .set("clientIp", getIp())
- .set("serviceName", springConfigs.getApplication().getName())
- .set("type", "operate");
- sysMqttSendService.log(new MqttDto.RequestParams().setQos(0).setData(jsonObject.toString()));
- }
- /**
- * 系统事件
- *
- * @param code 事件编码
- * @param eventName 事件名称
- * @param eventDescript 事件描述
- */
- public static void events(String code, String eventName, String eventDescript) {
- SysCodeConfigureRedis sysCodeConfigureRedis = SysCodeConfigureUtils.get(code);
- Integer port = SpringBeanUtils.getBean("getPort", Integer.class);
- JSONObject jsonObject = new JSONObject()
- .set("code", code)
- .set("port", port)
- .set("eventName", eventName)
- .set("eventLevel", sysCodeConfigureRedis.getExpand())
- .set("eventDescript", eventDescript)
- .set("serviceName", springConfigs.getApplication().getName())
- .set("createTime", DataTime.getSring())
- .set("type", "events");
- sysMqttSendService.log(new MqttDto.RequestParams().setQos(0).setData(jsonObject.toString()));
- }
- /**
- * 定时任务日志
- *
- * @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");
- sysMqttSendService.log(new MqttDto.RequestParams().setQos(0).setData(jsonObject.toString()));
- }
- /**
- * 获取ip
- *
- * @return
- */
- public 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;
- }
- }
|