付延余
2022-12-16 f0f8ee8c4a945adbc742d9bab69382b28ad311fb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.wgcloud.controller;
 
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.wgcloud.entity.LogInfo;
import com.wgcloud.service.LogInfoService;
import com.wgcloud.util.PageUtil;
import com.wgcloud.util.ResDataUtils;
import com.wgcloud.util.TokenUtils;
import com.wgcloud.util.staticvar.StaticKeys;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
 
/**
 * @version v3.3
 * @ClassName:LogInfoController.java
 * @author: http://www.wgstart.com
 * @date: 2021年1月16日
 * @Description: 系统生成的日志管理
 * @Copyright: 2019-2021 wgcloud. All rights reserved.
 */
@Controller
@RequestMapping("/log")
public class LogInfoController {
 
 
    private static final Logger logger = LoggerFactory.getLogger(LogInfoController.class);
 
    @Resource
    private LogInfoService logInfoService;
    @Autowired
    private TokenUtils tokenUtils;
 
    /**
     * agent获取系统日志信息列表
     *
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "agentList")
    public String agentList(@RequestBody String paramBean) {
        JSONObject agentJsonObject = (JSONObject) JSONUtil.parse(paramBean);
        if (!tokenUtils.checkAgentToken(agentJsonObject)) {
            logger.error(StaticKeys.TOKEN_ERROR);
            return ResDataUtils.resetErrorJson(StaticKeys.TOKEN_ERROR);
        }
        Map<String, Object> params = new HashMap<String, Object>();
        try {
            if (null != agentJsonObject.get("hostname") && !StringUtils.isEmpty(agentJsonObject.get("hostname").toString())) {
                params.put("hostname", agentJsonObject.get("hostname").toString().trim());
            }
            if (null != agentJsonObject.get("startTime")) {
                params.put("startTime", agentJsonObject.get("startTime").toString().trim());
            }
            if (null != agentJsonObject.get("endTime")) {
                params.put("endTime", agentJsonObject.get("endTime").toString().trim());
            }
            if (null != agentJsonObject.get("state")) {
                params.put("state", agentJsonObject.get("state").toString().trim());
            }
            PageInfo logInfoList = logInfoService.selectByParams(params, agentJsonObject.getInt("page"), agentJsonObject.getInt("pageSize"));
            return ResDataUtils.resetSuccessJson(logInfoList);
        } catch (Exception e) {
            logger.error("agent获取系统日志信息列表错误", e);
            logInfoService.save("agent获取系统日志信息列表错误", e.toString(), StaticKeys.LOG_XTCZ);
            return ResDataUtils.resetErrorJson(e.toString());
        }
    }
 
    /**
     * 根据条件查询日志信息列表
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "list")
    public String LogInfoList(LogInfo logInfo, Model model, HttpServletRequest request) {
        Map<String, Object> params = new HashMap<String, Object>();
        try {
            StringBuffer url = new StringBuffer();
            String startTime = request.getParameter("startTime");
            String endTime = request.getParameter("endTime");
            String hostname = null;
            if (!StringUtils.isEmpty(logInfo.getHostname())) {
                hostname = logInfo.getHostname();
                params.put("hostname", hostname.trim());
                url.append("&hostname=").append(hostname);
            }
            if (!StringUtils.isEmpty(startTime)) {
                params.put("startTime", startTime + " 00:00:00");
                url.append("&startTime=").append(startTime);
                model.addAttribute("startTime", startTime);
            }
            if (!StringUtils.isEmpty(endTime)) {
                params.put("endTime", endTime + " 23:59:59");
                url.append("&endTime=").append(endTime);
                model.addAttribute("endTime", endTime);
            }
            if (!StringUtils.isEmpty(logInfo.getState())) {
                params.put("state", logInfo.getState());
                url.append("&state=").append(logInfo.getState());
            }
            PageInfo pageInfo = logInfoService.selectByParams(params, logInfo.getPage(), logInfo.getPageSize());
            PageUtil.initPageNumber(pageInfo, model);
 
            model.addAttribute("pageUrl", "/log/list?1=1" + url.toString());
            model.addAttribute("page", pageInfo);
            model.addAttribute("logInfo", logInfo);
        } catch (Exception e) {
            logger.error("查询日志列表错误", e);
        }
        return "log/list";
    }
 
    /**
     * 查看日志信息
     *
     * @param LogInfo
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "view")
    public String viewLogInfo(Model model, HttpServletRequest request) {
        String id = request.getParameter("id");
        LogInfo logInfo;
        try {
            logInfo = logInfoService.selectById(id);
            model.addAttribute("logInfo", logInfo);
        } catch (Exception e) {
            logger.error("查看日志信息错误", e);
        }
        return "log/view";
    }
 
}