From 59ecc2c67d3b5fd38c2d98cbbc1da3fa0c14cf18 Mon Sep 17 00:00:00 2001 From: AH-dark Date: Mon, 29 Aug 2022 21:44:16 +0800 Subject: [PATCH] Chore: removed unused logger.go --- pkg/task/slavetask/transfer.go | 2 +- pkg/util/io.go | 4 +- pkg/util/logger.go | 150 --------------------------------- pkg/util/logger_test.go | 104 ----------------------- pkg/util/session.go | 3 +- service/explorer/upload.go | 2 +- 6 files changed, 7 insertions(+), 258 deletions(-) delete mode 100644 pkg/util/logger.go delete mode 100644 pkg/util/logger_test.go diff --git a/pkg/task/slavetask/transfer.go b/pkg/task/slavetask/transfer.go index 67bb3494..d40b8dac 100644 --- a/pkg/task/slavetask/transfer.go +++ b/pkg/task/slavetask/transfer.go @@ -3,7 +3,7 @@ package slavetask import ( "context" "os" - + model "github.com/cloudreve/Cloudreve/v3/models" "github.com/cloudreve/Cloudreve/v3/pkg/cluster" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem" diff --git a/pkg/util/io.go b/pkg/util/io.go index 25b9dc99..a7d9dfa8 100644 --- a/pkg/util/io.go +++ b/pkg/util/io.go @@ -4,6 +4,8 @@ import ( "io" "os" "path/filepath" + + "github.com/cloudreve/Cloudreve/v3/pkg/logger" ) // Exists reports whether the named file or directory exists. @@ -22,7 +24,7 @@ func CreatNestedFile(path string) (*os.File, error) { if !Exists(basePath) { err := os.MkdirAll(basePath, 0700) if err != nil { - Log().Warning("无法创建目录,%s", err) + logger.Warning("无法创建目录,%s", err) return nil, err } } diff --git a/pkg/util/logger.go b/pkg/util/logger.go deleted file mode 100644 index 107ec718..00000000 --- a/pkg/util/logger.go +++ /dev/null @@ -1,150 +0,0 @@ -package util - -import ( - "fmt" - "github.com/fatih/color" - "sync" - "time" -) - -const ( - // LevelError 错误 - LevelError = iota - // LevelWarning 警告 - LevelWarning - // LevelInformational 提示 - LevelInformational - // LevelDebug 除错 - LevelDebug -) - -var GloablLogger *Logger -var Level = LevelDebug - -// Logger 日志 -type Logger struct { - level int - mu sync.Mutex -} - -// 日志颜色 -var colors = map[string]func(a ...interface{}) string{ - "Warning": color.New(color.FgYellow).Add(color.Bold).SprintFunc(), - "Panic": color.New(color.BgRed).Add(color.Bold).SprintFunc(), - "Error": color.New(color.FgRed).Add(color.Bold).SprintFunc(), - "Info": color.New(color.FgCyan).Add(color.Bold).SprintFunc(), - "Debug": color.New(color.FgWhite).Add(color.Bold).SprintFunc(), -} - -// 不同级别前缀与时间的间隔,保持宽度一致 -var spaces = map[string]string{ - "Warning": "", - "Panic": " ", - "Error": " ", - "Info": " ", - "Debug": " ", -} - -// Println 打印 -func (ll *Logger) Println(prefix string, msg string) { - // TODO Release时去掉 - // color.NoColor = false - - c := color.New() - - ll.mu.Lock() - defer ll.mu.Unlock() - - _, _ = c.Printf( - "%s%s %s %s\n", - colors[prefix]("["+prefix+"]"), - spaces[prefix], - time.Now().Format("2006-01-02 15:04:05"), - msg, - ) -} - -// Panic 极端错误 -func (ll *Logger) Panic(format string, v ...interface{}) { - if LevelError > ll.level { - return - } - msg := fmt.Sprintf(format, v...) - ll.Println("Panic", msg) - panic(msg) -} - -// Error 错误 -func (ll *Logger) Error(format string, v ...interface{}) { - if LevelError > ll.level { - return - } - msg := fmt.Sprintf(format, v...) - ll.Println("Error", msg) -} - -// Warning 警告 -func (ll *Logger) Warning(format string, v ...interface{}) { - if LevelWarning > ll.level { - return - } - msg := fmt.Sprintf(format, v...) - ll.Println("Warning", msg) -} - -// Info 信息 -func (ll *Logger) Info(format string, v ...interface{}) { - if LevelInformational > ll.level { - return - } - msg := fmt.Sprintf(format, v...) - ll.Println("Info", msg) -} - -// Debug 校验 -func (ll *Logger) Debug(format string, v ...interface{}) { - if LevelDebug > ll.level { - return - } - msg := fmt.Sprintf(format, v...) - ll.Println("Debug", msg) -} - -// Print GORM 的 Logger实现 -//func (ll *Logger) Print(v ...interface{}) { -// if LevelDebug > ll.level { -// return -// } -// msg := fmt.Sprintf("[SQL] %s", v...) -// ll.Println(msg) -//} - -// BuildLogger 构建logger -func BuildLogger(level string) { - intLevel := LevelError - switch level { - case "error": - intLevel = LevelError - case "warning": - intLevel = LevelWarning - case "info": - intLevel = LevelInformational - case "debug": - intLevel = LevelDebug - } - l := Logger{ - level: intLevel, - } - GloablLogger = &l -} - -// Log 返回日志对象 -func Log() *Logger { - if GloablLogger == nil { - l := Logger{ - level: Level, - } - GloablLogger = &l - } - return GloablLogger -} diff --git a/pkg/util/logger_test.go b/pkg/util/logger_test.go deleted file mode 100644 index a67c9588..00000000 --- a/pkg/util/logger_test.go +++ /dev/null @@ -1,104 +0,0 @@ -//go:build !race -// +build !race - -package util - -import ( - "github.com/stretchr/testify/assert" - "testing" -) - -func TestBuildLogger(t *testing.T) { - asserts := assert.New(t) - asserts.NotPanics(func() { - BuildLogger("error") - }) - asserts.NotPanics(func() { - BuildLogger("warning") - }) - asserts.NotPanics(func() { - BuildLogger("info") - }) - asserts.NotPanics(func() { - BuildLogger("?") - }) - asserts.NotPanics(func() { - BuildLogger("debug") - }) -} - -func TestLog(t *testing.T) { - asserts := assert.New(t) - asserts.NotNil(Log()) - GloablLogger = nil - asserts.NotNil(Log()) -} - -func TestLogger_Debug(t *testing.T) { - asserts := assert.New(t) - l := Logger{ - level: LevelDebug, - } - asserts.NotPanics(func() { - l.Debug("123") - }) - l.level = LevelError - asserts.NotPanics(func() { - l.Debug("123") - }) -} - -func TestLogger_Info(t *testing.T) { - asserts := assert.New(t) - l := Logger{ - level: LevelDebug, - } - asserts.NotPanics(func() { - l.Info("123") - }) - l.level = LevelError - asserts.NotPanics(func() { - l.Info("123") - }) -} -func TestLogger_Warning(t *testing.T) { - asserts := assert.New(t) - l := Logger{ - level: LevelDebug, - } - asserts.NotPanics(func() { - l.Warning("123") - }) - l.level = LevelError - asserts.NotPanics(func() { - l.Warning("123") - }) -} - -func TestLogger_Error(t *testing.T) { - asserts := assert.New(t) - l := Logger{ - level: LevelDebug, - } - asserts.NotPanics(func() { - l.Error("123") - }) - l.level = -1 - asserts.NotPanics(func() { - l.Error("123") - }) -} - -func TestLogger_Panic(t *testing.T) { - asserts := assert.New(t) - l := Logger{ - level: LevelDebug, - } - asserts.Panics(func() { - l.Panic("123") - }) - l.level = -1 - asserts.NotPanics(func() { - l.Error("123") - }) -} diff --git a/pkg/util/session.go b/pkg/util/session.go index 705eee19..8bd7b491 100644 --- a/pkg/util/session.go +++ b/pkg/util/session.go @@ -1,6 +1,7 @@ package util import ( + "github.com/cloudreve/Cloudreve/v3/pkg/logger" "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" ) @@ -14,7 +15,7 @@ func SetSession(c *gin.Context, list map[string]interface{}) { err := s.Save() if err != nil { - Log().Warning("无法设置 Session 值:%s", err) + logger.Warning("无法设置 Session 值:%s", err) } } diff --git a/service/explorer/upload.go b/service/explorer/upload.go index 3bd5b0f7..a12d56ba 100644 --- a/service/explorer/upload.go +++ b/service/explorer/upload.go @@ -7,7 +7,7 @@ import ( "strconv" "strings" "time" - + model "github.com/cloudreve/Cloudreve/v3/models" "github.com/cloudreve/Cloudreve/v3/pkg/auth" "github.com/cloudreve/Cloudreve/v3/pkg/cache"