Add ytdl debug output and badger logger

This commit is contained in:
Evan Fiordeliso 2023-05-23 19:07:44 -04:00
parent c3ba1ce1dd
commit a749a80238
5 changed files with 95 additions and 2 deletions

40
utils/badgerlogger.go Normal file
View File

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

22
utils/logwriter.go Normal file
View File

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

View File

@ -3,9 +3,13 @@ package web
import ( import (
"fmt" "fmt"
"github.com/dgraph-io/badger/v2"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
slogfiber "github.com/samber/slog-fiber" slogfiber "github.com/samber/slog-fiber"
"go.fifitido.net/ytdl-web/config" "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" "golang.org/x/exp/slog"
) )
@ -17,12 +21,25 @@ func Serve(cfg *config.Config) error {
TrustedProxies: cfg.HTTP.TrustedProxies, TrustedProxies: cfg.HTTP.TrustedProxies,
DisableStartupMessage: true, DisableStartupMessage: true,
}) })
routes := &routes{}
logger := slog.With("module", "web") logger := slog.With("module", "web")
app.Use(slogfiber.New(logger)) 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) routes.Register(app)
listenAddr := fmt.Sprintf("%s:%d", cfg.HTTP.Listen, cfg.HTTP.Port) listenAddr := fmt.Sprintf("%s:%d", cfg.HTTP.Listen, cfg.HTTP.Port)

View File

@ -6,7 +6,9 @@ import (
"io" "io"
"strings" "strings"
"go.fifitido.net/ytdl-web/utils"
"go.fifitido.net/ytdl-web/ytdl/metadata" "go.fifitido.net/ytdl-web/ytdl/metadata"
"golang.org/x/exp/slog"
) )
type Options struct { type Options struct {
@ -89,3 +91,11 @@ func WithStreamOutput(output io.Writer) Option {
return nil 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
}
}

View File

@ -49,6 +49,10 @@ func (y *ytdlImpl) baseOptions(url string) []Option {
} }
} }
if y.cfg.IsDevelopment() {
options = append(options, WithDebug())
}
return options return options
} }