Browse Source

Added TRACE level logging.

tags/v1.2.0
drampull 2 years ago
parent
commit
4981d8161c
10 changed files with 81 additions and 3 deletions
  1. 1
    0
      .gitignore
  2. 18
    0
      entry.go
  3. 7
    1
      example_basic_test.go
  4. 15
    0
      exported.go
  5. 2
    0
      hook_test.go
  6. 1
    1
      hooks/syslog/syslog.go
  7. 24
    0
      logger.go
  8. 10
    0
      logrus.go
  9. 1
    1
      text_formatter.go
  10. 2
    0
      writer.go

+ 1
- 0
.gitignore View File

@@ -1 +1,2 @@
1 1
 logrus
2
+vendor

+ 18
- 0
entry.go View File

@@ -168,6 +168,12 @@ func (entry *Entry) Debug(args ...interface{}) {
168 168
 	}
169 169
 }
170 170
 
171
+func (entry *Entry) Trace(args ...interface{}) {
172
+	if entry.Logger.IsLevelEnabled(TraceLevel) {
173
+		entry.log(TraceLevel, fmt.Sprint(args...))
174
+	}
175
+}
176
+
171 177
 func (entry *Entry) Print(args ...interface{}) {
172 178
 	entry.Info(args...)
173 179
 }
@@ -216,6 +222,12 @@ func (entry *Entry) Debugf(format string, args ...interface{}) {
216 222
 	}
217 223
 }
218 224
 
225
+func (entry *Entry) Tracef(format string, args ...interface{}) {
226
+	if entry.Logger.IsLevelEnabled(TraceLevel) {
227
+		entry.Trace(fmt.Sprintf(format, args...))
228
+	}
229
+}
230
+
219 231
 func (entry *Entry) Infof(format string, args ...interface{}) {
220 232
 	if entry.Logger.IsLevelEnabled(InfoLevel) {
221 233
 		entry.Info(fmt.Sprintf(format, args...))
@@ -257,6 +269,12 @@ func (entry *Entry) Panicf(format string, args ...interface{}) {
257 269
 
258 270
 // Entry Println family functions
259 271
 
272
+func (entry *Entry) Traceln(args ...interface{}) {
273
+	if entry.Logger.IsLevelEnabled(TraceLevel) {
274
+		entry.Trace(entry.sprintlnn(args...))
275
+	}
276
+}
277
+
260 278
 func (entry *Entry) Debugln(args ...interface{}) {
261 279
 	if entry.Logger.IsLevelEnabled(DebugLevel) {
262 280
 		entry.Debug(entry.sprintlnn(args...))

+ 7
- 1
example_basic_test.go View File

@@ -12,7 +12,7 @@ func Example_basic() {
12 12
 	log.Formatter = new(logrus.TextFormatter)                     //default
13 13
 	log.Formatter.(*logrus.TextFormatter).DisableColors = true    // remove colors
14 14
 	log.Formatter.(*logrus.TextFormatter).DisableTimestamp = true // remove timestamp from test output
15
-	log.Level = logrus.DebugLevel
15
+	log.Level = logrus.TraceLevel
16 16
 	log.Out = os.Stdout
17 17
 
18 18
 	// file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY, 0666)
@@ -37,6 +37,11 @@ func Example_basic() {
37 37
 		}
38 38
 	}()
39 39
 
40
+	log.WithFields(logrus.Fields{
41
+		"animal": "walrus",
42
+		"number": 0,
43
+	}).Trace("Went to the beach")
44
+
40 45
 	log.WithFields(logrus.Fields{
41 46
 		"animal": "walrus",
42 47
 		"number": 8,
@@ -62,6 +67,7 @@ func Example_basic() {
62 67
 	}).Panic("It's over 9000!")
63 68
 
64 69
 	// Output:
70
+	// level=trace msg="Went to the beach" animal=walrus number=0
65 71
 	// level=debug msg="Started observing beach" animal=walrus number=8
66 72
 	// level=info msg="A group of walrus emerges from the ocean" animal=walrus size=10
67 73
 	// level=warning msg="The group's number increased tremendously!" number=122 omg=true

+ 15
- 0
exported.go View File

@@ -77,6 +77,11 @@ func WithTime(t time.Time) *Entry {
77 77
 	return std.WithTime(t)
78 78
 }
79 79
 
80
+// Trace logs a message at level Trace on the standard logger.
81
+func Trace(args ...interface{}) {
82
+	std.Trace(args...)
83
+}
84
+
80 85
 // Debug logs a message at level Debug on the standard logger.
81 86
 func Debug(args ...interface{}) {
82 87
 	std.Debug(args...)
@@ -117,6 +122,11 @@ func Fatal(args ...interface{}) {
117 122
 	std.Fatal(args...)
118 123
 }
119 124
 
125
+// Tracef logs a message at level Debug on the standard logger.
126
+func Tracef(format string, args ...interface{}) {
127
+	std.Tracef(format, args...)
128
+}
129
+
120 130
 // Debugf logs a message at level Debug on the standard logger.
121 131
 func Debugf(format string, args ...interface{}) {
122 132
 	std.Debugf(format, args...)
@@ -157,6 +167,11 @@ func Fatalf(format string, args ...interface{}) {
157 167
 	std.Fatalf(format, args...)
158 168
 }
159 169
 
170
+// Traceln logs a message at level Debug on the standard logger.
171
+func Traceln(args ...interface{}) {
172
+	std.Traceln(args...)
173
+}
174
+
160 175
 // Debugln logs a message at level Debug on the standard logger.
161 176
 func Debugln(args ...interface{}) {
162 177
 	std.Debugln(args...)

+ 2
- 0
hook_test.go View File

@@ -21,6 +21,7 @@ func (hook *TestHook) Fire(entry *Entry) error {
21 21
 
22 22
 func (hook *TestHook) Levels() []Level {
23 23
 	return []Level{
24
+		TraceLevel,
24 25
 		DebugLevel,
25 26
 		InfoLevel,
26 27
 		WarnLevel,
@@ -53,6 +54,7 @@ func (hook *ModifyHook) Fire(entry *Entry) error {
53 54
 
54 55
 func (hook *ModifyHook) Levels() []Level {
55 56
 	return []Level{
57
+		TraceLevel,
56 58
 		DebugLevel,
57 59
 		InfoLevel,
58 60
 		WarnLevel,

+ 1
- 1
hooks/syslog/syslog.go View File

@@ -43,7 +43,7 @@ func (hook *SyslogHook) Fire(entry *logrus.Entry) error {
43 43
 		return hook.Writer.Warning(line)
44 44
 	case logrus.InfoLevel:
45 45
 		return hook.Writer.Info(line)
46
-	case logrus.DebugLevel:
46
+	case logrus.DebugLevel, logrus.TraceLevel:
47 47
 		return hook.Writer.Debug(line)
48 48
 	default:
49 49
 		return nil

+ 24
- 0
logger.go View File

@@ -126,6 +126,14 @@ func (logger *Logger) WithTime(t time.Time) *Entry {
126 126
 	return entry.WithTime(t)
127 127
 }
128 128
 
129
+func (logger *Logger) Tracef(format string, args ...interface{}) {
130
+	if logger.IsLevelEnabled(TraceLevel) {
131
+		entry := logger.newEntry()
132
+		entry.Tracef(format, args...)
133
+		logger.releaseEntry(entry)
134
+	}
135
+}
136
+
129 137
 func (logger *Logger) Debugf(format string, args ...interface{}) {
130 138
 	if logger.IsLevelEnabled(DebugLevel) {
131 139
 		entry := logger.newEntry()
@@ -189,6 +197,14 @@ func (logger *Logger) Panicf(format string, args ...interface{}) {
189 197
 	}
190 198
 }
191 199
 
200
+func (logger *Logger) Trace(args ...interface{}) {
201
+	if logger.IsLevelEnabled(TraceLevel) {
202
+		entry := logger.newEntry()
203
+		entry.Trace(args...)
204
+		logger.releaseEntry(entry)
205
+	}
206
+}
207
+
192 208
 func (logger *Logger) Debug(args ...interface{}) {
193 209
 	if logger.IsLevelEnabled(DebugLevel) {
194 210
 		entry := logger.newEntry()
@@ -252,6 +268,14 @@ func (logger *Logger) Panic(args ...interface{}) {
252 268
 	}
253 269
 }
254 270
 
271
+func (logger *Logger) Traceln(args ...interface{}) {
272
+	if logger.IsLevelEnabled(TraceLevel) {
273
+		entry := logger.newEntry()
274
+		entry.Traceln(args...)
275
+		logger.releaseEntry(entry)
276
+	}
277
+}
278
+
255 279
 func (logger *Logger) Debugln(args ...interface{}) {
256 280
 	if logger.IsLevelEnabled(DebugLevel) {
257 281
 		entry := logger.newEntry()

+ 10
- 0
logrus.go View File

@@ -15,6 +15,8 @@ type Level uint32
15 15
 // Convert the Level to a string. E.g. PanicLevel becomes "panic".
16 16
 func (level Level) String() string {
17 17
 	switch level {
18
+	case TraceLevel:
19
+		return "trace"
18 20
 	case DebugLevel:
19 21
 		return "debug"
20 22
 	case InfoLevel:
@@ -47,6 +49,8 @@ func ParseLevel(lvl string) (Level, error) {
47 49
 		return InfoLevel, nil
48 50
 	case "debug":
49 51
 		return DebugLevel, nil
52
+	case "trace":
53
+		return TraceLevel, nil
50 54
 	}
51 55
 
52 56
 	var l Level
@@ -61,6 +65,7 @@ var AllLevels = []Level{
61 65
 	WarnLevel,
62 66
 	InfoLevel,
63 67
 	DebugLevel,
68
+	TraceLevel,
64 69
 }
65 70
 
66 71
 // These are the different logging levels. You can set the logging level to log
@@ -82,6 +87,8 @@ const (
82 87
 	InfoLevel
83 88
 	// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
84 89
 	DebugLevel
90
+	// TraceLevel level. Usually only enabled when debugging. Very verbose logging.  Usually reserved for message traces
91
+	TraceLevel
85 92
 )
86 93
 
87 94
 // Won't compile if StdLogger can't be realized by a log.Logger
@@ -114,6 +121,7 @@ type FieldLogger interface {
114 121
 	WithFields(fields Fields) *Entry
115 122
 	WithError(err error) *Entry
116 123
 
124
+	Tracef(format string, args ...interface{})
117 125
 	Debugf(format string, args ...interface{})
118 126
 	Infof(format string, args ...interface{})
119 127
 	Printf(format string, args ...interface{})
@@ -123,6 +131,7 @@ type FieldLogger interface {
123 131
 	Fatalf(format string, args ...interface{})
124 132
 	Panicf(format string, args ...interface{})
125 133
 
134
+	Trace(args ...interface{})
126 135
 	Debug(args ...interface{})
127 136
 	Info(args ...interface{})
128 137
 	Print(args ...interface{})
@@ -132,6 +141,7 @@ type FieldLogger interface {
132 141
 	Fatal(args ...interface{})
133 142
 	Panic(args ...interface{})
134 143
 
144
+	Traceln(args ...interface{})
135 145
 	Debugln(args ...interface{})
136 146
 	Infoln(args ...interface{})
137 147
 	Println(args ...interface{})

+ 1
- 1
text_formatter.go View File

@@ -183,7 +183,7 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
183 183
 func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []string, timestampFormat string) {
184 184
 	var levelColor int
185 185
 	switch entry.Level {
186
-	case DebugLevel:
186
+	case DebugLevel, TraceLevel:
187 187
 		levelColor = gray
188 188
 	case WarnLevel:
189 189
 		levelColor = yellow

+ 2
- 0
writer.go View File

@@ -24,6 +24,8 @@ func (entry *Entry) WriterLevel(level Level) *io.PipeWriter {
24 24
 	var printFunc func(args ...interface{})
25 25
 
26 26
 	switch level {
27
+	case TraceLevel:
28
+		printFunc = entry.Trace
27 29
 	case DebugLevel:
28 30
 		printFunc = entry.Debug
29 31
 	case InfoLevel:

Loading…
Cancel
Save