Skip to content

Commit

Permalink
Merge pull request #116 from Dash-Industry-Forum/slog
Browse files Browse the repository at this point in the history
feat: change logger from zerolog to slog and add pretty handler
  • Loading branch information
tobbee authored Oct 17, 2023
2 parents 646611c + 0094cc0 commit dd64eba
Show file tree
Hide file tree
Showing 30 changed files with 322 additions and 445 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"prettylog",
"promhttp",
"proxied",
"proxying",
"repdata",
"repdataroot",
"reqcount",
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Changed

- Upgrade to Go 1.21
- Changed logging to slog instead of zerolog. Log levels limited to DEBUG, INFO, WARN, ERROR.

### Fixed

Expand Down
32 changes: 17 additions & 15 deletions cmd/dashfetcher/app/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"os"
Expand All @@ -19,7 +20,6 @@ import (

"github.com/Dash-Industry-Forum/livesim2/internal"
m "github.com/Eyevinn/dash-mpd/mpd"
"github.com/rs/zerolog/log"
)

type Options struct {
Expand Down Expand Up @@ -50,8 +50,10 @@ func Fetch(o *Options) error {
return fmt.Errorf("createDir: %w", err)
}
cnt, err := start(ctx, o)
log.Info().Int("nrFiles", cnt.total()).Int("nrExisting", cnt.nrExisting).
Int("nrDownloaded", cnt.nrDownloaded).Int("nrErrors", cnt.nrErrors).Msg("download results")
slog.Info("download results", "nrFiles", cnt.total(),
"nrExisting", cnt.nrExisting,
"nrDownloaded", cnt.nrDownloaded,
"nrErrors", cnt.nrErrors)
return err
}

Expand Down Expand Up @@ -121,7 +123,7 @@ func start(ctx context.Context, o *Options) (counts, error) {
case strings.Contains(media, "$Time$"):
cnt = downloadSegmentTimeLineWithTime(ctx, stl, media, outDir, baseURL, cnt, o.Force)
case strings.Contains(media, "$Number$"):
log.Warn().Msg("SegmentTimeline with $Number$ not yet supported")
slog.Warn("SegmentTimeline with $Number$ not yet supported")
// downloadSegmentTimeLineWithNumber
default:
return cnt, fmt.Errorf("strange media for SegmentTimeline")
Expand All @@ -145,7 +147,7 @@ func start(ctx context.Context, o *Options) (counts, error) {
func downloadMPD(ctx context.Context, mpdURL, outDir, mpdName string, cnt counts, force bool) (counts, error) {
outPath := path.Join(outDir, mpdName)
if fileExists(outPath) && !force {
log.Info().Str("path", outPath).Str("url", mpdURL).Msg("file already exists. Skipping")
slog.Info("file already exists. Skipping", "path", outPath, "url", mpdURL)
cnt.nrExisting++
} else {
err := downloadToFile(ctx, mpdURL, outPath)
Expand All @@ -155,7 +157,7 @@ func downloadMPD(ctx context.Context, mpdURL, outDir, mpdName string, cnt counts
}
err = internal.WriteMPDData(outDir, mpdName, mpdURL)
if err != nil {
log.Warn().Err(err).Msg("could not write mdlist file")
slog.Warn("could not write mpdlist file", "error", err)
}
}
return cnt, nil
Expand All @@ -166,7 +168,7 @@ func downloadInit(ctx context.Context, segTmpl *m.SegmentTemplateType, outDir, b
p := path.Join(outDir, initStr)
cnt, err := downloadAndCount(ctx, u, p, cnt, force)
if err != nil {
log.Warn().Err(err).Msg("download init segment")
slog.Warn("download init segment", "error", err)
}
return cnt
}
Expand All @@ -183,7 +185,7 @@ func downloadSegmentTimeLineWithTime(ctx context.Context, stl *m.SegmentTimeline
p := path.Join(outDir, mPart)
cnt, err = downloadAndCount(ctx, u, p, cnt, force)
if err != nil {
log.Warn().Err(err).Msg("download file")
slog.Warn("download file", "error", err)
}
dur := segItvl.D
startTime += dur
Expand All @@ -193,7 +195,7 @@ func downloadSegmentTimeLineWithTime(ctx context.Context, stl *m.SegmentTimeline
p := path.Join(outDir, mPart)
cnt, err = downloadAndCount(ctx, u, p, cnt, force)
if err != nil {
log.Warn().Err(err).Msg("download file")
slog.Warn("download file", "error", err)
}
startTime += dur
}
Expand All @@ -207,7 +209,7 @@ func downloadSegmentNumber(ctx context.Context, stpl *m.SegmentTemplateType, tot
startNr = *stpl.StartNumber
}
if stpl == nil {
log.Warn().Msg("segment duration not set")
slog.Warn("segment duration not set")
return cnt
}
dur := *stpl.Duration
Expand All @@ -223,7 +225,7 @@ func downloadSegmentNumber(ctx context.Context, stpl *m.SegmentTemplateType, tot
p := path.Join(outDir, mPart)
cnt, err = downloadAndCount(ctx, u, p, cnt, force)
if err != nil && i < nrSegments {
log.Warn().Err(err).Msg("download file")
slog.Warn("download file", "error", err)
}
}
return cnt
Expand All @@ -232,7 +234,7 @@ func downloadSegmentNumber(ctx context.Context, stpl *m.SegmentTemplateType, tot
func downloadAndCount(ctx context.Context, url, outPath string, cnt counts, force bool) (counts, error) {
if fileExists(outPath) && !force {
cnt.nrExisting++
log.Info().Str("path", outPath).Str("url", url).Msg("file already exists. Skipping")
slog.Info("file already exists. Skipping", "path", outPath, "url", url)
} else {
err := downloadToFile(ctx, url, outPath)
if err != nil {
Expand Down Expand Up @@ -265,10 +267,10 @@ func replaceNumber(media string, nr uint32) string {
func downloadToFile(ctx context.Context, url, outPath string) error {
client := http.DefaultClient
if fileExists(outPath) {
log.Info().Str("path", outPath).Msg("file exists")
slog.Info("file exists", "path", outPath)
return nil
}
log.Info().Str("url", url).Str("path", outPath).Msg("Downloading")
slog.Info("downloading", "url", url, "path", outPath)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return err
Expand Down Expand Up @@ -296,7 +298,7 @@ func downloadToFile(ctx context.Context, url, outPath string) error {
if err != nil {
return err
}
log.Debug().Str("path", outPath).Msg("stored")
slog.Debug("stored", "path", outPath)
return nil
}

Expand Down
25 changes: 14 additions & 11 deletions cmd/dashfetcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ package main

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

"github.com/rs/zerolog/log"

"github.com/Dash-Industry-Forum/livesim2/cmd/dashfetcher/app"
"github.com/Dash-Industry-Forum/livesim2/internal"
"github.com/Dash-Industry-Forum/livesim2/pkg/logging"
Expand All @@ -35,9 +34,9 @@ func parseOptions() *app.Options {
o := app.Options{}
flag.StringVarP(&o.OutDir, "outdir", "o", ".", "output directory")
flag.BoolVarP(&o.AutoOutDir, "auto", "a", false, "automatically add output directory parts from URL")
logFormatUsage := fmt.Sprintf("format and type of log: %v", logging.LogFormatsCommandLine)
logFormatUsage := fmt.Sprintf("format and type of log: %v", logging.LogFormats)
flag.StringVarP(&o.LogFile, "logfile", "l", "", "log file [default stdout]")
flag.StringVarP(&o.LogFormat, "logformat", "", "consolepretty", logFormatUsage)
flag.StringVarP(&o.LogFormat, "logformat", "", logging.LogText, logFormatUsage)
flag.StringVarP(&o.LogLevel, "loglevel", "", "info", "initial log level")
flag.BoolVarP(&o.Version, "version", "v", false, "print version and date")
longHelp := flag.Bool("help", false, "extended tool help")
Expand Down Expand Up @@ -74,30 +73,34 @@ func parseOptions() *app.Options {
func main() {
o := parseOptions()

_, err := logging.InitZerolog(o.LogLevel, o.LogFormat)
err := logging.InitSlog(o.LogLevel, o.LogFormat)

if err != nil {
log.Fatal().Err(err).Send()
slog.Error(err.Error())
os.Exit(1)
}

if o.OutDir == "." {
o.OutDir, err = os.Getwd()
if err != nil {
log.Fatal().Err(err).Send()
slog.Error(err.Error())
os.Exit(1)
}
}

if o.AutoOutDir {
o.OutDir, err = app.AutoDir(o.AssetURL, o.OutDir)
if err != nil {
log.Fatal().Err(err).Send()
slog.Error(err.Error())
os.Exit(1)
}
log.Info().Str("output dir", o.OutDir).Msg("automatic output dir for MPD")
slog.Info("automatic output dir for MPD", "output dir", o.OutDir)
}

log.Info().Str("version", internal.GetVersion()).Msg("starting")
slog.Info("starting", "version", internal.GetVersion())
err = app.Fetch(o)
if err != nil {
log.Fatal().Err(err).Send()
slog.Error(err.Error())
os.Exit(1)
}
}
26 changes: 13 additions & 13 deletions cmd/livesim2/app/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"io"
"io/fs"
"log/slog"
"math"
"os"
"path"
Expand All @@ -23,7 +24,6 @@ import (
m "github.com/Eyevinn/dash-mpd/mpd"
"github.com/Eyevinn/mp4ff/bits"
"github.com/Eyevinn/mp4ff/mp4"
"github.com/rs/zerolog/log"
)

func newAssetMgr(vodFS fs.FS, repDataDir string, writeRepData bool) *assetMgr {
Expand Down Expand Up @@ -73,7 +73,7 @@ func (am *assetMgr) discoverAssets() error {
if path.Ext(p) == ".mpd" {
err := am.loadAsset(p)
if err != nil {
log.Warn().Err(err).Str("asset", p).Msg("Asset loading problem. Skipping")
slog.Warn("Asset loading problem. Skipping", "asset", p, "err", err.Error())
}
}
return nil
Expand All @@ -88,11 +88,11 @@ func (am *assetMgr) discoverAssets() error {
for aID, a := range am.assets {
err := a.consolidateAsset()
if err != nil {
log.Warn().Err(err).Str("asset", aID).Msg("Asset consolidation problem. Skipping")
slog.Warn("Asset consolidation problem. Skipping", "error", err.Error())
delete(am.assets, aID) // This deletion should be safe
continue
}
log.Info().Str("asset", a.AssetPath).Int("loopDurMS", a.LoopDurMS).Msg("Asset consolidated")
slog.Info("Asset consolidated", "asset", a.AssetPath, "loopDurMS", a.LoopDurMS)
}
return nil
}
Expand Down Expand Up @@ -142,7 +142,7 @@ func (am *assetMgr) loadAsset(mpdPath string) error {
return fmt.Errorf("segmentTemplate on Representation level. Only supported on AdaptationSet level")
}
if _, ok := asset.Reps[rep.Id]; ok {
log.Debug().Str("rep", rep.Id).Str("asset", mpdPath).Msg("Representation already loaded")
slog.Debug("Representation already loaded", "rep", rep.Id, "asset", mpdPath)
continue
}
r, err := am.loadRep(assetPath, mpd, as, rep)
Expand All @@ -164,7 +164,7 @@ func (am *assetMgr) loadAsset(mpdPath string) error {
}
}
}
log.Info().Str("mpdName", mpdPath).Msg("Asset MPD loaded")
slog.Info("asset MPD loaded", "asset", assetPath, "mpdName", mpdPath)
return nil
}

Expand All @@ -177,11 +177,11 @@ func (am *assetMgr) loadRep(assetPath string, mpd *m.MPD, as *m.AdaptationSetTyp
if !am.writeRepData {
ok, err := rp.readFromJSON(am.vodFS, am.repDataDir, assetPath)
if ok {
log.Info().Str("rep", rp.ID).Str("asset", assetPath).Msg("Representation loaded from JSON")
slog.Info("Representation loaded from JSON", "rep", rp.ID, "asset", assetPath)
return &rp, err
}
}
log.Debug().Str("rep", rp.ID).Str("asset", assetPath).Msg("Loading full representation")
slog.Debug("Loading full representation", "rep", rp.ID, "asset", assetPath)
st := as.SegmentTemplate
if rep.SegmentTemplate != nil {
st = rep.SegmentTemplate
Expand Down Expand Up @@ -292,7 +292,7 @@ segLoop:
}
err = rp.writeToJSON(am.repDataDir, assetPath)
if err == nil {
log.Info().Str("rep", rp.ID).Str("asset", assetPath).Msg("Representation data written to JSON file")
slog.Info("Representation data written to JSON file", "rep", rp.ID, "asset", assetPath)
}
return &rp, err
}
Expand Down Expand Up @@ -321,7 +321,7 @@ func (rp *RepData) readFromJSON(vodFS fs.FS, repDataDir, assetPath string) (bool
if err != nil {
return true, err
}
log.Debug().Str("path", gzipPath).Msg("Read repData")
slog.Debug("Read repdata", "path", gzipPath)
}
if len(data) == 0 {
_, err := os.Stat(repDataPath)
Expand All @@ -330,7 +330,7 @@ func (rp *RepData) readFromJSON(vodFS fs.FS, repDataDir, assetPath string) (bool
if err != nil {
return true, err
}
log.Debug().Str("path", repDataPath).Msg("Read repData")
slog.Debug("Read repdata", "path", repDataPath)
}
}
if len(data) == 0 {
Expand Down Expand Up @@ -395,7 +395,7 @@ func (rp *RepData) writeToJSON(repDataDir, assetPath string) error {
if err != nil {
return err
}
log.Debug().Str("path", gzipPath).Msg("Wrote repData")
slog.Debug("Wrote repData", "path", gzipPath)
return nil
}

Expand Down Expand Up @@ -607,7 +607,7 @@ func (a *asset) consolidateAsset() error {
}
repDurMS := 1000 * rep.duration() / rep.MediaTimescale
if repDurMS != a.LoopDurMS {
log.Info().Str("rep", rep.ID).Str("asset", a.AssetPath).Msg("rep duration differs from loop duration")
slog.Info("Rep duration differs from loop duration", "rep", rep.ID, "asset", a.AssetPath)
}
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/livesim2/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ type ServerConfig struct {
}

var DefaultConfig = ServerConfig{
LogFormat: "consolepretty",
LogLevel: "info",
LogFormat: "text",
LogLevel: "INFO",
Port: 8888,
LiveWindowS: 300,
TimeoutS: 60,
Expand Down
5 changes: 2 additions & 3 deletions cmd/livesim2/app/handler_assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ package app

import (
"fmt"
"log/slog"
"net/http"
"net/url"
"sort"
"strings"

"github.com/rs/zerolog/log"
)

type assetsInfo struct {
Expand Down Expand Up @@ -45,7 +44,7 @@ func (s *Server) assetsHandlerFunc(w http.ResponseWriter, r *http.Request) {
fh := fullHost(s.Cfg.Host, r)
playURL, err := createPlayURL(fh, s.Cfg.PlayURL)
if err != nil {
log.Err(err).Msg("cannot create playurl")
slog.Error("cannot create playurl")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/livesim2/app/handler_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestIndexPageWithHost(t *testing.T) {
LogFormat: logging.LogDiscard,
Host: "https://example.com/subfolder",
}
_, err := logging.InitZerolog(cfg.LogLevel, cfg.LogFormat)
err := logging.InitSlog(cfg.LogLevel, cfg.LogFormat)
require.NoError(t, err)
server, err := SetupServer(context.Background(), &cfg)
require.NoError(t, err)
Expand All @@ -41,7 +41,7 @@ func TestIndexPageWithoutHost(t *testing.T) {
TimeoutS: 0,
LogFormat: logging.LogDiscard,
}
_, err := logging.InitZerolog(cfg.LogLevel, cfg.LogFormat)
err := logging.InitSlog(cfg.LogLevel, cfg.LogFormat)
require.NoError(t, err)
server, err := SetupServer(context.Background(), &cfg)
require.NoError(t, err)
Expand Down
Loading

0 comments on commit dd64eba

Please sign in to comment.