From 9b0c7bfcdcf73a78d7cbe62dd31c7b9ea2821270 Mon Sep 17 00:00:00 2001 From: Granty1 Date: Tue, 24 Mar 2020 13:42:55 +0800 Subject: [PATCH] Rename the directory of 'log' to 'initlog' --- QMPlusServer/cmd/windows.go | 2 +- QMPlusServer/init/initRedis/init_redis.go | 2 +- QMPlusServer/init/initRouter/init_router.go | 2 +- QMPlusServer/init/initlog/log.go | 17 +++ QMPlusServer/init/initlog/qmlog/log.go | 143 ++++++++++++++++++ QMPlusServer/init/qmsql/initMysql.go | 2 +- QMPlusServer/init/registTable/regist_table.go | 2 +- QMPlusServer/main.go | 2 +- QMPlusServer/middleware/logger.go | 2 +- 9 files changed, 167 insertions(+), 7 deletions(-) create mode 100644 QMPlusServer/init/initlog/log.go create mode 100644 QMPlusServer/init/initlog/qmlog/log.go diff --git a/QMPlusServer/cmd/windows.go b/QMPlusServer/cmd/windows.go index 1dbd9a96..ca200497 100644 --- a/QMPlusServer/cmd/windows.go +++ b/QMPlusServer/cmd/windows.go @@ -3,7 +3,7 @@ package cmd import ( "fmt" "gin-vue-admin/config" - "gin-vue-admin/init/log" + "gin-vue-admin/init/initlog" "github.com/gin-gonic/gin" "net/http" "time" diff --git a/QMPlusServer/init/initRedis/init_redis.go b/QMPlusServer/init/initRedis/init_redis.go index 1db86f31..eb87b15c 100644 --- a/QMPlusServer/init/initRedis/init_redis.go +++ b/QMPlusServer/init/initRedis/init_redis.go @@ -2,7 +2,7 @@ package initRedis import ( "gin-vue-admin/config" - "gin-vue-admin/init/log" + "gin-vue-admin/init/initlog" "github.com/go-redis/redis" ) diff --git a/QMPlusServer/init/initRouter/init_router.go b/QMPlusServer/init/initRouter/init_router.go index 5aa1ab94..c41c40a9 100644 --- a/QMPlusServer/init/initRouter/init_router.go +++ b/QMPlusServer/init/initRouter/init_router.go @@ -2,7 +2,7 @@ package initRouter import ( _ "gin-vue-admin/docs" - "gin-vue-admin/init/log" + "gin-vue-admin/init/initlog" "gin-vue-admin/middleware" "gin-vue-admin/router" "github.com/gin-gonic/gin" diff --git a/QMPlusServer/init/initlog/log.go b/QMPlusServer/init/initlog/log.go new file mode 100644 index 00000000..6850a4c7 --- /dev/null +++ b/QMPlusServer/init/initlog/log.go @@ -0,0 +1,17 @@ +package log + +// Custom Logger +type Logger interface { + Debug(v ...interface{}) + Info(v ...interface{}) + Warning(v ...interface{}) + Error(v ...interface{}) + Critical(v ...interface{}) + Fatal(v ...interface{}) +} + +var L Logger + +func SetLogger(logger Logger) { + L = logger +} diff --git a/QMPlusServer/init/initlog/qmlog/log.go b/QMPlusServer/init/initlog/qmlog/log.go new file mode 100644 index 00000000..fdae1349 --- /dev/null +++ b/QMPlusServer/init/initlog/qmlog/log.go @@ -0,0 +1,143 @@ +package qmlog + +// Register logger +import ( + "errors" + "fmt" + "gin-vue-admin/config" + "gin-vue-admin/init/initlog" + "gin-vue-admin/tools" + rotatelogs "github.com/lestrrat/go-file-rotatelogs" + oplogging "github.com/op/go-logging" + "io" + "os" + "strings" + "time" +) + +const ( + logDir = "log" + logSoftLink = "api.log" + module = "gin-vue-admin" +) + +var ( + configNotFound = errors.New("logger config not found") + + defaultFormatter = `%{time:2006/01/02 - 15:04:05.000} %{longfile} %{color:bold}▶ [%{level:.6s}] %{message}%{color:reset}` +) + +type Logger struct{ + logger *oplogging.Logger +} + +func NewLogger() (log.Logger, error) { + c := config.GinVueAdminconfig.Log + logger := oplogging.MustGetLogger(module) + var backends []oplogging.Backend + backends = registerStdout(c, backends) + backends = registerFile(c, backends) + + oplogging.SetBackend(backends...) + log.SetLogger(logger) + return logger, nil +} + +func registerStdout(c config.Log, backends []oplogging.Backend) []oplogging.Backend { + for _, v := range c.Stdout { + level, err := oplogging.LogLevel(v) + if err != nil { + fmt.Println(err) + continue + } + backends = append(backends, createBackend(os.Stdout, c, level)) + } + + return backends +} + +func registerFile(c config.Log, backends []oplogging.Backend) []oplogging.Backend { + if len(c.File) > 0 { + if ok, _ := tools.PathExists(logDir); !ok { + // directory not exist + fmt.Println("create log directory") + _ = os.Mkdir(logDir, os.ModePerm) + } + apiLogPath := logDir + string(os.PathSeparator) + logSoftLink + fileWriter, err := rotatelogs.New( + apiLogPath+".%Y-%m-%d-%H-%M.log", + // generate soft link, point to latest log file + rotatelogs.WithLinkName(apiLogPath), + // maximum time to save log files + rotatelogs.WithMaxAge(7*24*time.Hour), + // time period of log file switching + rotatelogs.WithRotationTime(24*time.Hour), + ) + if err != nil { + fmt.Println(err) + return backends + } + for _, v := range c.File { + level, err := oplogging.LogLevel(v) + if err != nil { + fmt.Println(err) + continue + } + backends = append(backends, createBackend(fileWriter, c, level)) + } + } + + return backends +} + +func createBackend(w io.Writer, c config.Log, level oplogging.Level) oplogging.Backend { + backend := oplogging.NewLogBackend(w, c.Prefix, 0) + stdoutWriter := false + if w == os.Stdout { + stdoutWriter = true + } + format := getLogFormatter(c, stdoutWriter) + backendLeveled := oplogging.AddModuleLevel(oplogging.NewBackendFormatter(backend, format)) + backendLeveled.SetLevel(level, module) + return backendLeveled +} + +func getLogFormatter(c config.Log, stdoutWriter bool) oplogging.Formatter { + pattern := defaultFormatter + if !stdoutWriter { + // Color is only required for console output + // Other writers don't need %{color} tag + pattern = strings.Replace(pattern, "%{color:bold}", "", -1) + pattern = strings.Replace(pattern, "%{color:reset}", "", -1) + } + if !c.LogFile { + // Remove %{logfile} tag + pattern = strings.Replace(pattern, "%{longfile}", "", -1) + } + return oplogging.MustStringFormatter(pattern) +} + +func (l Logger) Debug(v ...interface{}) { + l.logger.Debug(v) +} + +func (l Logger) Info(v ...interface{}) { + l.logger.Info(v) +} + +func (l Logger) Warning(v ...interface{}) { + l.logger.Warning(v) +} + +func (l Logger) Error(v ...interface{}) { + l.logger.Error(v) +} + +func (l Logger) Critical(v ...interface{}) { + l.logger.Critical(v) +} + +func (l Logger) Fatal(v ...interface{}) { + l.logger.Fatal(v) +} + diff --git a/QMPlusServer/init/qmsql/initMysql.go b/QMPlusServer/init/qmsql/initMysql.go index 1227e855..269860fc 100644 --- a/QMPlusServer/init/qmsql/initMysql.go +++ b/QMPlusServer/init/qmsql/initMysql.go @@ -2,7 +2,7 @@ package qmsql import ( "gin-vue-admin/config" - "gin-vue-admin/init/log" + "gin-vue-admin/init/initlog" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/mysql" ) diff --git a/QMPlusServer/init/registTable/regist_table.go b/QMPlusServer/init/registTable/regist_table.go index 3920c62d..e0d620f4 100644 --- a/QMPlusServer/init/registTable/regist_table.go +++ b/QMPlusServer/init/registTable/regist_table.go @@ -1,7 +1,7 @@ package registTable import ( - "gin-vue-admin/init/log" + "gin-vue-admin/init/initlog" "gin-vue-admin/model/dbModel" "gin-vue-admin/model/sysModel" "github.com/jinzhu/gorm" diff --git a/QMPlusServer/main.go b/QMPlusServer/main.go index d695c7cb..2763cb55 100644 --- a/QMPlusServer/main.go +++ b/QMPlusServer/main.go @@ -6,7 +6,7 @@ import ( "gin-vue-admin/config" "gin-vue-admin/init/initRedis" "gin-vue-admin/init/initRouter" - "gin-vue-admin/init/log/qmlog" + "gin-vue-admin/init/initlog/qmlog" "gin-vue-admin/init/qmsql" "gin-vue-admin/init/registTable" //"runtime" diff --git a/QMPlusServer/middleware/logger.go b/QMPlusServer/middleware/logger.go index 2368d0b4..23869bbb 100644 --- a/QMPlusServer/middleware/logger.go +++ b/QMPlusServer/middleware/logger.go @@ -2,7 +2,7 @@ package middleware import ( "bytes" - "gin-vue-admin/init/log" + "gin-vue-admin/init/initlog" "net/http/httputil" "strings" "time"