github.com/goravel/framework@v1.13.9/log/logger/single.go (about) 1 package logger 2 3 import ( 4 "errors" 5 "path/filepath" 6 7 "github.com/rifflock/lfshook" 8 "github.com/sirupsen/logrus" 9 10 "github.com/goravel/framework/contracts/config" 11 "github.com/goravel/framework/log/formatter" 12 "github.com/goravel/framework/support" 13 ) 14 15 type Single struct { 16 config config.Config 17 } 18 19 func NewSingle(config config.Config) *Single { 20 return &Single{ 21 config: config, 22 } 23 } 24 25 func (single *Single) Handle(channel string) (logrus.Hook, error) { 26 logPath := single.config.GetString(channel + ".path") 27 if logPath == "" { 28 return nil, errors.New("error log path") 29 } 30 31 logPath = filepath.Join(support.RelativePath, logPath) 32 levels := getLevels(single.config.GetString(channel + ".level")) 33 pathMap := lfshook.PathMap{} 34 for _, level := range levels { 35 pathMap[level] = logPath 36 } 37 38 return lfshook.NewHook( 39 pathMap, 40 formatter.NewGeneral(single.config), 41 ), nil 42 } 43 44 func getLevels(level string) []logrus.Level { 45 if level == "panic" { 46 return []logrus.Level{ 47 logrus.PanicLevel, 48 } 49 } 50 51 if level == "fatal" { 52 return []logrus.Level{ 53 logrus.FatalLevel, 54 logrus.PanicLevel, 55 } 56 } 57 58 if level == "error" { 59 return []logrus.Level{ 60 logrus.ErrorLevel, 61 logrus.FatalLevel, 62 logrus.PanicLevel, 63 } 64 } 65 66 if level == "warning" { 67 return []logrus.Level{ 68 logrus.WarnLevel, 69 logrus.ErrorLevel, 70 logrus.FatalLevel, 71 logrus.PanicLevel, 72 } 73 } 74 75 if level == "info" { 76 return []logrus.Level{ 77 logrus.InfoLevel, 78 logrus.WarnLevel, 79 logrus.ErrorLevel, 80 logrus.FatalLevel, 81 logrus.PanicLevel, 82 } 83 } 84 85 return []logrus.Level{ 86 logrus.DebugLevel, 87 logrus.InfoLevel, 88 logrus.WarnLevel, 89 logrus.ErrorLevel, 90 logrus.FatalLevel, 91 logrus.PanicLevel, 92 } 93 }