From b4b0b2304532d6f51eb9a329c94f7f3fab7f2cd9 Mon Sep 17 00:00:00 2001 From: Jason Hall Date: Fri, 2 Feb 2024 16:44:26 -0500 Subject: [PATCH 1/4] WIP: add a flag option Signed-off-by: Jason Hall --- examples/logger/main.go | 11 +++++++++++ flag.go | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 flag.go diff --git a/examples/logger/main.go b/examples/logger/main.go index 6d40b05..db1ee35 100644 --- a/examples/logger/main.go +++ b/examples/logger/main.go @@ -2,18 +2,29 @@ package main import ( "context" + "flag" "log/slog" + "os" "github.com/chainguard-dev/clog" ) func main() { + level := clog.LevelFlag("log-level", slog.LevelInfo, "log level") + flag.Parse() + + slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{ + Level: slog.Level(level.Get()), + }))) + 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") + // Package level context loggers are also aware clog.ErrorContext(ctx, "asdf") } diff --git a/flag.go b/flag.go new file mode 100644 index 0000000..aa4e53d --- /dev/null +++ b/flag.go @@ -0,0 +1,19 @@ +package clog + +import ( + "flag" + "log/slog" + "strings" +) + +type Level struct{ l slog.Level } + +func (l *Level) String() string { return l.l.String() } +func (l *Level) Set(s string) error { return l.l.UnmarshalText([]byte(strings.ToUpper(s))) } +func (l *Level) Get() slog.Level { return l.l } + +func LevelFlag(name string, value slog.Level, usage string) *Level { + l := &Level{value} + flag.CommandLine.Var(l, name, usage) + return l +} From 7ba38d52aba888f4e472a7e01997ef3686bb6a7d Mon Sep 17 00:00:00 2001 From: Jason Hall Date: Sat, 3 Feb 2024 12:25:27 -0500 Subject: [PATCH 2/4] move to subpackage slag Signed-off-by: Jason Hall --- examples/logger/main.go | 6 ++++-- flag.go | 19 ------------------- slag/flag.go | 26 ++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 21 deletions(-) delete mode 100644 flag.go create mode 100644 slag/flag.go diff --git a/examples/logger/main.go b/examples/logger/main.go index db1ee35..1b929d0 100644 --- a/examples/logger/main.go +++ b/examples/logger/main.go @@ -7,14 +7,16 @@ import ( "os" "github.com/chainguard-dev/clog" + "github.com/chainguard-dev/clog/slag" ) func main() { - level := clog.LevelFlag("log-level", slog.LevelInfo, "log level") + level := slag.Level(slog.LevelInfo) + flag.Var(&level, "log-level", "log level") flag.Parse() slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{ - Level: slog.Level(level.Get()), + Level: &level, }))) log := clog.NewLogger(slog.Default()).With("a", "b") diff --git a/flag.go b/flag.go deleted file mode 100644 index aa4e53d..0000000 --- a/flag.go +++ /dev/null @@ -1,19 +0,0 @@ -package clog - -import ( - "flag" - "log/slog" - "strings" -) - -type Level struct{ l slog.Level } - -func (l *Level) String() string { return l.l.String() } -func (l *Level) Set(s string) error { return l.l.UnmarshalText([]byte(strings.ToUpper(s))) } -func (l *Level) Get() slog.Level { return l.l } - -func LevelFlag(name string, value slog.Level, usage string) *Level { - l := &Level{value} - flag.CommandLine.Var(l, name, usage) - return l -} diff --git a/slag/flag.go b/slag/flag.go new file mode 100644 index 0000000..26d9073 --- /dev/null +++ b/slag/flag.go @@ -0,0 +1,26 @@ +package slag + +import ( + "flag" + "log/slog" + "strings" +) + +type Level slog.Level + +func (l *Level) String() string { return slog.Level(*l).String() } +func (l *Level) Set(s string) error { + var ll slog.Level + if err := ll.UnmarshalJSON([]byte(strings.ToUpper(s))); err != nil { + return err + } + *l = Level(ll) + return nil +} +func (l *Level) Level() slog.Level { return slog.Level(*l) } + +func LevelFlag(name string, value slog.Level, usage string) *Level { + var l Level + flag.CommandLine.Var(&l, name, usage) + return &l +} From 26a75ce5b02049d6416930e264439f91751fc4b7 Mon Sep 17 00:00:00 2001 From: Jason Hall Date: Mon, 5 Feb 2024 19:55:06 -0500 Subject: [PATCH 3/4] actually take advantage of the log level flag Signed-off-by: Jason Hall --- examples/logger/main.go | 6 +++--- logger.go | 8 ++++++++ slag/flag.go | 16 +++------------- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/examples/logger/main.go b/examples/logger/main.go index 1b929d0..6ea5457 100644 --- a/examples/logger/main.go +++ b/examples/logger/main.go @@ -23,10 +23,10 @@ func main() { 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") } diff --git a/logger.go b/logger.go index 37365c6..39607e6 100644 --- a/logger.go +++ b/logger.go @@ -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]) @@ -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]) diff --git a/slag/flag.go b/slag/flag.go index 26d9073..f918fde 100644 --- a/slag/flag.go +++ b/slag/flag.go @@ -1,26 +1,16 @@ package slag -import ( - "flag" - "log/slog" - "strings" -) +import "log/slog" type Level slog.Level -func (l *Level) String() string { return slog.Level(*l).String() } func (l *Level) Set(s string) error { var ll slog.Level - if err := ll.UnmarshalJSON([]byte(strings.ToUpper(s))); err != nil { + 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) } - -func LevelFlag(name string, value slog.Level, usage string) *Level { - var l Level - flag.CommandLine.Var(&l, name, usage) - return &l -} From 3c8cd35d7160107cc5ebba89b3fd030f3cf62087 Mon Sep 17 00:00:00 2001 From: Jason Hall Date: Mon, 5 Feb 2024 19:55:46 -0500 Subject: [PATCH 4/4] simplify example Signed-off-by: Jason Hall --- examples/logger/main.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/logger/main.go b/examples/logger/main.go index 6ea5457..ef1adaa 100644 --- a/examples/logger/main.go +++ b/examples/logger/main.go @@ -11,13 +11,11 @@ import ( ) func main() { - level := slag.Level(slog.LevelInfo) + 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, - }))) + 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)