-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add draft logr interoperability methods
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module github.com/veqryn/slog-context/logr-context | ||
|
||
go 1.21 | ||
|
||
require ( | ||
github.com/go-logr/logr v1.3.0 | ||
github.com/veqryn/slog-context v0.3.1-0.20231126110827-74878698ea2b | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY= | ||
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= | ||
github.com/veqryn/slog-context v0.3.1-0.20231126110827-74878698ea2b h1:BG6I67Hp571eSmcc3s8O0XAxSAUg3WHSiWPoMzeV91E= | ||
github.com/veqryn/slog-context v0.3.1-0.20231126110827-74878698ea2b/go.mod h1:2IEM+uLEBRZyq+CRMdpETc13tUj9aA7zFZnm6/YnvPQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package logrcontext | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/go-logr/logr/slogr" | ||
"github.com/veqryn/slog-context/internal" | ||
) | ||
|
||
func LogrToCtx(parent context.Context, logger logr.Logger) context.Context { | ||
if parent == nil { | ||
parent = context.Background() | ||
} | ||
return context.WithValue(parent, internal.CtxKey{}, internal.LoggerCtxVal{ | ||
Logger: slog.New(slogr.NewSlogHandler(logger)), | ||
IterOp: logger, | ||
}) | ||
} | ||
|
||
func SlogToCtx(parent context.Context, logger *slog.Logger) context.Context { | ||
if parent == nil { | ||
parent = context.Background() | ||
} | ||
if logger == nil { | ||
return context.WithValue(parent, internal.CtxKey{}, internal.LoggerCtxVal{Logger: logger}) | ||
} | ||
return context.WithValue(parent, internal.CtxKey{}, internal.LoggerCtxVal{ | ||
Logger: logger, | ||
IterOp: slogr.NewLogr(logger.Handler()), | ||
}) | ||
} | ||
|
||
func LogrFromCtx(ctx context.Context) logr.Logger { | ||
if ctx == nil { | ||
return slogr.NewLogr(slog.Default().Handler()) | ||
} | ||
if l, ok := ctx.Value(internal.CtxKey{}).(internal.LoggerCtxVal); ok { | ||
if lgr, hasLogr := l.IterOp.(logr.Logger); hasLogr { | ||
return lgr | ||
} | ||
if l.Logger != nil { | ||
return slogr.NewLogr(l.Logger.Handler()) | ||
} | ||
} | ||
return slogr.NewLogr(slog.Default().Handler()) | ||
} |