From de63d4af208aaf53315f379857adf1a39232447f Mon Sep 17 00:00:00 2001 From: Geon Kim Date: Wed, 13 Dec 2023 20:53:46 +0900 Subject: [PATCH] chore(memlimit): refine logging convention --- automemlimit.go | 7 ++++++- memlimit/logger.go | 13 +++++++++++++ memlimit/memlimit.go | 28 +++++++++++++++------------- 3 files changed, 34 insertions(+), 14 deletions(-) create mode 100644 memlimit/logger.go diff --git a/automemlimit.go b/automemlimit.go index 0480208..7d6097d 100644 --- a/automemlimit.go +++ b/automemlimit.go @@ -1,9 +1,14 @@ package automemlimit import ( + "log/slog" + "github.com/KimMachineGun/automemlimit/memlimit" ) func init() { - memlimit.SetGoMemLimitWithEnv() + memlimit.SetGoMemLimitWithOpts( + memlimit.WithEnv(), + memlimit.WithLogger(slog.Default()), + ) } diff --git a/memlimit/logger.go b/memlimit/logger.go new file mode 100644 index 0000000..4cf0b58 --- /dev/null +++ b/memlimit/logger.go @@ -0,0 +1,13 @@ +package memlimit + +import ( + "context" + "log/slog" +) + +type noopLogger struct{} + +func (noopLogger) Enabled(context.Context, slog.Level) bool { return false } +func (noopLogger) Handle(context.Context, slog.Record) error { return nil } +func (d noopLogger) WithAttrs([]slog.Attr) slog.Handler { return d } +func (d noopLogger) WithGroup(string) slog.Handler { return d } diff --git a/memlimit/memlimit.go b/memlimit/memlimit.go index 1e7bf98..35120f4 100644 --- a/memlimit/memlimit.go +++ b/memlimit/memlimit.go @@ -11,8 +11,9 @@ import ( ) const ( - envGOMEMLIMIT = "GOMEMLIMIT" - envAUTOMEMLIMIT = "AUTOMEMLIMIT" + envGOMEMLIMIT = "GOMEMLIMIT" + envAUTOMEMLIMIT = "AUTOMEMLIMIT" + // Deprecated: use memlimit.WithLogger instead envAUTOMEMLIMIT_DEBUG = "AUTOMEMLIMIT_DEBUG" defaultAUTOMEMLIMIT = 0.9 @@ -64,9 +65,13 @@ func WithProvider(provider Provider) Option { } } +// WithLogger configures the logger. +// It automatically attaches the "package" attribute to the logs. +// +// Default: slog.New(noopLogger{}) func WithLogger(logger *slog.Logger) Option { return func(cfg *config) { - cfg.logger = logger + cfg.logger = logger.With(slog.String("package", "memlimit")) } } @@ -76,25 +81,25 @@ func WithLogger(logger *slog.Logger) Option { // - WithRatio // - WithEnv (see more SetGoMemLimitWithEnv) // - WithProvider +// - WithLogger func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error) { cfg := &config{ - logger: slog.Default(), + logger: slog.New(noopLogger{}), ratio: defaultAUTOMEMLIMIT, env: false, provider: FromCgroup, } + // TODO: remove this if _, ok := os.LookupEnv(envAUTOMEMLIMIT_DEBUG); ok { - cfg.logger.Warn("AUTOMEMLIMIT_DEBUG is deprecated, use slog.Level instead") + WithLogger(slog.Default())(cfg) + cfg.logger.Warn("AUTOMEMLIMIT_DEBUG is deprecated, use memlimit.WithLogger instead") } for _, opt := range opts { opt(cfg) } defer func() { if _err != nil { - cfg.logger.Error( - "failed to set GOMEMLIMIT", - slog.Any("err", _err), - ) + cfg.logger.Error("failed to set GOMEMLIMIT", slog.Any("error", _err)) } }() @@ -103,10 +108,7 @@ func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error) { err := recover() if err != nil { if _err != nil { - cfg.logger.Error( - "failed to set GOMEMLIMIT", - slog.Any("error", _err), - ) + cfg.logger.Error("failed to set GOMEMLIMIT", slog.Any("error", _err)) } _err = fmt.Errorf("panic during setting the Go's memory limit, rolling back to previous value %d: %v", snapshot, err) debug.SetMemoryLimit(snapshot)