-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentry.go
More file actions
58 lines (48 loc) · 1.16 KB
/
entry.go
File metadata and controls
58 lines (48 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package logmgr
import (
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
const (
keyUserID = "user_id"
keyGinContext = "gin_context"
)
// Wrapper on a logrus.Entry to provide convenience functions
type logEntry struct {
*logrus.Entry
}
// LoggerName returns the set logger name, or the empty string if not set
func (entry *logEntry) LoggerName() string {
if entry.Data != nil {
if name, ok := entry.Data[keyLoggerName]; ok {
return name.(string)
}
}
return ""
}
// UserID returns the set user id, or the empty string if not set
func (entry *logEntry) UserID() string {
if entry.Context != nil {
if userID := entry.Context.Value(keyUserID); userID != nil {
return userID.(string)
}
}
return ""
}
// GinContext returns the set gin request context, or the nil if not set
func (entry *logEntry) GinContext() *gin.Context {
if entry.Context != nil {
if c := entry.Context.Value(keyGinContext); c != nil {
return c.(*gin.Context)
}
}
return nil
}
// Error returns the set error, or the nil if not set
func (entry *logEntry) Error() error {
if err, ok := entry.Data[logrus.ErrorKey].(error); ok {
return err
} else {
return nil
}
}