-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlogger.go
More file actions
112 lines (91 loc) · 2.35 KB
/
logger.go
File metadata and controls
112 lines (91 loc) · 2.35 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package sdk
import (
"context"
"github.com/sirupsen/logrus"
)
type Level string
const (
LevelTrace Level = "trace"
LevelDebug Level = "debug"
LevelInfo Level = "info"
LevelWarn Level = "warn"
LevelError Level = "error"
LevelFatal Level = "fatal"
LevelPanic Level = "panic"
)
type Logger interface {
SetLevel(level Level)
GetLevel() Level
Entry
}
type Entry interface {
WithError(err error) Entry
WithField(key string, value any) Entry
WithFields(fields map[string]any) Entry
WithContext(ctx context.Context) Entry
Log
}
type Log interface {
Trace(args ...any)
Tracef(format string, args ...any)
Debug(args ...any)
Debugf(format string, args ...any)
Info(args ...any)
Infof(format string, args ...any)
Warn(args ...any)
Warnf(format string, args ...any)
Error(args ...any)
Errorf(format string, args ...any)
Fatal(args ...any)
Fatalf(format string, args ...any)
Panic(args ...any)
Panicf(format string, args ...any)
}
type logger struct {
*logrus.Logger
}
func (l *logger) SetLevel(level Level) {
switch level {
case LevelTrace, LevelDebug, LevelInfo, LevelWarn, LevelError, LevelFatal, LevelPanic:
var lv logrus.Level
if err := lv.UnmarshalText([]byte(level)); err != nil {
l.Logger.SetLevel(logrus.InfoLevel)
}
}
}
func (l *logger) GetLevel() Level {
return Level(l.Logger.GetLevel().String())
}
func (l *logger) WithError(err error) Entry {
return &entry{Entry: l.Logger.WithError(err)}
}
func (l *logger) WithField(key string, value any) Entry {
return &entry{Entry: l.Logger.WithField(key, value)}
}
func (l *logger) WithFields(fields map[string]any) Entry {
return &entry{Entry: l.Logger.WithFields(fields)}
}
func (l *logger) WithContext(ctx context.Context) Entry {
return &entry{Entry: l.Logger.WithContext(ctx)}
}
type entry struct {
*logrus.Entry
}
func (e *entry) WithError(err error) Entry {
return &entry{Entry: e.Entry.WithError(err)}
}
func (e *entry) WithField(key string, value any) Entry {
return &entry{Entry: e.Entry.WithField(key, value)}
}
func (e *entry) WithFields(fields map[string]any) Entry {
return &entry{Entry: e.Entry.WithFields(fields)}
}
func (e *entry) WithContext(ctx context.Context) Entry {
return &entry{Entry: e.Entry.WithContext(ctx)}
}
func DefaultLogger() Logger {
return &logger{Logger: logrus.StandardLogger()}
}
func WrapLogrus(l *logrus.Logger) Logger {
return &logger{Logger: l}
}