github.com/packtpublishing/learning-functional-programming-in-go@v0.0.0-20230130084745-8b849f6d58c4/Chapter07/func-param/src/utils/logger.go (about) 1 package utils 2 3 import ( 4 "io" 5 log "github.com/sirupsen/logrus" 6 "os" 7 "fmt" 8 ) 9 10 var ( 11 Debug *log.Logger 12 Info *log.Logger 13 Error *log.Logger 14 InfoHandler io.Writer 15 ErrorHandler io.Writer 16 ) 17 18 const BasicTimeStampFormat = "2006-01-02 15:04:05" 19 var LevelDescriptions = []string{"PANC", "FATL", "ERRO", "WARN", "INFO", "DEBG"} 20 21 func InitLog ( 22 traceFileName string, 23 debugHandler io.Writer, 24 infoHandler io.Writer, 25 errorHandler io.Writer, 26 ) { 27 if len(traceFileName) > 0 { 28 _ = os.Remove(traceFileName) 29 file, err := os.OpenFile(traceFileName, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666) 30 if err != nil { 31 log.Fatalf("Failed to create log file: %s", traceFileName) 32 } 33 debugHandler = io.MultiWriter(file, debugHandler) 34 infoHandler = io.MultiWriter(file, infoHandler) 35 errorHandler = io.MultiWriter(file, errorHandler) 36 } 37 38 InfoHandler = infoHandler 39 ErrorHandler = errorHandler 40 41 plainFormatter := new(PlainFormatter) 42 43 basicFormatter := new(BasicFormatter) 44 basicFormatter.TimestampFormat = BasicTimeStampFormat 45 basicFormatter.LevelDesc = LevelDescriptions 46 47 plusVFormatter := new(PlusVFormatter) 48 plusVFormatter.TimestampFormat = BasicTimeStampFormat 49 plusVFormatter.LevelDesc = LevelDescriptions 50 plusVFormatter.FullTimestamp = true 51 52 53 Debug = log.New() 54 Debug.Out = debugHandler 55 //Debug.Formatter = new(log.TextFormatter) //new(log.JSONFormatter) 56 Debug.Formatter = basicFormatter 57 //Debug.Formatter = plainFormatter 58 Debug.Hooks= make(log.LevelHooks) 59 Debug.Level = log.DebugLevel 60 61 Info = log.New() 62 Info.Out = infoHandler 63 //Info.Formatter = customFormatter 64 Info.Formatter = plainFormatter 65 Info.Hooks= make(log.LevelHooks) 66 Info.Level = log.InfoLevel 67 68 Error = log.New() 69 Error.Out = errorHandler 70 //Error.Formatter = plusVFormatter 71 Error.Formatter = plainFormatter 72 Error.Hooks= make(log.LevelHooks) 73 Error.Level = log.DebugLevel 74 } 75 76 77 type PlainFormatter struct {} 78 func (f *PlainFormatter) Format(entry *log.Entry) ([]byte, error) { 79 return []byte(fmt.Sprintf("%s\n", entry.Message)), nil 80 } 81 82 type BasicFormatter struct { 83 TimestampFormat string 84 LevelDesc []string 85 } 86 func (f *BasicFormatter) Format(entry *log.Entry) ([]byte, error) { 87 timestamp := fmt.Sprintf(entry.Time.Format(f.TimestampFormat)) 88 return []byte(fmt.Sprintf("%s %s %s\n", f.LevelDesc[entry.Level], timestamp, entry.Message)), nil 89 } 90 91 type PlusVFormatter struct { 92 TimestampFormat string 93 LevelDesc []string 94 FullTimestamp bool 95 } 96 func (f *PlusVFormatter) Format(entry *log.Entry) ([]byte, error) { 97 timestamp := fmt.Sprintf(entry.Time.Format(f.TimestampFormat)) 98 //TODO: Find bug in logrus that prevents entry.Level from returning correct value 99 return []byte(fmt.Sprintf("%s %s %s\n", f.LevelDesc[Error.Level], timestamp, entry.Message)), nil 100 }