mirror of
https://github.com/chainguard-dev/clog
synced 2026-07-06 22:12:46 +00:00
Update godoc
This commit is contained in:
parent
3f156fd3a4
commit
0d562667b2
5 changed files with 58 additions and 5 deletions
44
example_test.go
Normal file
44
example_test.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
package slogctx_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"os"
|
||||
|
||||
"github.com/wlynch/slogctx"
|
||||
"github.com/wlynch/slogctx/slogtest"
|
||||
)
|
||||
|
||||
func ExampleHandler() {
|
||||
log := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
||||
// Remove time for repeatable results
|
||||
ReplaceAttr: slogtest.RemoveTime,
|
||||
}))
|
||||
|
||||
ctx := context.Background()
|
||||
ctx = slogctx.WithValues(ctx, "foo", "bar")
|
||||
log.InfoContext(ctx, "hello world", slog.Bool("baz", true))
|
||||
|
||||
// Output:
|
||||
// level=INFO msg="hello world" baz=true
|
||||
}
|
||||
|
||||
func ExampleLogger() {
|
||||
log := slogctx.NewLogger(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
||||
// Remove time for repeatable results
|
||||
ReplaceAttr: slogtest.RemoveTime,
|
||||
})))
|
||||
log = log.With("a", "b")
|
||||
ctx := slogctx.WithLogger(context.Background(), log)
|
||||
|
||||
// Grab logger from context and use
|
||||
// Note: this is a formatter aware method, not an slog.Attr method.
|
||||
slogctx.FromContext(ctx).With("foo", "bar").Infof("hello %s", "world")
|
||||
|
||||
// Package level context loggers are also aware
|
||||
slogctx.ErrorContext(ctx, "asdf", slog.Bool("baz", true))
|
||||
|
||||
// Output:
|
||||
// level=INFO msg="hello world" a=b foo=bar
|
||||
// level=ERROR msg=asdf a=b baz=true
|
||||
}
|
||||
|
|
@ -21,8 +21,4 @@ func main() {
|
|||
|
||||
// glog / zap style (note: can't pass additional attributes)
|
||||
slogctx.Errorf("hello %s", "world")
|
||||
|
||||
log := slogctx.DefaultLogger()
|
||||
log.With("foo", "bar").Infof("hello %s", "world")
|
||||
log.Info("hello world", slog.Bool("baz", true))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ func get(ctx context.Context) ctxVal {
|
|||
}
|
||||
|
||||
// Handler is a slog.Handler that adds context values to the log record.
|
||||
// Values are added via [With].
|
||||
// Values are added via [WithValues].
|
||||
type Handler struct {
|
||||
h slog.Handler
|
||||
}
|
||||
|
|
|
|||
|
|
@ -91,6 +91,7 @@ func (l *Logger) Base() *slog.Logger {
|
|||
return &l.Logger
|
||||
}
|
||||
|
||||
// Handler returns the underlying [slog.Handler].
|
||||
func (l *Logger) Handler() slog.Handler {
|
||||
return l.Logger.Handler()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,3 +35,15 @@ func TestLogger(t Logger) *slogctx.Logger {
|
|||
func TestContextWithLogger(t Logger) context.Context {
|
||||
return slogctx.WithLogger(context.Background(), TestLogger(t))
|
||||
}
|
||||
|
||||
// RemoveTime removes the top-level time attribute.
|
||||
// It is intended to be used as a ReplaceAttr function,
|
||||
// to make example output deterministic.
|
||||
//
|
||||
// This is taken from slog/internal/slogtest.RemoveTime.
|
||||
func RemoveTime(groups []string, a slog.Attr) slog.Attr {
|
||||
if a.Key == slog.TimeKey && len(groups) == 0 {
|
||||
return slog.Attr{}
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue