|
@@ -1,12 +1,17 @@
|
|
|
package com.ruoyi.project.monitor.controller;
|
|
|
|
|
|
+import ch.qos.logback.classic.LoggerContext;
|
|
|
+import ch.qos.logback.core.util.StatusPrinter;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
import com.ruoyi.common.utils.file.FileUtils;
|
|
|
import com.ruoyi.framework.web.controller.BaseController;
|
|
|
import com.ruoyi.framework.web.domain.AjaxResult;
|
|
|
import com.ruoyi.project.monitor.domain.SysLog;
|
|
|
+import com.ruoyi.project.system.service.ISysConfigService;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.w3c.dom.Document;
|
|
|
import org.w3c.dom.NamedNodeMap;
|
|
@@ -19,6 +24,8 @@ import javax.xml.parsers.DocumentBuilder;
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
import java.io.*;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Comparator;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
@@ -33,7 +40,8 @@ import java.util.List;
|
|
|
public class SysLogController extends BaseController {
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
-
|
|
|
+ @Autowired
|
|
|
+ private ISysConfigService configService;
|
|
|
/**
|
|
|
* 查询操作日志列表
|
|
|
*
|
|
@@ -42,58 +50,35 @@ public class SysLogController extends BaseController {
|
|
|
@GetMapping("/list")
|
|
|
public AjaxResult list()
|
|
|
{
|
|
|
+ // 获取log.path的值
|
|
|
String filePath = null;
|
|
|
- //1、创建一个DocumentBuilderFactory的对象
|
|
|
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
|
|
- //2、创建一个DocumentBuilder的对象
|
|
|
- try {
|
|
|
- //创建DocumentBuilder对象
|
|
|
- DocumentBuilder db = dbf.newDocumentBuilder();
|
|
|
- //3、通过DocumentBuilder对象的parser方法加载books.xml文件到当前项目下
|
|
|
- /*注意导入Document对象时,要导入org.w3c.dom.Document包下的*/
|
|
|
- Document document = db.parse("master/src/main/resources/logback.xml");//传入文件名可以是相对路径也可以是绝对路径
|
|
|
- //获取所有property节点的集合
|
|
|
- NodeList nodeList = document.getElementsByTagName("property");
|
|
|
- //遍历每一个nodelist节点
|
|
|
- for (int i = 0; i < nodeList.getLength(); i++) {
|
|
|
- //未知节点属性的个数和属性名时:
|
|
|
- //通过 item(i)方法 获取一个节点,nodelist的索引值从0开始
|
|
|
- Node node = nodeList.item(i);
|
|
|
- //获取节点的所有属性集合
|
|
|
- NamedNodeMap attrs = node.getAttributes();
|
|
|
- //遍历节点的属性
|
|
|
- for (int j = 0; j < attrs.getLength(); j++) {
|
|
|
- //通过item(index)方法获取节点的某一个属性
|
|
|
- Node attr = attrs.item(j);
|
|
|
- NodeList childNodes = attr.getChildNodes();
|
|
|
- int indexI = 0;
|
|
|
- int indexJ = 0;
|
|
|
- int indexK = 0;
|
|
|
- for (int k = 0; k < childNodes.getLength(); k++) {
|
|
|
- Node item = childNodes.item(k);
|
|
|
- String nodeValue = item.getNodeValue();
|
|
|
- if (nodeValue.equals("log.path")) {
|
|
|
- indexI = i;
|
|
|
- indexJ = j;
|
|
|
- indexK = k;
|
|
|
- }
|
|
|
- if (i == indexI && j == (indexJ + 1) && k == indexK) {
|
|
|
- filePath = nodeValue;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- System.out.println(filePath);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+
|
|
|
+ filePath = configService.selectConfigByKey("log.path");
|
|
|
+ if (StringUtils.isEmpty(filePath)) {
|
|
|
+ filePath = "/u03/logs";
|
|
|
}
|
|
|
+ logger.info(filePath);
|
|
|
File file = new File(filePath);//父级文件夹
|
|
|
String[] fileNameList = file.list();//文件名列表
|
|
|
+ File[] fileList = file.listFiles();
|
|
|
+ // 使用Comparator对文件列表按照修改日期进行排序
|
|
|
+ Arrays.sort(fileList, new Comparator<File>() {
|
|
|
+ @Override
|
|
|
+ public int compare(File file1, File file2) {
|
|
|
+ long lastModified1 = file1.lastModified();
|
|
|
+ long lastModified2 = file2.lastModified();
|
|
|
+ return Long.compare(lastModified2, lastModified1);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ // 获取前200条文件(如果文件列表超过200条)
|
|
|
+ int limit = Math.min(fileList.length, 200);
|
|
|
+ File[] topFiles = Arrays.copyOf(fileList, limit);
|
|
|
+
|
|
|
List<SysLog> logList = new ArrayList<>();//日志信息列表
|
|
|
- for (String fileName : fileNameList) {
|
|
|
+ for (File f : topFiles) {
|
|
|
SysLog sysLog = new SysLog();
|
|
|
- sysLog.setFileName(fileName);//文件名
|
|
|
- sysLog.setFileUrl(file.getAbsolutePath() + "\\" + fileName);//绝对路径
|
|
|
+ sysLog.setFileName(f.getName());//文件名
|
|
|
+ sysLog.setFileUrl(f.getAbsolutePath());//绝对路径
|
|
|
logList.add(sysLog);
|
|
|
}
|
|
|
return AjaxResult.success(logList);
|