-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
184 lines (145 loc) · 3.25 KB
/
proxy.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package logrus
import (
"context"
"fmt"
"log/slog"
)
// Proxy is a proxy type for logrus.Logger
type Proxy struct {
dest *slog.Logger
ctx context.Context
}
var proxy *Proxy
// NewProxy creates a new Proxy
func NewProxyFor(logger *slog.Logger) *Proxy {
return &Proxy{
dest: logger.With(slog.Bool("logrus", true)),
}
}
func NewProxy() *Proxy {
return NewProxyFor(slog.Default())
}
func SetDefault(p *Proxy) {
proxy = p
}
func (p *Proxy) WithContext(ctx context.Context) *Proxy {
p.ctx = ctx
return p
}
// WithField converts all values to strings
func (p *Proxy) WithField(key string, value any) *Proxy {
return &Proxy{
dest: p.dest.With(slog.String(key, fmt.Sprint(value))),
ctx: p.ctx,
}
}
func (p *Proxy) Trace(args ...any) {
if p.ctx != nil {
p.dest.Debug(fmt.Sprint(args...))
} else {
p.dest.DebugContext(p.ctx, fmt.Sprint(args...))
}
}
func (p *Proxy) Debug(args ...any) {
if p.ctx != nil {
p.dest.Debug(fmt.Sprint(args...))
} else {
p.dest.DebugContext(p.ctx, fmt.Sprint(args...))
}
}
func (p *Proxy) Info(args ...any) {
if p.ctx != nil {
p.dest.Info(fmt.Sprint(args...))
} else {
p.dest.InfoContext(p.ctx, fmt.Sprint(args...))
}
}
func (p *Proxy) Warn(args ...any) {
if p.ctx != nil {
p.dest.Warn(fmt.Sprint(args...))
} else {
p.dest.WarnContext(p.ctx, fmt.Sprint(args...))
}
}
func (p *Proxy) Error(args ...any) {
if p.ctx != nil {
p.dest.Error(fmt.Sprint(args...))
} else {
p.dest.ErrorContext(p.ctx, fmt.Sprint(args...))
}
}
func (p *Proxy) Fatal(args ...any) {
p.Error(args...)
}
func (p *Proxy) Panic(args ...any) {
p.Error(args...)
}
func (p *Proxy) Tracef(format string, args ...any) {
p.Debug(fmt.Sprintf(format, args...))
}
func (p *Proxy) Debugf(format string, args ...any) {
p.Debug(fmt.Sprintf(format, args...))
}
func (p *Proxy) Infof(format string, args ...any) {
p.Info(fmt.Sprintf(format, args...))
}
func (p *Proxy) Warnf(format string, args ...any) {
p.Warn(fmt.Sprintf(format, args...))
}
func (p *Proxy) Errorf(format string, args ...any) {
p.Error(fmt.Sprintf(format, args...))
}
func (p *Proxy) Fatalf(format string, args ...any) {
p.Fatal(fmt.Sprintf(format, args...))
}
func (p *Proxy) Panicf(format string, args ...any) {
p.Panic(fmt.Sprintf(format, args...))
}
func Trace(args ...any) {
proxy.Trace(args...)
}
func Debug(args ...any) {
proxy.Debug(args...)
}
func Info(args ...any) {
proxy.Info(args...)
}
func Warn(args ...any) {
proxy.Warn(args...)
}
func Error(args ...any) {
proxy.Error(args...)
}
func Fatal(args ...any) {
proxy.Fatal(args...)
}
func Panic(args ...any) {
proxy.Panic(args...)
}
func Tracef(format string, args ...any) {
proxy.Tracef(format, args...)
}
func Debugf(format string, args ...any) {
proxy.Debugf(format, args...)
}
func Infof(format string, args ...any) {
proxy.Infof(format, args...)
}
func Warnf(format string, args ...any) {
proxy.Warnf(format, args...)
}
func Errorf(format string, args ...any) {
proxy.Errorf(format, args...)
}
func Fatalf(format string, args ...any) {
proxy.Fatalf(format, args...)
}
func Panicf(format string, args ...any) {
proxy.Panicf(format, args...)
}
func WithField(key string, value any) *Proxy {
return proxy.WithField(key, value)
}
func WithContext(ctx context.Context) *Proxy {
return proxy.WithContext(ctx)
}