From 695f3278c3284f6ac55a7aeba4a65eccf706f4cf Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Wed, 20 Sep 2023 16:24:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=9D=A2=E6=9D=BF=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E5=A2=9E=E5=8A=A0=E6=97=A5=E6=9C=9F=E9=80=89?= =?UTF-8?q?=E6=8B=A9=EF=BC=8C=E6=94=AF=E6=8C=81=E8=BF=BD=E8=B8=AA=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=20(#2361)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/logs.go | 31 ++++++++-- backend/app/service/logs.go | 45 ++++++++++++-- backend/app/service/ssh.go | 4 +- backend/router/ro_log.go | 4 +- cmd/server/docs/docs.go | 21 ++++++- cmd/server/docs/swagger.json | 21 ++++++- cmd/server/docs/swagger.yaml | 13 +++- frontend/src/api/modules/log.ts | 7 ++- frontend/src/styles/common.scss | 13 ++-- frontend/src/views/log/login/index.vue | 6 +- frontend/src/views/log/operation/index.vue | 6 +- frontend/src/views/log/system/index.vue | 70 ++++++++++++++++++---- 12 files changed, 202 insertions(+), 39 deletions(-) diff --git a/backend/app/api/v1/logs.go b/backend/app/api/v1/logs.go index 7a1c28bad..92a947b0f 100644 --- a/backend/app/api/v1/logs.go +++ b/backend/app/api/v1/logs.go @@ -91,13 +91,34 @@ func (b *BaseApi) CleanLogs(c *gin.Context) { } // @Tags Logs -// @Summary Load system logs -// @Description 获取系统日志 +// @Summary Load system log files +// @Description 获取系统日志文件列表 // @Success 200 // @Security ApiKeyAuth -// @Router /logs/system [get] -func (b *BaseApi) GetSystemLogs(c *gin.Context) { - data, err := logService.LoadSystemLog() +// @Router /logs/system/files [get] +func (b *BaseApi) GetSystemFiles(c *gin.Context) { + data, err := logService.ListSystemLogFile() + if err != nil { + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) + return + } + + helper.SuccessWithData(c, data) +} + +// @Tags Logs +// @Summary Load system logs +// @Description 获取系统日志 +// @Success 200 +// @Security ApiKeyAuth +// @Router /logs/system [post] +func (b *BaseApi) GetSystemLogs(c *gin.Context) { + var req dto.OperationWithName + if err := c.ShouldBindJSON(&req); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } + data, err := logService.LoadSystemLog(req.Name) if err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return diff --git a/backend/app/service/logs.go b/backend/app/service/logs.go index f73cadf9b..d212957a0 100644 --- a/backend/app/service/logs.go +++ b/backend/app/service/logs.go @@ -1,8 +1,12 @@ package service import ( + "fmt" "os" "path" + "path/filepath" + "sort" + "strings" "github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/app/model" @@ -19,13 +23,14 @@ type LogService struct{} const logs = "https://resource.fit2cloud.com/installation-log.sh" type ILogService interface { + ListSystemLogFile() ([]string, error) CreateLoginLog(operation model.LoginLog) error PageLoginLog(search dto.SearchLgLogWithPage) (int64, interface{}, error) CreateOperationLog(operation model.OperationLog) error PageOperationLog(search dto.SearchOpLogWithPage) (int64, interface{}, error) - LoadSystemLog() (string, error) + LoadSystemLog(name string) (string, error) CleanLogs(logtype string) error } @@ -38,6 +43,32 @@ func (u *LogService) CreateLoginLog(operation model.LoginLog) error { return logRepo.CreateLoginLog(&operation) } +func (u *LogService) ListSystemLogFile() ([]string, error) { + logDir := path.Join(global.CONF.System.BaseDir, "1panel/log") + var files []string + if err := filepath.Walk(logDir, func(pathItem string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if !info.IsDir() && (strings.HasSuffix(info.Name(), ".log") || strings.HasSuffix(info.Name(), ".log.gz")) { + files = append(files, strings.TrimSuffix(info.Name(), ".gz")) + return nil + } + return nil + }); err != nil { + return nil, err + } + + if len(files) < 2 { + return files, nil + } + sort.Slice(files, func(i, j int) bool { + return files[i] > files[j] + }) + + return files, nil +} + func (u *LogService) PageLoginLog(req dto.SearchLgLogWithPage) (int64, interface{}, error) { total, ops, err := logRepo.PageLoginLog( req.Page, @@ -81,10 +112,16 @@ func (u *LogService) PageOperationLog(req dto.SearchOpLogWithPage) (int64, inter return total, dtoOps, err } -func (u *LogService) LoadSystemLog() (string, error) { - filePath := path.Join(global.CONF.System.DataDir, "log/1Panel.log") +func (u *LogService) LoadSystemLog(name string) (string, error) { + filePath := path.Join(global.CONF.System.DataDir, "log", name) if _, err := os.Stat(filePath); err != nil { - return "", buserr.New("ErrHttpReqNotFound") + fileGzPath := path.Join(global.CONF.System.DataDir, "log", name+".gz") + if _, err := os.Stat(fileGzPath); err != nil { + return "", buserr.New("ErrHttpReqNotFound") + } + if err := handleGunzip(fileGzPath); err != nil { + return "", fmt.Errorf("handle ungzip file %s falied, err: %v", fileGzPath, err) + } } content, err := os.ReadFile(filePath) if err != nil { diff --git a/backend/app/service/ssh.go b/backend/app/service/ssh.go index 9db48ad35..cd1e5484c 100644 --- a/backend/app/service/ssh.go +++ b/backend/app/service/ssh.go @@ -240,7 +240,7 @@ func (u *SSHService) LoadLog(req dto.SearchSSHLog) (*dto.SSHLog, error) { if err != nil { return err } - if !info.IsDir() && strings.HasPrefix(info.Name(), "secure") || strings.HasPrefix(info.Name(), "auth") { + if !info.IsDir() && (strings.HasPrefix(info.Name(), "secure") || strings.HasPrefix(info.Name(), "auth")) { if !strings.HasSuffix(info.Name(), ".gz") { fileList = append(fileList, sshFileItem{Name: pathItem, Year: info.ModTime().Year()}) return nil @@ -311,7 +311,7 @@ func (u *SSHService) AnalysisLog(req dto.SearchForAnalysis) ([]dto.SSHLogAnalysi if err != nil { return err } - if !info.IsDir() && strings.HasPrefix(info.Name(), "secure") || strings.HasPrefix(info.Name(), "auth") { + if !info.IsDir() && (strings.HasPrefix(info.Name(), "secure") || strings.HasPrefix(info.Name(), "auth")) { if !strings.HasSuffix(info.Name(), ".gz") { fileList = append(fileList, pathItem) return nil diff --git a/backend/router/ro_log.go b/backend/router/ro_log.go index 2294748cb..95b567eef 100644 --- a/backend/router/ro_log.go +++ b/backend/router/ro_log.go @@ -17,7 +17,7 @@ func (s *LogRouter) InitLogRouter(Router *gin.RouterGroup) { operationRouter.POST("/login", baseApi.GetLoginLogs) operationRouter.POST("/operation", baseApi.GetOperationLogs) operationRouter.POST("/clean", baseApi.CleanLogs) - operationRouter.GET("/system", baseApi.GetSystemLogs) - + operationRouter.GET("/system/files", baseApi.GetSystemFiles) + operationRouter.POST("/system", baseApi.GetSystemLogs) } } diff --git a/cmd/server/docs/docs.go b/cmd/server/docs/docs.go index 283de3f00..68071c20d 100644 --- a/cmd/server/docs/docs.go +++ b/cmd/server/docs/docs.go @@ -7465,7 +7465,7 @@ const docTemplate = `{ } }, "/logs/system": { - "get": { + "post": { "security": [ { "ApiKeyAuth": [] @@ -7483,6 +7483,25 @@ const docTemplate = `{ } } }, + "/logs/system/files": { + "get": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "获取系统日志文件列表", + "tags": [ + "Logs" + ], + "summary": "Load system log files", + "responses": { + "200": { + "description": "OK" + } + } + } + }, "/openResty": { "get": { "security": [ diff --git a/cmd/server/docs/swagger.json b/cmd/server/docs/swagger.json index 2e53860c5..cbc43ccee 100644 --- a/cmd/server/docs/swagger.json +++ b/cmd/server/docs/swagger.json @@ -7458,7 +7458,7 @@ } }, "/logs/system": { - "get": { + "post": { "security": [ { "ApiKeyAuth": [] @@ -7476,6 +7476,25 @@ } } }, + "/logs/system/files": { + "get": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "获取系统日志文件列表", + "tags": [ + "Logs" + ], + "summary": "Load system log files", + "responses": { + "200": { + "description": "OK" + } + } + } + }, "/openResty": { "get": { "security": [ diff --git a/cmd/server/docs/swagger.yaml b/cmd/server/docs/swagger.yaml index 12f7a1b7a..6b4682b68 100644 --- a/cmd/server/docs/swagger.yaml +++ b/cmd/server/docs/swagger.yaml @@ -8807,7 +8807,7 @@ paths: tags: - Logs /logs/system: - get: + post: description: 获取系统日志 responses: "200": @@ -8817,6 +8817,17 @@ paths: summary: Load system logs tags: - Logs + /logs/system/files: + get: + description: 获取系统日志文件列表 + responses: + "200": + description: OK + security: + - ApiKeyAuth: [] + summary: Load system log files + tags: + - Logs /openResty: get: description: 获取 OpenResty 配置信息 diff --git a/frontend/src/api/modules/log.ts b/frontend/src/api/modules/log.ts index 16ec77055..2b2c80b34 100644 --- a/frontend/src/api/modules/log.ts +++ b/frontend/src/api/modules/log.ts @@ -10,8 +10,11 @@ export const getLoginLogs = (info: Log.SearchLgLog) => { return http.post>(`/logs/login`, info); }; -export const getSystemLogs = () => { - return http.get(`/logs/system`); +export const getSystemFiles = () => { + return http.get>(`/logs/system/files`); +}; +export const getSystemLogs = (name: string) => { + return http.post(`/logs/system`, { name: name }); }; export const cleanLogs = (param: Log.CleanLog) => { diff --git a/frontend/src/styles/common.scss b/frontend/src/styles/common.scss index ee67c5bb5..f451a995c 100644 --- a/frontend/src/styles/common.scss +++ b/frontend/src/styles/common.scss @@ -336,11 +336,6 @@ html { } } -.no-active-button { - background: none; - border: none; -} - .common-prompt { margin-bottom: 20px !important; } @@ -369,3 +364,11 @@ html { box-shadow: 0 1px 0 0 var(--el-input-border-color) inset, 0 -1px 0 0 var(--el-input-border-color) inset, -1px 0 0 0 var(--el-input-border-color) inset; } + +.tag-button { + margin-right: 10px; + &.no-active { + background: none; + border: none; + } +} \ No newline at end of file diff --git a/frontend/src/views/log/login/index.vue b/frontend/src/views/log/login/index.vue index d2b786b48..f9cf23a37 100644 --- a/frontend/src/views/log/login/index.vue +++ b/frontend/src/views/log/login/index.vue @@ -4,13 +4,13 @@