Skip to content

Commit

Permalink
fix(proxy): set logger on annotated wait group (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Jan 5, 2024
1 parent 094a530 commit 2c91a32
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 45 deletions.
45 changes: 44 additions & 1 deletion src/app/command/shrink-cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -132,6 +138,7 @@ func (b *Bootstrap) buildShrinkCommand(container *assistant.CobraContainer) *cob
}
}

logger, _ := b.createLogger()
appErr = proxy.EnterShrink(
&proxy.ShrinkParams{
Inputs: inputs,
Expand All @@ -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,
},
)
Expand Down Expand Up @@ -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
}
5 changes: 3 additions & 2 deletions src/app/proxy/enter-shrink.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package proxy

import (
"fmt"
"log/slog"
"path"
"strings"

Expand Down Expand Up @@ -298,7 +299,7 @@ type ShrinkParams struct {
SchemesCFG SchemesConfig
SamplerCFG SamplerConfig
AdvancedCFG AdvancedConfig
LoggingCFG LoggingConfig
Logger *slog.Logger
Vfs storage.VirtualFS
}

Expand All @@ -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,
Expand Down
45 changes: 3 additions & 42 deletions src/app/proxy/entry-base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -51,7 +46,7 @@ type EntryBase struct {
SchemesCFG SchemesConfig
SamplerCFG SamplerConfig
AdvancedCFG AdvancedConfig
LoggingCFG LoggingConfig
Logger *slog.Logger
Vfs storage.VirtualFS
FileManager *FileManager
}
Expand Down Expand Up @@ -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(
Expand All @@ -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())
Expand Down Expand Up @@ -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
}

0 comments on commit 2c91a32

Please sign in to comment.