1
0
Fork 0
mirror of https://github.com/chainguard-dev/clog synced 2026-07-06 22:12:46 +00:00

add a flag option (#10)

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>
This commit is contained in:
Jason Hall 2024-02-06 11:01:13 -05:00 committed by GitHub
parent d34ebd4160
commit 83244dc767
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 37 additions and 2 deletions

View file

@ -2,18 +2,29 @@ package main
import ( import (
"context" "context"
"flag"
"log/slog" "log/slog"
"os"
"github.com/chainguard-dev/clog" "github.com/chainguard-dev/clog"
"github.com/chainguard-dev/clog/slag"
) )
func main() { 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") log := clog.NewLogger(slog.Default()).With("a", "b")
ctx := clog.WithLogger(context.Background(), log) ctx := clog.WithLogger(context.Background(), log)
// Grab logger from context and use // 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 // Package level context loggers are also aware
clog.ErrorContext(ctx, "asdf") clog.ErrorContext(ctx, "hello error world")
} }

View file

@ -97,6 +97,10 @@ func (l *Logger) Handler() slog.Handler {
} }
func wrap(ctx context.Context, logger *Logger, level slog.Level, msg string, args ...any) { 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 var pcs [1]uintptr
runtime.Callers(3, pcs[:]) // skip [Callers, Infof, wrapf] runtime.Callers(3, pcs[:]) // skip [Callers, Infof, wrapf]
r := slog.NewRecord(time.Now(), level, msg, pcs[0]) 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. // wrapf is like wrap, but uses fmt.Sprintf to format the message.
// NOTE: args are passed to fmt.Sprintf, not as [slog.Attr]. // 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) { 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 var pcs [1]uintptr
runtime.Callers(3, pcs[:]) // skip [Callers, Infof, wrapf] runtime.Callers(3, pcs[:]) // skip [Callers, Infof, wrapf]
r := slog.NewRecord(time.Now(), level, fmt.Sprintf(format, args...), pcs[0]) r := slog.NewRecord(time.Now(), level, fmt.Sprintf(format, args...), pcs[0])

16
slag/flag.go Normal file
View file

@ -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) }