mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-02-03 22:18:39 +00:00
Import code, preparing for release
This commit is contained in:
committed by
Cuong Manh Le
parent
cef3cc497e
commit
91d60d2a64
29
log.go
Normal file
29
log.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package ctrld
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
// ProxyLog emits the log record for proxy operations.
|
||||
// The caller should set it only once.
|
||||
var ProxyLog = zerolog.New(io.Discard)
|
||||
|
||||
// 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...))
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user