github.com/yaoapp/kun@v0.9.0/log/log.go (about) 1 package log 2 3 import ( 4 "fmt" 5 "io" 6 7 "github.com/sirupsen/logrus" 8 ) 9 10 // F the log fields 11 type F map[string]interface{} 12 13 // Entry the log entry 14 type Entry struct { 15 *logrus.Entry 16 } 17 18 // Level type 19 type Level uint32 20 21 // Format type 22 type Format uint32 23 24 const ( 25 // JSON JSON Format 26 JSON Format = iota 27 // TEXT Text Format 28 TEXT = 1 // 1 29 ) 30 31 // These are the different logging levels. You can set the logging level to log 32 // on your instance of logger, obtained with `logrus.New()`. 33 const ( 34 // PanicLevel level, highest level of severity. Logs and then calls panic with the 35 // message passed to Debug, Info, ... 36 PanicLevel Level = iota 37 // FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the 38 // logging level is set to Panic. 39 FatalLevel 40 // ErrorLevel level. Logs. Used for errors that should definitely be noted. 41 // Commonly used for hooks to send errors to an error tracking service. 42 ErrorLevel 43 // WarnLevel level. Non-critical entries that deserve eyes. 44 WarnLevel 45 // InfoLevel level. General operational entries about what's going on inside the 46 // application. 47 InfoLevel 48 // DebugLevel level. Usually only enabled when debugging. Very verbose logging. 49 DebugLevel 50 // TraceLevel level. Designates finer-grained informational events than the Debug. 51 TraceLevel 52 ) 53 54 // SetFormatter 0=JSON, 1=TEXT 55 func SetFormatter(format Format) { 56 if format == JSON { 57 logrus.SetFormatter(&logrus.JSONFormatter{}) 58 } else if format == TEXT { 59 logrus.SetFormatter(&logrus.TextFormatter{}) 60 } 61 } 62 63 // SetOutput log writer 64 func SetOutput(out io.Writer) { 65 logrus.SetOutput(out) 66 } 67 68 // SetLevel log level 69 func SetLevel(level Level) { 70 logrus.SetLevel(logrus.Level(level)) 71 } 72 73 // GetLevel get log level 74 func GetLevel() Level { 75 return Level(logrus.GetLevel()) 76 } 77 78 // With fields 79 func With(fields F) *Entry { 80 return &Entry{logrus.WithFields(logrus.Fields(fields))} 81 } 82 83 // Trace trace log 84 func (entry *Entry) Trace(message string, v ...interface{}) { 85 entry.Entry.Trace(fmt.Sprintf(message, v...)) 86 } 87 88 // Trace trace log 89 func Trace(message string, v ...interface{}) { 90 logrus.Trace(fmt.Sprintf(message, v...)) 91 } 92 93 // Debug debug log 94 func (entry *Entry) Debug(message string, v ...interface{}) { 95 entry.Entry.Debug(fmt.Sprintf(message, v...)) 96 } 97 98 // Debug debug log 99 func Debug(message string, v ...interface{}) { 100 logrus.Debug(fmt.Sprintf(message, v...)) 101 } 102 103 // Info info log 104 func (entry *Entry) Info(message string, v ...interface{}) { 105 entry.Entry.Info(fmt.Sprintf(message, v...)) 106 } 107 108 // Info info log 109 func Info(message string, v ...interface{}) { 110 logrus.Info(fmt.Sprintf(message, v...)) 111 } 112 113 // Warn warn log 114 func (entry *Entry) Warn(message string, v ...interface{}) { 115 entry.Entry.Warn(fmt.Sprintf(message, v...)) 116 } 117 118 // Warn warn log 119 func Warn(message string, v ...interface{}) { 120 logrus.Warn(fmt.Sprintf(message, v...)) 121 } 122 123 // Error error log 124 func (entry *Entry) Error(message string, v ...interface{}) { 125 entry.Entry.Error(fmt.Sprintf(message, v...)) 126 } 127 128 // Error error log 129 func Error(message string, v ...interface{}) { 130 logrus.Error(fmt.Sprintf(message, v...)) 131 } 132 133 // Fatal fatal log Calls os.Exit(1) after logging 134 func (entry *Entry) Fatal(message string, v ...interface{}) { 135 entry.Entry.Fatal(fmt.Sprintf(message, v...)) 136 } 137 138 // Fatal fatal log Calls os.Exit(1) after logging 139 func Fatal(message string, v ...interface{}) { 140 logrus.Fatal(fmt.Sprintf(message, v...)) 141 } 142 143 // Panic panic log Calls panic() after logging 144 func (entry *Entry) Panic(message string, v ...interface{}) { 145 entry.Entry.Panic(fmt.Sprintf(message, v...)) 146 } 147 148 // Panic panic log Calls panic() after logging 149 func Panic(message string, v ...interface{}) { 150 logrus.Panic(fmt.Sprintf(message, v...)) 151 }