github.com/l3x/learn-fp-go@v0.0.0-20171228022418-7639825d0b71/4-purely-functional/ch10-monads/01_car_steps/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 var DebugChars = ">>" 21 22 func InitLog ( 23 traceFileName string, 24 debugHandler io.Writer, 25 infoHandler io.Writer, 26 errorHandler io.Writer, 27 ) { 28 if len(traceFileName) > 0 { 29 _ = os.Remove(traceFileName) 30 file, err := os.OpenFile(traceFileName, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666) 31 if err != nil { 32 log.Fatalf("Failed to create log file: %s", traceFileName) 33 } 34 debugHandler = io.MultiWriter(file, debugHandler) 35 infoHandler = io.MultiWriter(file, infoHandler) 36 errorHandler = io.MultiWriter(file, errorHandler) 37 } 38 39 InfoHandler = infoHandler 40 ErrorHandler = errorHandler 41 42 plainFormatter := new(PlainFormatter) 43 44 basicFormatter := new(BasicFormatter) 45 basicFormatter.TimestampFormat = BasicTimeStampFormat 46 basicFormatter.LevelDesc = LevelDescriptions 47 48 basicDebugFormatter := new(BasicDebugFormatter) 49 50 plusVFormatter := new(PlusVFormatter) 51 plusVFormatter.TimestampFormat = BasicTimeStampFormat 52 plusVFormatter.LevelDesc = LevelDescriptions 53 plusVFormatter.FullTimestamp = true 54 55 logLevel, err := log.ParseLevel(Config.LogLevel) 56 if err != nil { 57 println("ERROR: " + err.Error()) 58 logLevel = log.InfoLevel 59 } 60 61 Debug = log.New() 62 Debug.Out = debugHandler 63 //Debug.Formatter = new(log.TextFormatter) //new(log.JSONFormatter) 64 if Config.LogVerbose { 65 Debug.Formatter = basicFormatter 66 } else { 67 Debug.Formatter = basicDebugFormatter 68 } 69 //Debug.Formatter = plainFormatter 70 Debug.Hooks= make(log.LevelHooks) 71 Debug.Level = logLevel 72 73 74 Info = log.New() 75 Info.Out = infoHandler 76 //Info.Formatter = customFormatter 77 if Config.LogVerbose { 78 Info.Formatter = basicFormatter 79 } else { 80 Info.Formatter = plainFormatter 81 } 82 Info.Hooks= make(log.LevelHooks) 83 Info.Level = logLevel 84 85 Error = log.New() 86 Error.Out = errorHandler 87 //Error.Formatter = plusVFormatter 88 if Config.LogVerbose { 89 Error.Formatter = basicFormatter 90 } else { 91 Error.Formatter = plainFormatter 92 } 93 Error.Hooks= make(log.LevelHooks) 94 Error.Level = logLevel 95 96 DebugChars = Config.LogDebugChars 97 } 98 99 100 type PlainFormatter struct {} 101 func (f *PlainFormatter) Format(entry *log.Entry) ([]byte, error) { 102 return []byte(fmt.Sprintf("%s\n", entry.Message)), nil 103 } 104 105 type BasicDebugFormatter struct { 106 TimestampFormat string 107 LevelDesc []string 108 } 109 func (f *BasicDebugFormatter) Format(entry *log.Entry) ([]byte, error) { 110 return []byte(fmt.Sprintf("%s %s\n", DebugChars, entry.Message)), nil 111 } 112 113 type BasicFormatter struct { 114 TimestampFormat string 115 LevelDesc []string 116 } 117 func (f *BasicFormatter) Format(entry *log.Entry) ([]byte, error) { 118 timestamp := fmt.Sprintf(entry.Time.Format(f.TimestampFormat)) 119 return []byte(fmt.Sprintf("%s %s %s\n", f.LevelDesc[entry.Level], timestamp, entry.Message)), nil 120 } 121 122 type PlusVFormatter struct { 123 TimestampFormat string 124 LevelDesc []string 125 FullTimestamp bool 126 } 127 func (f *PlusVFormatter) Format(entry *log.Entry) ([]byte, error) { 128 timestamp := fmt.Sprintf(entry.Time.Format(f.TimestampFormat)) 129 //TODO: Find bug in logrus that prevents entry.Level from returning correct value 130 return []byte(fmt.Sprintf("%s %s %s\n", f.LevelDesc[Error.Level], timestamp, entry.Message)), nil 131 }