Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a flag option #10

Merged
merged 4 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions examples/logger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@ package main

import (
"context"
"flag"
"log/slog"
"os"

"github.com/chainguard-dev/clog"
"github.com/chainguard-dev/clog/slag"
)

func main() {
var level slag.Level
flag.Var(&level, "log-level", "log level")
flag.Parse()

slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: &level})))

log := clog.NewLogger(slog.Default()).With("a", "b")
ctx := clog.WithLogger(context.Background(), log)

// Grab logger from context and use
clog.FromContext(ctx).With("foo", "bar").Infof("hello world")
clog.FromContext(ctx).With("foo", "bar").Debugf("hello debug world")
clog.FromContext(ctx).With("info", true).Infof("hello info world")
clog.FromContext(ctx).With("warn", 42).Warnf("hello warn world")

// Package level context loggers are also aware
clog.ErrorContext(ctx, "asdf")
clog.ErrorContext(ctx, "hello error world")
}
8 changes: 8 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func (l *Logger) Handler() slog.Handler {
}

func wrap(ctx context.Context, logger *Logger, level slog.Level, msg string, args ...any) {
if !logger.Handler().Enabled(ctx, level) {
return
}

var pcs [1]uintptr
runtime.Callers(3, pcs[:]) // skip [Callers, Infof, wrapf]
r := slog.NewRecord(time.Now(), level, msg, pcs[0])
Expand All @@ -107,6 +111,10 @@ func wrap(ctx context.Context, logger *Logger, level slog.Level, msg string, arg
// wrapf is like wrap, but uses fmt.Sprintf to format the message.
// NOTE: args are passed to fmt.Sprintf, not as [slog.Attr].
func wrapf(ctx context.Context, logger *Logger, level slog.Level, format string, args ...any) {
if !logger.Handler().Enabled(ctx, level) {
return
}

var pcs [1]uintptr
runtime.Callers(3, pcs[:]) // skip [Callers, Infof, wrapf]
r := slog.NewRecord(time.Now(), level, fmt.Sprintf(format, args...), pcs[0])
Expand Down
16 changes: 16 additions & 0 deletions slag/flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package slag

import "log/slog"

type Level slog.Level

func (l *Level) Set(s string) error {
var ll slog.Level
if err := ll.UnmarshalText([]byte(s)); err != nil {
return err
}
*l = Level(ll)
imjasonh marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
func (l *Level) String() string { return slog.Level(*l).String() }
func (l *Level) Level() slog.Level { return slog.Level(*l) }
Loading