|
@@ -0,0 +1,92 @@
|
|
|
+package com.ruoyi.project.monitor.controller;
|
|
|
+
|
|
|
+import com.ruoyi.framework.web.controller.BaseController;
|
|
|
+import com.ruoyi.framework.web.domain.AjaxResult;
|
|
|
+import com.ruoyi.project.monitor.domain.SysLog;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.w3c.dom.Document;
|
|
|
+import org.w3c.dom.NamedNodeMap;
|
|
|
+import org.w3c.dom.Node;
|
|
|
+import org.w3c.dom.NodeList;
|
|
|
+
|
|
|
+import javax.xml.parsers.DocumentBuilder;
|
|
|
+import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
+import java.io.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 操作日志记录
|
|
|
+ *
|
|
|
+ * @author Wang Zi Wen
|
|
|
+ * @email wangggziwen@163.com
|
|
|
+ * @date 2023/04/25 15:47:42
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping("/monitor/log")
|
|
|
+public class SysLogController extends BaseController {
|
|
|
+ /**
|
|
|
+ * 查询操作日志列表
|
|
|
+ *
|
|
|
+ * @return 操作日志列表
|
|
|
+ */
|
|
|
+ @GetMapping("/list")
|
|
|
+ public AjaxResult list()
|
|
|
+ {
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ File file = new File(filePath);//父级文件夹
|
|
|
+ String[] fileNameList = file.list();//文件名列表
|
|
|
+ List<SysLog> logList = new ArrayList<>();//日志信息列表
|
|
|
+ for (String fileName : fileNameList) {
|
|
|
+ SysLog sysLog = new SysLog();
|
|
|
+ sysLog.setFileName(fileName);//文件名
|
|
|
+ sysLog.setFileUrl(file.getAbsolutePath() + "\\" + fileName);//绝对路径
|
|
|
+ logList.add(sysLog);
|
|
|
+ }
|
|
|
+ return AjaxResult.success(logList);
|
|
|
+ }
|
|
|
+}
|