From b76d875493535a08846780e75f22c734b44c60b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B7=A6=E7=A7=80=E6=9C=8B?= Date: Wed, 2 Jan 2019 19:14:29 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E4=B8=BA=E4=B8=80=E4=B8=AA=E6=96=87=E4=BB=B6=EF=BC=8C=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E6=97=A5=E5=BF=97=E7=AD=9B=E9=80=89=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- program/logger/logger.go | 14 +- program/v1/v1.go | 71 +++++++--- static/src/views/Logs.vue | 60 ++++++++- tpls/tpls.go | 272 +++++++++++++++++++++++++++++++++++--- 4 files changed, 370 insertions(+), 47 deletions(-) diff --git a/program/logger/logger.go b/program/logger/logger.go index 9a95cd6..41d9966 100644 --- a/program/logger/logger.go +++ b/program/logger/logger.go @@ -18,18 +18,18 @@ var ( // InitLogger 日志初始化,用于记录操作日志 func InitLogger(logPath string, isDebug bool) (*zap.SugaredLogger, error) { infoLogPath := "" - errorLogPath := "" + // errorLogPath := "" if logPath == "" { logRoot := common.GetRootDir() + "logs/" if isExt, _ := common.PathExists(logRoot); isExt == false { os.MkdirAll(logRoot, os.ModePerm) } - infoLogPath = logRoot + time.Now().Format("20060102_info") + ".log" - errorLogPath = logRoot + time.Now().Format("20060102_error") + ".log" + infoLogPath = logRoot + time.Now().Format("20060102") + ".log" + // errorLogPath = logRoot + time.Now().Format("20060102_error") + ".log" } else { logPath = strings.TrimRight(logPath, string(os.PathSeparator)) - infoLogPath = logPath + string(os.PathSeparator) + time.Now().Format("20060102_info") + ".log" - errorLogPath = logPath + string(os.PathSeparator) + time.Now().Format("20060102_error") + ".log" + infoLogPath = logPath + string(os.PathSeparator) + time.Now().Format("20060102") + ".log" + // errorLogPath = logPath + string(os.PathSeparator) + time.Now().Format("20060102_error") + ".log" } cfg := &zap.Config{ @@ -40,11 +40,11 @@ func InitLogger(logPath string, isDebug bool) (*zap.SugaredLogger, error) { if isDebug == true { atom.SetLevel(zapcore.DebugLevel) cfg.OutputPaths = []string{"stdout"} - cfg.ErrorOutputPaths = []string{"stdout"} + // cfg.ErrorOutputPaths = []string{"stdout"} } else { atom.SetLevel(zapcore.InfoLevel) cfg.OutputPaths = []string{infoLogPath} - cfg.ErrorOutputPaths = []string{errorLogPath} + // cfg.ErrorOutputPaths = []string{errorLogPath} } cfg.Level = atom logger, err := cfg.Build() diff --git a/program/v1/v1.go b/program/v1/v1.go index 75d7a16..471b057 100644 --- a/program/v1/v1.go +++ b/program/v1/v1.go @@ -31,6 +31,8 @@ func V1(v1 *gin.RouterGroup) { v1.GET("/logs", getLogsList) // 查询日志 + v1.GET("/users", getUserList) // 获取用户列表 + v1.GET("/logtypes", getLogTypeList) // 获取日志类型列表 } // 获取etcd key列表 @@ -305,12 +307,41 @@ func getEtcdServerList(c *gin.Context) { c.JSON(http.StatusOK, list1) } +// 获取用户列表 +func getUserList(c *gin.Context) { + us := make([]map[string]string, 0) + cfg := config.GetCfg() + if cfg != nil { + for _, v := range cfg.Users { + us = append(us, map[string]string{ + "name": v.Username, + "role": v.Role, + }) + } + } + + c.JSON(http.StatusOK, us) +} + +// 获取操作类型列表 +func getLogTypeList(c *gin.Context) { + c.JSON(http.StatusOK, []string{ + "获取列表", + "获取key的值", + "获取etcd集群信息", + "删除key", + "保存key", + "获取etcd服务列表", + }) +} + type LogLine struct { - Date string `json:"date"` - User string `json:"user"` - Role string `json:"role"` - Msg string `json:"msg"` - Ts float64 `json:"ts"` + Date string `json:"date"` + User string `json:"user"` + Role string `json:"role"` + Msg string `json:"msg"` + Ts float64 `json:"ts"` + Level string `json:"level"` } // 查看日志 @@ -341,7 +372,7 @@ func getLogsList(c *gin.Context) { startLine := (pageNum - 1) * pageSizeNum endLine := pageNum * pageSizeNum - fileName := fmt.Sprintf("%slogs/%s_info.log", common.GetRootDir(), dateStr) + fileName := fmt.Sprintf("%slogs/%s.log", common.GetRootDir(), dateStr) // fmt.Println(fileName) // 判断文件是否存在 if exists, err := common.PathExists(fileName); exists == false || err != nil { @@ -361,24 +392,28 @@ func getLogsList(c *gin.Context) { defer file.Close() fileScanner := bufio.NewScanner(file) lineCount := 1 - listStr := make([]string, 0) + list := make([]*LogLine, 0) // 最终数组 for fileScanner.Scan() { - if lineCount > startLine && lineCount <= endLine { - listStr = append(listStr, fileScanner.Text()) + logTxt := fileScanner.Text() + if logTxt == "" { + continue } - lineCount++ - } - // 解析每一行 - list := make([]*LogLine, 0) - for _, v := range listStr { + // 解析日志 oneLog := new(LogLine) - err = json.Unmarshal([]byte(v), oneLog) + err = json.Unmarshal([]byte(logTxt), oneLog) if err != nil { - logger.Log.Errorw("解析一行的日志错误", "err", err) + logger.Log.Errorw("解析日志文件错误", "err", err) continue } - oneLog.Date = time.Unix(int64(oneLog.Ts), 0).Format("2006-01-02 15:04:05") - list = append(list, oneLog) + + if lineCount > startLine && lineCount <= endLine { + // 判断用户和日志类型参数 + + oneLog.Date = time.Unix(int64(oneLog.Ts), 0).Format("2006-01-02 15:04:05") + list = append(list, oneLog) + } + + lineCount++ } err = nil diff --git a/static/src/views/Logs.vue b/static/src/views/Logs.vue index cab965f..dccda33 100644 --- a/static/src/views/Logs.vue +++ b/static/src/views/Logs.vue @@ -14,7 +14,27 @@