Skip to content

Commit

Permalink
add a flag option (#10)
Browse files Browse the repository at this point in the history
I don't like any of the names here, and we might want it to be in a
separate subpackage. This lets you say `--log-level=DEBUG` to set the
level. The default is still INFO.

---------

Signed-off-by: Jason Hall <jason@chainguard.dev>
  • Loading branch information
imjasonh authored Feb 6, 2024
1 parent d34ebd4 commit 83244dc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
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)
return nil
}
func (l *Level) String() string { return slog.Level(*l).String() }
func (l *Level) Level() slog.Level { return slog.Level(*l) }

0 comments on commit 83244dc

Please sign in to comment.