-
Notifications
You must be signed in to change notification settings - Fork 22
/
log.go
39 lines (32 loc) · 920 Bytes
/
log.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
package ctrld
import (
"context"
"fmt"
"io"
"sync/atomic"
"github.com/rs/zerolog"
)
func init() {
l := zerolog.New(io.Discard)
ProxyLogger.Store(&l)
}
// ProxyLog emits the log record for proxy operations.
// The caller should set it only once.
// DEPRECATED: use ProxyLogger instead.
var ProxyLog = zerolog.New(io.Discard)
// ProxyLogger emits the log record for proxy operations.
var ProxyLogger atomic.Pointer[zerolog.Logger]
// ReqIdCtxKey is the context.Context key for a request id.
type ReqIdCtxKey struct{}
// Log emits the logs for a particular zerolog event.
// The request id associated with the context will be included if presents.
func Log(ctx context.Context, e *zerolog.Event, format string, v ...any) {
id, ok := ctx.Value(ReqIdCtxKey{}).(string)
if !ok {
e.Msgf(format, v...)
return
}
e.MsgFunc(func() string {
return fmt.Sprintf("[%s] %s", id, fmt.Sprintf(format, v...))
})
}