generated from logur/adapter-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
144 lines (115 loc) · 3.27 KB
/
logger.go
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Package logrus provides a Logur adapter for Logrus.
package logrus
import (
"context"
"github.com/sirupsen/logrus"
"logur.dev/logur"
)
// Logger is a Logur adapter for Logrus.
type Logger struct {
entry *logrus.Entry
}
// New returns a new Logur logger.
// If logger is nil, a default instance is created.
func New(logger *logrus.Logger) *Logger {
if logger == nil {
return NewFromEntry(nil)
}
return NewFromEntry(logrus.NewEntry(logger))
}
// NewFromEntry returns a new Logur logger from a Logrus entry.
// If entry is nil, a default instance is created.
func NewFromEntry(entry *logrus.Entry) *Logger {
if entry == nil {
entry = logrus.NewEntry(logrus.StandardLogger())
}
return &Logger{
entry: entry,
}
}
// Trace implements the Logur Logger interface.
func (l *Logger) Trace(msg string, fields ...map[string]interface{}) {
if !l.entry.Logger.IsLevelEnabled(logrus.TraceLevel) {
return
}
var entry = l.entry
if len(fields) > 0 {
entry = entry.WithFields(fields[0])
}
entry.Trace(msg)
}
// Debug implements the Logur Logger interface.
func (l *Logger) Debug(msg string, fields ...map[string]interface{}) {
if !l.entry.Logger.IsLevelEnabled(logrus.DebugLevel) {
return
}
var entry = l.entry
if len(fields) > 0 {
entry = entry.WithFields(fields[0])
}
entry.Debug(msg)
}
// Info implements the Logur Logger interface.
func (l *Logger) Info(msg string, fields ...map[string]interface{}) {
if !l.entry.Logger.IsLevelEnabled(logrus.InfoLevel) {
return
}
var entry = l.entry
if len(fields) > 0 {
entry = entry.WithFields(fields[0])
}
entry.Info(msg)
}
// Warn implements the Logur Logger interface.
func (l *Logger) Warn(msg string, fields ...map[string]interface{}) {
if !l.entry.Logger.IsLevelEnabled(logrus.WarnLevel) {
return
}
var entry = l.entry
if len(fields) > 0 {
entry = entry.WithFields(fields[0])
}
entry.Warn(msg)
}
// Error implements the Logur Logger interface.
func (l *Logger) Error(msg string, fields ...map[string]interface{}) {
if !l.entry.Logger.IsLevelEnabled(logrus.ErrorLevel) {
return
}
var entry = l.entry
if len(fields) > 0 {
entry = entry.WithFields(fields[0])
}
entry.Error(msg)
}
func (l *Logger) TraceContext(_ context.Context, msg string, fields ...map[string]interface{}) {
l.Trace(msg, fields...)
}
func (l *Logger) DebugContext(_ context.Context, msg string, fields ...map[string]interface{}) {
l.Debug(msg, fields...)
}
func (l *Logger) InfoContext(_ context.Context, msg string, fields ...map[string]interface{}) {
l.Info(msg, fields...)
}
func (l *Logger) WarnContext(_ context.Context, msg string, fields ...map[string]interface{}) {
l.Warn(msg, fields...)
}
func (l *Logger) ErrorContext(_ context.Context, msg string, fields ...map[string]interface{}) {
l.Error(msg, fields...)
}
// LevelEnabled implements the Logur LevelEnabler interface.
func (l *Logger) LevelEnabled(level logur.Level) bool {
switch level {
case logur.Trace:
return l.entry.Logger.IsLevelEnabled(logrus.TraceLevel)
case logur.Debug:
return l.entry.Logger.IsLevelEnabled(logrus.DebugLevel)
case logur.Info:
return l.entry.Logger.IsLevelEnabled(logrus.InfoLevel)
case logur.Warn:
return l.entry.Logger.IsLevelEnabled(logrus.WarnLevel)
case logur.Error:
return l.entry.Logger.IsLevelEnabled(logrus.ErrorLevel)
}
return true
}