feat: 日志持久化存储到文件+管理员后台查看/清空日志页面
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"sale/internal/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type LogHandler struct{}
|
||||
|
||||
func NewLogHandler() *LogHandler {
|
||||
return &LogHandler{}
|
||||
}
|
||||
|
||||
func (h *LogHandler) GetLogs(c *gin.Context) {
|
||||
lines := 500
|
||||
if l := c.Query("lines"); l != "" {
|
||||
if v, err := strconv.Atoi(l); err == nil && v > 0 {
|
||||
lines = v
|
||||
}
|
||||
}
|
||||
|
||||
logs, err := utils.GetLogs(lines)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read logs"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"data": logs})
|
||||
}
|
||||
|
||||
func (h *LogHandler) ClearLogs(c *gin.Context) {
|
||||
if err := utils.ClearLogs(); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to clear logs"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Logs cleared successfully"})
|
||||
}
|
||||
|
||||
func (h *LogHandler) GetLogInfo(c *gin.Context) {
|
||||
size, err := utils.GetLogSize()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get log info"})
|
||||
return
|
||||
}
|
||||
|
||||
modTime, err := utils.GetLogModTime()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get log info"})
|
||||
return
|
||||
}
|
||||
|
||||
var sizeStr string
|
||||
if size >= 1024*1024 {
|
||||
sizeStr = fmt.Sprintf("%.2f MB", float64(size)/(1024*1024))
|
||||
} else if size >= 1024 {
|
||||
sizeStr = fmt.Sprintf("%.2f KB", float64(size)/1024)
|
||||
} else {
|
||||
sizeStr = fmt.Sprintf("%d B", size)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"data": gin.H{
|
||||
"size": size,
|
||||
"size_str": sizeStr,
|
||||
"mod_time": modTime,
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user