Bladeren bron

增加日志查看接口

zou.yingbin 4 jaren geleden
bovenliggende
commit
f8a30141d3
4 gewijzigde bestanden met toevoegingen van 84 en 2 verwijderingen
  1. 0 1
      controllers/other/logfiledown.go
  2. 3 1
      controllers/other/logfilelist.go
  3. 80 0
      controllers/other/logfileview.go
  4. 1 0
      routers/router.go

+ 0 - 1
controllers/other/logfiledown.go

@@ -43,7 +43,6 @@ func DownloadFile(ctx *gin.Context) {
 		return
 	}
 	if fullPath, err := fGetFile(filename); err == nil && fullPath != "" {
-		fmt.Println("download file:", fullPath)
 		ctx.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
 		ctx.Writer.Header().Add("Content-Type", "application/octet-stream")
 		ctx.File(fullPath)

+ 3 - 1
controllers/other/logfilelist.go

@@ -40,8 +40,10 @@ func LogList(c *gin.Context) {
 	if d, err := ioutil.ReadDir(path); err == nil {
 		for _, v := range d {
 			if !v.IsDir() {
-				lk := fmt.Sprintf(`<a href="%v/debug/download?filename=%v" target="_blank">%v</a> size:%.2f k`,
+				lk := fmt.Sprintf(`<a href="%v/debug/download?filename=%v" target="_blank">%v</a>   size:%.2f k`,
 					host, v.Name(), v.Name(), float64(v.Size())/1024.0)
+				lk2 := fmt.Sprintf(`<a href="%v/debug/log/view?filename=%v" target="_blank">查看</a>`, host, v.Name())
+				lk = lk + " " + lk2
 				sFile = append(sFile, lk)
 			}
 		}

+ 80 - 0
controllers/other/logfileview.go

@@ -0,0 +1,80 @@
+/**
+* @Author: zou.yingbin
+* @Create  : 2021/10/18 9:52
+* @Modify  : 2021/10/18 9:52
+* @note    : 查看日志文件内容
+ */
+
+package other
+
+import (
+	"bufio"
+	"github.com/gin-gonic/gin"
+	"html/template"
+	"mtp2_if/config"
+	"net/http"
+	"os"
+)
+
+// LogView 查看日志文件内容
+func LogView(c *gin.Context) {
+	// 最多显示最新100行, 如果要查看更多, 请下载文件
+	filename := c.DefaultQuery("filename", "")
+	if filename == "" {
+		c.JSON(http.StatusBadRequest, gin.H{"message": "param error"})
+		return
+	}
+	str := getFileContent(filename, 200)
+	//c.String(http.StatusOK,  str)
+
+	const templateText = `
+<p>
+<h1></h1>
+<h3> 日志文件内容(只显示最新的n行) </h3>
+-------------------<br>
+</p>
+<p>
+{{.data}}
+</p>
+`
+	if tmpl, err := template.New("index").Parse(templateText); err == nil {
+		_ = tmpl.Execute(c.Writer, gin.H{"data": template.HTML(str)})
+	} else {
+		c.String(400, "template err")
+	}
+}
+
+// getFileContent 获取文件内容
+func getFileContent(filename string, nLines int) string {
+	fileDir := config.SerCfg.LogCfg.LogPath
+	if len(fileDir) > 0 {
+		if nLen := len(fileDir); fileDir[nLen-1] != '/' {
+			fileDir += "/"
+		}
+	}
+	file, err := os.Open(fileDir + filename)
+	if err != nil {
+		return "file not found"
+	}
+	scanner := bufio.NewScanner(file)
+	total := 0
+	for scanner.Scan() {
+		total++
+	}
+	readPos := 0
+	if total > nLines {
+		readPos = total - nLines
+	}
+	txt := ""
+	file.Seek(0, 0)
+	index := 0
+	s := bufio.NewScanner(file)
+	for s.Scan() {
+		if index >= readPos {
+			txt += s.Text() + "<br>"
+		}
+		index++
+	}
+
+	return txt
+}

+ 1 - 0
routers/router.go

@@ -54,6 +54,7 @@ func InitRouter() *gin.Engine {
 	if config.SerCfg.GetDebugMode() {
 		r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
 		r.GET("/debug/log", other.LogList)
+		r.GET("/debug/log/view", other.LogView)
 		r.GET("/debug/download", other.DownloadFile)
 	}
 	// 终端配置