diff --git a/src/app/command/shrink-cmd.go b/src/app/command/shrink-cmd.go index 2cc07d1..fa3bdf2 100644 --- a/src/app/command/shrink-cmd.go +++ b/src/app/command/shrink-cmd.go @@ -2,13 +2,19 @@ package command import ( "fmt" + "log/slog" "maps" "strings" + "github.com/natefinch/lumberjack" + "github.com/pkg/errors" "github.com/snivilised/cobrass" "github.com/snivilised/cobrass/src/assistant" "github.com/snivilised/cobrass/src/store" xi18n "github.com/snivilised/extendio/i18n" + "go.uber.org/zap" + "go.uber.org/zap/exp/zapslog" + "go.uber.org/zap/zapcore" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -132,6 +138,7 @@ func (b *Bootstrap) buildShrinkCommand(container *assistant.CobraContainer) *cob } } + logger, _ := b.createLogger() appErr = proxy.EnterShrink( &proxy.ShrinkParams{ Inputs: inputs, @@ -141,7 +148,7 @@ func (b *Bootstrap) buildShrinkCommand(container *assistant.CobraContainer) *cob SchemesCFG: b.SchemesCFG, SamplerCFG: b.SamplerCFG, AdvancedCFG: b.AdvancedCFG, - LoggingCFG: b.LoggingCFG, + Logger: logger, Vfs: b.Vfs, }, ) @@ -374,3 +381,39 @@ func (b *Bootstrap) getShrinkInputs() *proxy.ShrinkCommandInputs { ).(*assistant.ParamSet[store.FilesFilterParameterSet]), } } + +func (b *Bootstrap) level(raw string) zapcore.LevelEnabler { + if l, err := zapcore.ParseLevel(raw); err == nil { + return l + } + + return zapcore.InfoLevel +} + +func (b *Bootstrap) createLogger() (*slog.Logger, error) { + path := b.LoggingCFG.Path() + + if path == "" { + noc := slog.New(zapslog.NewHandler( + zapcore.NewNopCore(), nil), + ) + + return noc, errors.New("logging path not defined") + } + + ws := zapcore.AddSync(&lumberjack.Logger{ + Filename: path, + MaxSize: int(b.LoggingCFG.MaxSizeInMb()), + MaxBackups: int(b.LoggingCFG.MaxNoOfBackups()), + MaxAge: int(b.LoggingCFG.MaxAgeInDays()), + }) + config := zap.NewProductionEncoderConfig() + config.EncodeTime = zapcore.TimeEncoderOfLayout(b.LoggingCFG.TimeFormat()) + core := zapcore.NewCore( + zapcore.NewJSONEncoder(config), + ws, + b.level(b.LoggingCFG.Level()), + ) + + return slog.New(zapslog.NewHandler(core, nil)), nil +} diff --git a/src/app/proxy/enter-shrink.go b/src/app/proxy/enter-shrink.go index 6b149c2..4a6f095 100644 --- a/src/app/proxy/enter-shrink.go +++ b/src/app/proxy/enter-shrink.go @@ -2,6 +2,7 @@ package proxy import ( "fmt" + "log/slog" "path" "strings" @@ -298,7 +299,7 @@ type ShrinkParams struct { SchemesCFG SchemesConfig SamplerCFG SamplerConfig AdvancedCFG AdvancedConfig - LoggingCFG LoggingConfig + Logger *slog.Logger Vfs storage.VirtualFS } @@ -314,7 +315,7 @@ func EnterShrink( SchemesCFG: params.SchemesCFG, SamplerCFG: params.SamplerCFG, AdvancedCFG: params.AdvancedCFG, - LoggingCFG: params.LoggingCFG, + Logger: params.Logger, Vfs: params.Vfs, }, Inputs: params.Inputs, diff --git a/src/app/proxy/entry-base.go b/src/app/proxy/entry-base.go index 061ea0a..1d63fbe 100644 --- a/src/app/proxy/entry-base.go +++ b/src/app/proxy/entry-base.go @@ -8,16 +8,11 @@ import ( "os" "time" - "github.com/natefinch/lumberjack" - "github.com/pkg/errors" "github.com/samber/lo" "github.com/snivilised/cobrass/src/assistant/configuration" "github.com/snivilised/extendio/xfs/nav" "github.com/snivilised/extendio/xfs/storage" "github.com/snivilised/lorax/boost" - "go.uber.org/zap" - "go.uber.org/zap/exp/zapslog" - "go.uber.org/zap/zapcore" ) type afterFunc func(*nav.TraverseResult, error) @@ -51,7 +46,7 @@ type EntryBase struct { SchemesCFG SchemesConfig SamplerCFG SamplerConfig AdvancedCFG AdvancedConfig - LoggingCFG LoggingConfig + Logger *slog.Logger Vfs storage.VirtualFS FileManager *FileManager } @@ -154,9 +149,7 @@ func (e *EntryBase) ConfigureOptions(o *nav.TraverseOptions) { }) } - if logger, err := e.createLogger(); err == nil { - e.Options.Monitor.Log = logger - } + o.Monitor.Log = e.Logger } func (e *EntryBase) navigate( @@ -165,7 +158,7 @@ func (e *EntryBase) navigate( resumption *nav.Resumption, after ...afterFunc, ) error { - wgan := boost.NewAnnotatedWaitGroup("🍂 traversal") + wgan := boost.NewAnnotatedWaitGroup("🍂 traversal", e.Logger) wgan.Add(1, navigatorRoutineName) ctx, cancel := context.WithCancel(context.Background()) @@ -195,35 +188,3 @@ func (e *EntryBase) navigate( return err } - -func (e *EntryBase) level(raw string) zapcore.LevelEnabler { - if l, err := zapcore.ParseLevel(raw); err == nil { - return l - } - - return zapcore.InfoLevel -} - -func (e *EntryBase) createLogger() (*slog.Logger, error) { - path := e.LoggingCFG.Path() - - if path == "" { - return nil, errors.New("logging path not defined") - } - - ws := zapcore.AddSync(&lumberjack.Logger{ - Filename: path, - MaxSize: int(e.LoggingCFG.MaxSizeInMb()), - MaxBackups: int(e.LoggingCFG.MaxNoOfBackups()), - MaxAge: int(e.LoggingCFG.MaxAgeInDays()), - }) - config := zap.NewProductionEncoderConfig() - config.EncodeTime = zapcore.TimeEncoderOfLayout(e.LoggingCFG.TimeFormat()) - core := zapcore.NewCore( - zapcore.NewJSONEncoder(config), - ws, - e.level(e.LoggingCFG.Level()), - ) - - return slog.New(zapslog.NewHandler(core, nil)), nil -}