-
Notifications
You must be signed in to change notification settings - Fork 11
/
adapt.go
222 lines (193 loc) · 5.71 KB
/
adapt.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package log
import (
"context"
"fmt"
"sync"
"github.com/aws/smithy-go/logging"
"github.com/go-logr/logr"
"goa.design/goa/v3/middleware"
)
type (
// StdLogger implements an interface compatible with the stdlib log package.
StdLogger struct {
ctx context.Context
}
// AWSLogger returns an AWS SDK compatible logger.
AWSLogger struct {
context.Context
}
// LogrSink returns a logr LogSink compatible logger.
LogrSink struct {
lock sync.Mutex
name string
context.Context
}
// goaLogger is a Goa middleware compatible logger.
goaLogger struct {
context.Context
}
)
// NameKey is the key used to log the name of the logger.
const NameKey = "log"
// AsGoaMiddlewareLogger creates a Goa middleware compatible logger that can be used when
// configuring Goa HTTP or gRPC servers.
//
// Usage:
//
// // HTTP server:
// import goahttp "goa.design/goa/v3/http"
// import httpmdlwr "goa.design/goa/v3/http/middleware"
// ...
// mux := goahttp.NewMuxer()
// handler := httpmdlwr.LogContext(log.AsGoaMiddlewareLogger)(mux)
//
// // gRPC server:
// import "google.golang.org/grpc"
// import grpcmiddleware "github.com/grpc-ecosystem/go-grpc-middleware"
// import grpcmdlwr "goa.design/goa/v3/grpc/middleware"
// ...
// srv := grpc.NewServer(
// grpcmiddleware.WithUnaryServerChain(grpcmdlwr.UnaryServerLogContext(log.AsGoaMiddlewareLogger)),
// )
func AsGoaMiddlewareLogger(ctx context.Context) middleware.Logger {
return goaLogger{ctx}
}
// AsStdLogger adapts a Goa logger to a stdlib compatible logger.
func AsStdLogger(ctx context.Context) *StdLogger {
return &StdLogger{ctx}
}
// AsAWSLogger returns an AWS SDK compatible logger.
//
// Usage:
//
// import "github.com/aws/aws-sdk-go-v2/config"
// import "goa.design/clue/log"
//
// ctx := log.Context(context.Background())
// httpc := &http.Client{Transport: otelhttp.NewTransport(http.DefaultTransport)}
// cfg, err := config.LoadDefaultConfig(ctx,
// config.WithHTTPClient(httpc),
// config.WithLogger(log.AsAWSLogger(ctx)))
func AsAWSLogger(ctx context.Context) *AWSLogger {
return &AWSLogger{ctx}
}
// ToLogrSink returns a logr.LogSink.
//
// Usage:
//
// import "goa.design/clue/log"
//
// ctx := log.Context(context.Background())
// sink := log.ToLogrSink(ctx)
// logger := logr.New(sink)
func ToLogrSink(ctx context.Context) *LogrSink {
return &LogrSink{Context: ctx}
}
// Fatal is equivalent to l.Print() followed by a call to os.Exit(1).
func (l *StdLogger) Fatal(v ...interface{}) {
l.Print(v...)
osExit(1)
}
// Fatalf is equivalent to l.Printf() followed by a call to os.Exit(1).
func (l *StdLogger) Fatalf(format string, v ...interface{}) {
l.Printf(format, v...)
osExit(1)
}
// Fatalln is equivalent to l.Println() followed by a call to os.Exit(1).
func (l *StdLogger) Fatalln(v ...interface{}) {
l.Println(v...)
osExit(1)
}
// Panic is equivalent to l.Print() followed by a call to panic().
func (l *StdLogger) Panic(v ...interface{}) {
l.Print(v...)
panic(fmt.Sprint(v...))
}
// Panicf is equivalent to l.Printf() followed by a call to panic().
func (l *StdLogger) Panicf(format string, v ...interface{}) {
l.Printf(format, v...)
panic(fmt.Sprintf(format, v...))
}
// Panicln is equivalent to l.Println() followed by a call to panic().
func (l *StdLogger) Panicln(v ...interface{}) {
l.Println(v...)
panic(fmt.Sprintln(v...))
}
// Print print to the logger. Arguments are handled in the manner of fmt.Print.
func (l *StdLogger) Print(v ...interface{}) {
Printf(l.ctx, "%s", fmt.Sprint(v...))
}
// Printf prints to the logger. Arguments are handled in the manner of fmt.Printf.
func (l *StdLogger) Printf(format string, v ...interface{}) {
Printf(l.ctx, format, v...)
}
// Println prints to the logger. Arguments are handled in the manner of fmt.Println.
func (l *StdLogger) Println(v ...interface{}) {
Printf(l.ctx, "%s", fmt.Sprintln(v...))
}
func (l *AWSLogger) Logf(classification logging.Classification, format string, v ...any) {
fn := Infof
if classification == logging.Debug {
fn = Debugf
}
fn(l, format, v...)
}
func (l *AWSLogger) WithContext(ctx context.Context) logging.Logger {
l.Context = WithContext(ctx, l)
return l
}
func (l *LogrSink) Init(info logr.RuntimeInfo) {}
func (l *LogrSink) Enabled(level int) bool {
return true
}
func (l *LogrSink) Info(level int, msg string, keysAndValues ...interface{}) {
kvs := make([]KV, len(keysAndValues)/2+1)
kvs[0] = KV{K: "msg", V: msg}
for i := 0; i < len(keysAndValues); i += 2 {
kvs[i/2+1] = KV{K: fmt.Sprint(keysAndValues[i]), V: keysAndValues[i+1]}
}
if level == 0 {
Info(l, kvList(kvs))
} else {
Debug(l, kvList(kvs))
}
}
func (l *LogrSink) Error(err error, msg string, keysAndValues ...interface{}) {
kvs := make([]KV, len(keysAndValues)/2+1)
kvs[0] = KV{K: "msg", V: msg}
for i := 0; i < len(keysAndValues); i += 2 {
kvs[i/2+1] = KV{K: fmt.Sprint(keysAndValues[i]), V: keysAndValues[i+1]}
}
Error(l, err, kvList(kvs))
}
func (l *LogrSink) WithValues(keysAndValues ...any) logr.LogSink {
kvs := make([]KV, len(keysAndValues)/2)
for i := 0; i < len(keysAndValues); i += 2 {
kvs[i/2] = KV{K: fmt.Sprint(keysAndValues[i]), V: keysAndValues[i+1]}
}
return &LogrSink{Context: With(l, kvList(kvs))}
}
func (l *LogrSink) WithName(name string) logr.LogSink {
l.lock.Lock()
defer l.lock.Unlock()
cur := l.name
if cur != "" {
cur += "/"
}
l.name = cur + name
return &LogrSink{Context: With(l, KV{NameKey, l.name}), name: l.name}
}
// Log creates a log entry using a sequence of key/value pairs.
func (l goaLogger) Log(keyvals ...interface{}) error {
n := (len(keyvals) + 1) / 2
if len(keyvals)%2 != 0 {
keyvals = append(keyvals, "MISSING")
}
kvs := make([]KV, n)
for i := 0; i < n; i++ {
k, v := keyvals[2*i], keyvals[2*i+1]
kvs[i] = KV{K: fmt.Sprint(k), V: v}
}
Print(l, kvList(kvs))
return nil
}