...
1 package logrus
2
3 import (
4 "fmt"
5 "log"
6 "strings"
7 )
8
9
10 type Fields map[string]interface{}
11
12
13 type Level uint32
14
15
16 func (level Level) String() string {
17 if b, err := level.MarshalText(); err == nil {
18 return string(b)
19 } else {
20 return "unknown"
21 }
22 }
23
24
25 func ParseLevel(lvl string) (Level, error) {
26 switch strings.ToLower(lvl) {
27 case "panic":
28 return PanicLevel, nil
29 case "fatal":
30 return FatalLevel, nil
31 case "error":
32 return ErrorLevel, nil
33 case "warn", "warning":
34 return WarnLevel, nil
35 case "info":
36 return InfoLevel, nil
37 case "debug":
38 return DebugLevel, nil
39 case "trace":
40 return TraceLevel, nil
41 }
42
43 var l Level
44 return l, fmt.Errorf("not a valid logrus Level: %q", lvl)
45 }
46
47
48 func (level *Level) UnmarshalText(text []byte) error {
49 l, err := ParseLevel(string(text))
50 if err != nil {
51 return err
52 }
53
54 *level = Level(l)
55
56 return nil
57 }
58
59 func (level Level) MarshalText() ([]byte, error) {
60 switch level {
61 case TraceLevel:
62 return []byte("trace"), nil
63 case DebugLevel:
64 return []byte("debug"), nil
65 case InfoLevel:
66 return []byte("info"), nil
67 case WarnLevel:
68 return []byte("warning"), nil
69 case ErrorLevel:
70 return []byte("error"), nil
71 case FatalLevel:
72 return []byte("fatal"), nil
73 case PanicLevel:
74 return []byte("panic"), nil
75 }
76
77 return nil, fmt.Errorf("not a valid lorus level %q", level)
78 }
79
80
81 var AllLevels = []Level{
82 PanicLevel,
83 FatalLevel,
84 ErrorLevel,
85 WarnLevel,
86 InfoLevel,
87 DebugLevel,
88 TraceLevel,
89 }
90
91
92
93 const (
94
95
96 PanicLevel Level = iota
97
98
99 FatalLevel
100
101
102 ErrorLevel
103
104 WarnLevel
105
106
107 InfoLevel
108
109 DebugLevel
110
111 TraceLevel
112 )
113
114
115 var (
116 _ StdLogger = &log.Logger{}
117 _ StdLogger = &Entry{}
118 _ StdLogger = &Logger{}
119 )
120
121
122
123
124 type StdLogger interface {
125 Print(...interface{})
126 Printf(string, ...interface{})
127 Println(...interface{})
128
129 Fatal(...interface{})
130 Fatalf(string, ...interface{})
131 Fatalln(...interface{})
132
133 Panic(...interface{})
134 Panicf(string, ...interface{})
135 Panicln(...interface{})
136 }
137
138
139 type FieldLogger interface {
140 WithField(key string, value interface{}) *Entry
141 WithFields(fields Fields) *Entry
142 WithError(err error) *Entry
143
144 Debugf(format string, args ...interface{})
145 Infof(format string, args ...interface{})
146 Printf(format string, args ...interface{})
147 Warnf(format string, args ...interface{})
148 Warningf(format string, args ...interface{})
149 Errorf(format string, args ...interface{})
150 Fatalf(format string, args ...interface{})
151 Panicf(format string, args ...interface{})
152
153 Debug(args ...interface{})
154 Info(args ...interface{})
155 Print(args ...interface{})
156 Warn(args ...interface{})
157 Warning(args ...interface{})
158 Error(args ...interface{})
159 Fatal(args ...interface{})
160 Panic(args ...interface{})
161
162 Debugln(args ...interface{})
163 Infoln(args ...interface{})
164 Println(args ...interface{})
165 Warnln(args ...interface{})
166 Warningln(args ...interface{})
167 Errorln(args ...interface{})
168 Fatalln(args ...interface{})
169 Panicln(args ...interface{})
170
171
172
173
174
175
176
177 }
178
179
180
181 type Ext1FieldLogger interface {
182 FieldLogger
183 Tracef(format string, args ...interface{})
184 Trace(args ...interface{})
185 Traceln(args ...interface{})
186 }
187
View as plain text