blob: 4ef45186620caa40709fc68540ffaeeeb4c0aa67 [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001package logrus
2
3import (
4 "fmt"
5 "log"
6 "strings"
7)
8
9// Fields type, used to pass to `WithFields`.
10type Fields map[string]interface{}
11
12// Level type
13type Level uint32
14
15// Convert the Level to a string. E.g. PanicLevel becomes "panic".
16func (level Level) String() string {
17 switch level {
18 case TraceLevel:
19 return "trace"
20 case DebugLevel:
21 return "debug"
22 case InfoLevel:
23 return "info"
24 case WarnLevel:
25 return "warning"
26 case ErrorLevel:
27 return "error"
28 case FatalLevel:
29 return "fatal"
30 case PanicLevel:
31 return "panic"
32 }
33
34 return "unknown"
35}
36
37// ParseLevel takes a string level and returns the Logrus log level constant.
38func ParseLevel(lvl string) (Level, error) {
39 switch strings.ToLower(lvl) {
40 case "panic":
41 return PanicLevel, nil
42 case "fatal":
43 return FatalLevel, nil
44 case "error":
45 return ErrorLevel, nil
46 case "warn", "warning":
47 return WarnLevel, nil
48 case "info":
49 return InfoLevel, nil
50 case "debug":
51 return DebugLevel, nil
52 case "trace":
53 return TraceLevel, nil
54 }
55
56 var l Level
57 return l, fmt.Errorf("not a valid logrus Level: %q", lvl)
58}
59
60// UnmarshalText implements encoding.TextUnmarshaler.
61func (level *Level) UnmarshalText(text []byte) error {
62 l, err := ParseLevel(string(text))
63 if err != nil {
64 return err
65 }
66
67 *level = Level(l)
68
69 return nil
70}
71
72// A constant exposing all logging levels
73var AllLevels = []Level{
74 PanicLevel,
75 FatalLevel,
76 ErrorLevel,
77 WarnLevel,
78 InfoLevel,
79 DebugLevel,
80 TraceLevel,
81}
82
83// These are the different logging levels. You can set the logging level to log
84// on your instance of logger, obtained with `logrus.New()`.
85const (
86 // PanicLevel level, highest level of severity. Logs and then calls panic with the
87 // message passed to Debug, Info, ...
88 PanicLevel Level = iota
89 // FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
90 // logging level is set to Panic.
91 FatalLevel
92 // ErrorLevel level. Logs. Used for errors that should definitely be noted.
93 // Commonly used for hooks to send errors to an error tracking service.
94 ErrorLevel
95 // WarnLevel level. Non-critical entries that deserve eyes.
96 WarnLevel
97 // InfoLevel level. General operational entries about what's going on inside the
98 // application.
99 InfoLevel
100 // DebugLevel level. Usually only enabled when debugging. Very verbose logging.
101 DebugLevel
102 // TraceLevel level. Designates finer-grained informational events than the Debug.
103 TraceLevel
104)
105
106// Won't compile if StdLogger can't be realized by a log.Logger
107var (
108 _ StdLogger = &log.Logger{}
109 _ StdLogger = &Entry{}
110 _ StdLogger = &Logger{}
111)
112
113// StdLogger is what your logrus-enabled library should take, that way
114// it'll accept a stdlib logger and a logrus logger. There's no standard
115// interface, this is the closest we get, unfortunately.
116type StdLogger interface {
117 Print(...interface{})
118 Printf(string, ...interface{})
119 Println(...interface{})
120
121 Fatal(...interface{})
122 Fatalf(string, ...interface{})
123 Fatalln(...interface{})
124
125 Panic(...interface{})
126 Panicf(string, ...interface{})
127 Panicln(...interface{})
128}
129
130// The FieldLogger interface generalizes the Entry and Logger types
131type FieldLogger interface {
132 WithField(key string, value interface{}) *Entry
133 WithFields(fields Fields) *Entry
134 WithError(err error) *Entry
135
136 Debugf(format string, args ...interface{})
137 Infof(format string, args ...interface{})
138 Printf(format string, args ...interface{})
139 Warnf(format string, args ...interface{})
140 Warningf(format string, args ...interface{})
141 Errorf(format string, args ...interface{})
142 Fatalf(format string, args ...interface{})
143 Panicf(format string, args ...interface{})
144
145 Debug(args ...interface{})
146 Info(args ...interface{})
147 Print(args ...interface{})
148 Warn(args ...interface{})
149 Warning(args ...interface{})
150 Error(args ...interface{})
151 Fatal(args ...interface{})
152 Panic(args ...interface{})
153
154 Debugln(args ...interface{})
155 Infoln(args ...interface{})
156 Println(args ...interface{})
157 Warnln(args ...interface{})
158 Warningln(args ...interface{})
159 Errorln(args ...interface{})
160 Fatalln(args ...interface{})
161 Panicln(args ...interface{})
162
163 // IsDebugEnabled() bool
164 // IsInfoEnabled() bool
165 // IsWarnEnabled() bool
166 // IsErrorEnabled() bool
167 // IsFatalEnabled() bool
168 // IsPanicEnabled() bool
169}
170
171// Ext1FieldLogger (the first extension to FieldLogger) is superfluous, it is
172// here for consistancy. Do not use. Use Logger or Entry instead.
173type Ext1FieldLogger interface {
174 FieldLogger
175 Tracef(format string, args ...interface{})
176 Trace(args ...interface{})
177 Traceln(args ...interface{})
178}