good morning!!!!

Skip to content
Snippets Groups Projects
Verified Commit 0225bc58 authored by a's avatar a
Browse files

change log package

parent 51952f2e
No related branches found
No related tags found
No related merge requests found
......@@ -2,11 +2,11 @@ package middleware
import (
"context"
"gfx.cafe/open/jrpc/pkg/codec"
"time"
"tuxpa.in/a/zlog"
"tuxpa.in/a/zlog/log"
"gfx.cafe/open/jrpc/pkg/codec"
"log/slog"
)
// Key to use when setting the request ID.
......@@ -15,29 +15,41 @@ type ctxKeyLogger int
// RequestIDKey is the key that holds the unique request ID in a request context.
const LoggerKey ctxKeyLogger = 76
func Logger(next codec.Handler) codec.Handler {
fn := func(w codec.ResponseWriter, r *codec.Request) {
start := time.Now()
l := log.Trace().
Str("remote", r.Remote()).
Str("method", r.Method).
Str("params", string(r.Msg().Params))
next.ServeRPC(w, r.WithContext(context.WithValue(r.Context(), LoggerKey, l)))
if id := GetReqID(r.Context()); id != "" {
l = l.Str("id", id)
func NewLogger(logger *slog.Logger) func(next codec.Handler) codec.Handler {
return func(next codec.Handler) codec.Handler {
fn := func(w codec.ResponseWriter, r *codec.Request) {
start := time.Now()
lg := logger.With(
"remote", r.Remote(),
"method", r.Method,
"params", string(r.Msg().Params),
)
if id := GetReqID(r.Context()); id != "" {
lg = logger.With(
"req_id", id,
)
}
next.ServeRPC(w, r.WithContext(context.WithValue(r.Context(), LoggerKey, lg)))
lg = logger.With(
"params", time.Since(start),
)
logger.LogAttrs(r.Context(), slog.LevelDebug, "RPC Request")
}
l = l.Stringer("dur", time.Since(start))
l.Msg("RPC Request")
return codec.HandlerFunc(fn)
}
return codec.HandlerFunc(fn)
}
func GetLogger(ctx context.Context) *zlog.Event {
func Logger(next codec.Handler) codec.Handler {
lh := slog.Default()
return NewLogger(lh)(next)
}
func GetLogger(ctx context.Context) *slog.Logger {
if ctx == nil {
return log.Debug()
return slog.Default()
}
if lgr, ok := ctx.Value(LoggerKey).(*zlog.Event); ok {
if lgr, ok := ctx.Value(LoggerKey).(*slog.Logger); ok {
return lgr
}
return log.Debug()
return slog.Default()
}
module gfx.cafe/open/jrpc
go 1.18
go 1.21
require (
gfx.cafe/open/websocket v1.9.2
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment