1 Star 0 Fork 0

liangchao/gweb

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
log.go 1.57 KB
一键复制 编辑 原始数据 按行查看 历史
LiangChao(GW00244384) 提交于 2025-07-23 15:29 +08:00 . 2.0
package gweb
import (
"path/filepath"
"strings"
"gitee.com/makitdone/gx"
"github.com/google/uuid"
)
// 日志支持
type LogView struct{}
func (view *LogView) RegisterRoutes(router *RouterGroup) {
router.POST("/app/log/download", MustBeSuperuser(), view.download)
router.GET("/app/log/list", MustBeSuperuser(), view.list)
router.POST("/app/log/detail", MustBeSuperuser(), view.detail)
router.POST("/app/log/clear", MustBeSuperuser(), view.clear)
}
func (view *LogView) download(c *Context) {
app := c.App
dir := app.LogDirectory()
filename := uuid.New().String()
filename = filepath.Join(dir, filename+".zip")
gx.Path(dir).Zip(filename)
c.FileAttachment(filename, "logs.zip", true)
}
// 获取日志列表
func (view *LogView) list(c *Context) {
app := c.App
dir := app.LogDirectory()
if files, err := gx.Path(dir).Glob("**/*.log"); err != nil {
c.Error(err.Error())
} else {
for i, file := range files {
files[i] = strings.Replace(file, dir, "", 1)
}
c.SuccessWithData(files)
}
}
func (view *LogView) detail(c *Context) {
var req struct {
Filename string `json:"filename"`
}
c.ShouldBindJSON(&req)
app := c.App
dir := app.LogDirectory()
filePath := filepath.Join(dir, req.Filename)
if !gx.Path(filePath).IsFile() {
c.Error("Log file not found")
return
}
content, err := gx.Path(filePath).ReadAll()
if err != nil {
c.Error(err.Error())
return
}
c.SuccessWithData(string(content))
}
func (view *LogView) clear(c *Context) {
dir := c.App.LogDirectory()
if err := gx.Path(dir).RemoveAll(); err != nil {
c.Error(err.Error())
}
c.Success()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/makitdone/gweb.git
git@gitee.com:makitdone/gweb.git
makitdone
gweb
gweb
v2.0.1

搜索帮助