From a749a80238b651efc0975c43502d9be2af3451ca Mon Sep 17 00:00:00 2001 From: Evan Fiordeliso Date: Tue, 23 May 2023 19:07:44 -0400 Subject: [PATCH] Add ytdl debug output and badger logger --- utils/badgerlogger.go | 40 ++++++++++++++++++++++++++++++++++++++++ utils/logwriter.go | 22 ++++++++++++++++++++++ web/serve.go | 21 +++++++++++++++++++-- ytdl/cmdopts.go | 10 ++++++++++ ytdl/ytdl.go | 4 ++++ 5 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 utils/badgerlogger.go create mode 100644 utils/logwriter.go diff --git a/utils/badgerlogger.go b/utils/badgerlogger.go new file mode 100644 index 0000000..9a5a9bb --- /dev/null +++ b/utils/badgerlogger.go @@ -0,0 +1,40 @@ +package utils + +import ( + "fmt" + + "github.com/dgraph-io/badger/v2" + "golang.org/x/exp/slog" +) + +type SlogLogger struct { + logger *slog.Logger +} + +// Debugf implements badger.Logger +func (l *SlogLogger) Debugf(f string, a ...interface{}) { + l.logger.Debug(fmt.Sprintf(f, a...)) +} + +// Errorf implements badger.Logger +func (l *SlogLogger) Errorf(f string, a ...interface{}) { + l.logger.Error(fmt.Sprintf(f, a...)) +} + +// Infof implements badger.Logger +func (l *SlogLogger) Infof(f string, a ...interface{}) { + l.logger.Info(fmt.Sprintf(f, a...)) +} + +// Warningf implements badger.Logger +func (l *SlogLogger) Warningf(f string, a ...interface{}) { + l.logger.Warn(fmt.Sprintf(f, a...)) +} + +var _ badger.Logger = (*SlogLogger)(nil) + +func NewBadgerLogger(logger *slog.Logger) badger.Logger { + return &SlogLogger{ + logger: logger, + } +} diff --git a/utils/logwriter.go b/utils/logwriter.go new file mode 100644 index 0000000..0b98df8 --- /dev/null +++ b/utils/logwriter.go @@ -0,0 +1,22 @@ +package utils + +import ( + "context" + "io" + + "golang.org/x/exp/slog" +) + +type loggerWriter struct { + logger *slog.Logger + logLevel slog.Level +} + +func (lw *loggerWriter) Write(p []byte) (n int, err error) { + lw.logger.Log(context.Background(), lw.logLevel, string(p)) + return len(p), nil +} + +func LoggerWriter(logger *slog.Logger, level slog.Level) io.Writer { + return &loggerWriter{logger: logger, logLevel: level} +} diff --git a/web/serve.go b/web/serve.go index 26d321d..cc9d0cb 100644 --- a/web/serve.go +++ b/web/serve.go @@ -3,9 +3,13 @@ package web import ( "fmt" + "github.com/dgraph-io/badger/v2" "github.com/gofiber/fiber/v2" slogfiber "github.com/samber/slog-fiber" "go.fifitido.net/ytdl-web/config" + "go.fifitido.net/ytdl-web/utils" + "go.fifitido.net/ytdl-web/ytdl" + "go.fifitido.net/ytdl-web/ytdl/cache" "golang.org/x/exp/slog" ) @@ -17,12 +21,25 @@ func Serve(cfg *config.Config) error { TrustedProxies: cfg.HTTP.TrustedProxies, DisableStartupMessage: true, }) - routes := &routes{} logger := slog.With("module", "web") - app.Use(slogfiber.New(logger)) + db, err := badger.Open( + badger. + DefaultOptions("/tmp/badger"). + WithLogger(utils.NewBadgerLogger(slog.With("module", "badger"))), + ) + if err != nil { + return err + } + defer db.Close() + + cache := cache.NewDefaultMetadataCache(db) + + routes := &routes{ + ytdl: ytdl.NewYtdl(cfg, slog.Default(), cache), + } routes.Register(app) listenAddr := fmt.Sprintf("%s:%d", cfg.HTTP.Listen, cfg.HTTP.Port) diff --git a/ytdl/cmdopts.go b/ytdl/cmdopts.go index cf3c25d..0c5abc7 100644 --- a/ytdl/cmdopts.go +++ b/ytdl/cmdopts.go @@ -6,7 +6,9 @@ import ( "io" "strings" + "go.fifitido.net/ytdl-web/utils" "go.fifitido.net/ytdl-web/ytdl/metadata" + "golang.org/x/exp/slog" ) type Options struct { @@ -89,3 +91,11 @@ func WithStreamOutput(output io.Writer) Option { return nil } } + +func WithDebug() Option { + return func(opts *Options) error { + opts.stdout = utils.LoggerWriter(slog.With("module", "ytdl"), slog.LevelDebug) + opts.stderr = utils.LoggerWriter(slog.With("module", "ytdl"), slog.LevelError) + return nil + } +} diff --git a/ytdl/ytdl.go b/ytdl/ytdl.go index 077e7f5..bc3c33c 100644 --- a/ytdl/ytdl.go +++ b/ytdl/ytdl.go @@ -49,6 +49,10 @@ func (y *ytdlImpl) baseOptions(url string) []Option { } } + if y.cfg.IsDevelopment() { + options = append(options, WithDebug()) + } + return options }