From 3b87e4aad723b0fbc16e8a42be60bd4562637c24 Mon Sep 17 00:00:00 2001 From: Antonio Navarro Perez Date: Wed, 10 Mar 2021 10:54:56 +0100 Subject: [PATCH] Move from logrus to zerolog. (#45) Signed-off-by: Antonio Navarro Perez --- build_tools/assets_generate/main.go | 4 ++-- cmd/distribyted/main.go | 23 +++++++++++++++-------- config/handler.go | 4 ++-- fuse/handler.go | 8 ++++---- fuse/mount.go | 22 +++++++++++----------- go.mod | 3 ++- go.sum | 11 +++++++---- http/http.go | 4 ++-- torrent/handler.go | 6 +++--- webdav/http.go | 4 ++-- 10 files changed, 50 insertions(+), 39 deletions(-) diff --git a/build_tools/assets_generate/main.go b/build_tools/assets_generate/main.go index 0b1d181..3009d46 100644 --- a/build_tools/assets_generate/main.go +++ b/build_tools/assets_generate/main.go @@ -4,8 +4,8 @@ package main import ( "github.com/distribyted/distribyted" + "github.com/rs/zerolog/log" "github.com/shurcooL/vfsgen" - log "github.com/sirupsen/logrus" ) func main() { @@ -15,6 +15,6 @@ func main() { PackageName: "distribyted", }) if err != nil { - log.Fatalln(err) + log.Fatal().Err(err).Msg("problem generating static files") } } diff --git a/cmd/distribyted/main.go b/cmd/distribyted/main.go index 3e2affa..d5a34aa 100644 --- a/cmd/distribyted/main.go +++ b/cmd/distribyted/main.go @@ -15,10 +15,17 @@ import ( "github.com/distribyted/distribyted/stats" "github.com/distribyted/distribyted/torrent" "github.com/distribyted/distribyted/webdav" - "github.com/sirupsen/logrus" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" "github.com/urfave/cli/v2" ) +func init() { + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) + zerolog.TimeFieldFormat = zerolog.TimeFormatUnix + zerolog.SetGlobalLevel(zerolog.InfoLevel) +} + const ( configFlag = "config" fuseAllowOther = "fuse-allow-other" @@ -66,7 +73,7 @@ func main() { } if err := app.Run(os.Args); err != nil { - logrus.Fatal(err) + log.Fatal().Err(err).Msg("problem starting application") } } @@ -144,28 +151,28 @@ func load(configPath string, port, webDAVPort int, fuseAllowOther bool) error { if conf.WebDAV != nil { wdth := torrent.NewHandler(c, ss) if err := wdth.Load("::/webDAV", conf.WebDAV.Torrents); err != nil { - logrus.WithError(err).Error("error loading torrents for webDAV") + log.Error().Err(err).Msg("error loading torrents for webDAV") } if err := webdav.NewWebDAVServer(wdth.Fileststems(), webDAVPort); err != nil { - logrus.WithError(err).Error("error starting webDAV") + log.Error().Err(err).Msg("error starting webDAV") } } }() err = http.New(fc, ss, ch, port) - logrus.WithError(err).Error("error initializing HTTP server") + log.Error().Err(err).Msg("error initializing HTTP server") return err } func tryClose(c *t.Client, mountService *fuse.Handler) { - logrus.Info("closing torrent client...") + log.Info().Msg("closing torrent client...") c.Close() - logrus.Info("unmounting fuse filesystem...") + log.Info().Msg("unmounting fuse filesystem...") mountService.UnmountAll() - logrus.Info("exiting") + log.Info().Msg("exiting") os.Exit(1) } diff --git a/config/handler.go b/config/handler.go index 30c7aba..799f635 100644 --- a/config/handler.go +++ b/config/handler.go @@ -7,7 +7,7 @@ import ( "path" "github.com/distribyted/distribyted" - "github.com/sirupsen/logrus" + "github.com/rs/zerolog/log" "gopkg.in/yaml.v3" ) @@ -44,7 +44,7 @@ func (c *Handler) createFromTemplateFile() ([]byte, error) { func (c *Handler) GetRaw() ([]byte, error) { f, err := ioutil.ReadFile(c.p) if os.IsNotExist(err) { - logrus.WithField("file", c.p).Info("configuration file does not exist, creating from template file") + log.Info().Str("file", c.p).Msg("configuration file does not exist, creating from template file") return c.createFromTemplateFile() } diff --git a/fuse/handler.go b/fuse/handler.go index 121b95d..215d6c9 100644 --- a/fuse/handler.go +++ b/fuse/handler.go @@ -8,7 +8,7 @@ import ( "github.com/billziss-gh/cgofuse/fuse" "github.com/distribyted/distribyted/config" "github.com/distribyted/distribyted/fs" - log "github.com/sirupsen/logrus" + "github.com/rs/zerolog/log" ) type Handler struct { @@ -47,7 +47,7 @@ func (s *Handler) MountAll(fss map[string]fs.Filesystem, ef config.EventFunc) er ok := host.Mount(p, config) if !ok { - log.WithField("path", p).Error("error trying to mount filesystem") + log.Error().Str("path", p).Msg("error trying to mount filesystem") } }() @@ -59,11 +59,11 @@ func (s *Handler) MountAll(fss map[string]fs.Filesystem, ef config.EventFunc) er func (s *Handler) UnmountAll() { for path, server := range s.hosts { - log.WithField("path", path).Info("unmounting") + log.Info().Str("path", path).Msg("unmounting") ok := server.Unmount() if !ok { //TODO try to force unmount if possible - log.WithField("path", path).Error("unmount failed") + log.Error().Str("path", path).Msg("unmount failed") } } diff --git a/fuse/mount.go b/fuse/mount.go index 3cd04e5..6496965 100644 --- a/fuse/mount.go +++ b/fuse/mount.go @@ -9,7 +9,7 @@ import ( "github.com/billziss-gh/cgofuse/fuse" "github.com/distribyted/distribyted/fs" - "github.com/sirupsen/logrus" + "github.com/rs/zerolog/log" ) type FS struct { @@ -26,12 +26,12 @@ func NewFS(fs fs.Filesystem) fuse.FileSystemInterface { func (fs *FS) Open(path string, flags int) (errc int, fh uint64) { fh, err := fs.fh.OpenHolder(path) if err == os.ErrNotExist { - logrus.WithField("path", path).Debug("file does not exists") + log.Debug().Str("path", path).Msg("file does not exists") return -fuse.ENOENT, fhNone } if err != nil { - logrus.WithError(err).WithField("path", path).Error("error opening file") + log.Error().Err(err).Str("path", path).Msg("error opening file") return -fuse.EIO, fhNone } @@ -50,12 +50,12 @@ func (cfs *FS) Getattr(path string, stat *fuse.Stat_t, fh uint64) (errc int) { file, err := cfs.fh.GetFile(path, fh) if err == os.ErrNotExist { - logrus.WithField("path", path).Debug("file does not exists") + log.Debug().Str("path", path).Msg("file does not exists") return -fuse.ENOENT } if err != nil { - logrus.WithError(err).WithField("path", path).Error("error getting holder when reading file attributes") + log.Error().Err(err).Str("path", path).Msg("error getting holder when reading file attributes") return -fuse.EIO } @@ -72,12 +72,12 @@ func (cfs *FS) Getattr(path string, stat *fuse.Stat_t, fh uint64) (errc int) { func (fs *FS) Read(path string, dest []byte, off int64, fh uint64) int { file, err := fs.fh.GetFile(path, fh) if err == os.ErrNotExist { - logrus.WithField("path", path).Error("file not found on READ operation") + log.Error().Str("path", path).Msg("file not found on READ operation") return -fuse.ENOENT } if err != nil { - logrus.WithError(err).WithField("path", path).Error("error getting holder reading data from file") + log.Error().Err(err).Str("path", path).Msg("error getting holder reading data from file") return -fuse.EIO } @@ -90,7 +90,7 @@ func (fs *FS) Read(path string, dest []byte, off int64, fh uint64) int { n, err := file.ReadAt(buf, off) if err != nil && err != io.EOF { - logrus.WithError(err).WithField("path", path).Error("error reading data") + log.Error().Err(err).Str("path", path).Msg("error reading data") return -fuse.EIO } @@ -101,7 +101,7 @@ func (fs *FS) Read(path string, dest []byte, off int64, fh uint64) int { func (fs *FS) Release(path string, fh uint64) (errc int) { if err := fs.fh.Remove(fh); err != nil { - logrus.WithError(err).WithField("path", path).Error("error getting holder when releasing file") + log.Error().Err(err).Str("path", path).Msg("error getting holder when releasing file") return -fuse.EIO } @@ -122,13 +122,13 @@ func (fs *FS) Readdir(path string, //TODO improve this function to make use of fh index if possible paths, err := fs.fh.ListDir(path) if err != nil { - logrus.WithField("path", path).Error("error reading directory") + log.Error().Str("path", path).Msg("error reading directory") return -fuse.EIO } for _, p := range paths { if !fill(p, nil, 0) { - logrus.WithField("path", p).Error("error adding directory") + log.Error().Str("path", path).Msg("error adding directory") break } } diff --git a/go.mod b/go.mod index d11794c..71df25b 100644 --- a/go.mod +++ b/go.mod @@ -24,14 +24,15 @@ require ( github.com/pion/srtp v1.5.2 // indirect github.com/pion/turn/v2 v2.0.5 // indirect github.com/pion/webrtc/v2 v2.2.26 // indirect + github.com/rs/zerolog v1.20.0 github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 - github.com/sirupsen/logrus v1.7.0 github.com/stretchr/testify v1.6.1 github.com/tinylib/msgp v1.1.4 // indirect github.com/urfave/cli/v2 v2.3.0 github.com/willf/bitset v1.1.11 // indirect golang.org/x/crypto v0.0.0-20201112155050-0c6587e931a9 // indirect golang.org/x/net v0.0.0-20210119194325-5f4716e94777 + golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 // indirect golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect google.golang.org/protobuf v1.25.0 // indirect gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 diff --git a/go.sum b/go.sum index 2b73bfe..b0235e4 100644 --- a/go.sum +++ b/go.sum @@ -152,6 +152,7 @@ github.com/cheekybits/genny v1.0.0 h1:uGGa4nei+j20rOSeDeP5Of12XVm7TGUd4dJA9RDitf github.com/cheekybits/genny v1.0.0/go.mod h1:+tQajlRqAUrPI7DOSpB0XAqZYtQakVtB7wXkRAgjxjQ= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/coreos/go-systemd v0.0.0-20181012123002-c6f51f82210d/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -504,6 +505,9 @@ github.com/prometheus/procfs v0.0.11/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4 github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/rs/dnscache v0.0.0-20190621150935-06bb5526f76b h1:vQr7k9nLapW7xKl8qYc6+S5VHuhvdDF1vVzTFbWrptE= github.com/rs/dnscache v0.0.0-20190621150935-06bb5526f76b/go.mod h1:qe5TWALJ8/a1Lqznoc5BDHpYX/8HU60Hm2AwRmqzxqA= +github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= +github.com/rs/zerolog v1.20.0 h1:38k9hgtUBdxFwE34yS8rTHmHBa4eN16E4DJlv177LNs= +github.com/rs/zerolog v1.20.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJRjo= github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= @@ -538,11 +542,8 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5I github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/shurcooL/users v0.0.0-20180125191416-49c67e49c537/go.mod h1:QJTqeLYEDaXHZDBsXlPCDqdhQuJkuw4NOtaxYe3xii4= github.com/shurcooL/webdavfs v0.0.0-20170829043945-18c3829fa133/go.mod h1:hKmq5kWdCj2z2KEozexVbfEZIWiTjhE0+UjmZgPqehw= -github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= -github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= -github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/assertions v0.0.0-20190215210624-980c5ac6f3ac/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c/go.mod h1:XDJAKZRPZ1CvBcN2aX5YOUTYGHki24fSF0Iv48Ibg0s= @@ -703,7 +704,6 @@ golang.org/x/sys v0.0.0-20190910064555-bbd175535a8b h1:3S2h5FadpNr0zUUCVZjlKIEYF golang.org/x/sys v0.0.0-20190910064555-bbd175535a8b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191105231009-c1f44814a5cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191126131656-8a8471f7e56d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -723,6 +723,8 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 h1:46ULzRKLh1CwgRq2dC5SlBzEqqNCi8rreOZnNrbqcIY= +golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -750,6 +752,7 @@ golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3 golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135 h1:5Beo0mZN8dRzgrMMkDp0jc8YXQKx9DiJ2k1dkvGsn5A= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200928182047-19e03678916f/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU= diff --git a/http/http.go b/http/http.go index 994d118..213c788 100644 --- a/http/http.go +++ b/http/http.go @@ -9,8 +9,8 @@ import ( "github.com/distribyted/distribyted/stats" "github.com/gin-contrib/static" "github.com/gin-gonic/gin" + "github.com/rs/zerolog/log" "github.com/shurcooL/httpfs/html/vfstemplate" - "github.com/sirupsen/logrus" ) func New(fc *filecache.Cache, ss *stats.Torrent, ch *config.Handler, port int) error { @@ -44,7 +44,7 @@ func New(fc *filecache.Cache, ss *stats.Torrent, ch *config.Handler, port int) e api.GET("/events", apiStreamEvents(eventChan)) } - logrus.WithField("host", fmt.Sprintf("0.0.0.0:%d", port)).Info("starting webserver") + log.Info().Str("host", fmt.Sprintf("0.0.0.0:%d", port)).Msg("starting webserver") if err := r.Run(fmt.Sprintf(":%d", port)); err != nil { return fmt.Errorf("error initializing server: %w", err) diff --git a/torrent/handler.go b/torrent/handler.go index e6c958e..688d400 100644 --- a/torrent/handler.go +++ b/torrent/handler.go @@ -8,7 +8,7 @@ import ( "github.com/distribyted/distribyted/config" "github.com/distribyted/distribyted/fs" "github.com/distribyted/distribyted/stats" - log "github.com/sirupsen/logrus" + "github.com/rs/zerolog/log" ) type Handler struct { @@ -49,14 +49,14 @@ func (s *Handler) Load(path string, ts []*config.Torrent) error { // only get info if name is not available if t.Name() == "" { - log.WithField("hash", t.InfoHash()).Info("getting torrent info") + log.Info().Str("hash", t.InfoHash().String()).Msg("getting torrent info") <-t.GotInfo() } s.s.Add(path, t) torrents = append(torrents, t) - log.WithField("name", t.Name()).WithField("path", path).Info("torrent added to mountpoint") + log.Info().Str("name", t.Name()).Str("path", path).Msg("torrent added to mountpoint") } folder := path diff --git a/webdav/http.go b/webdav/http.go index ff9eb28..24f5649 100644 --- a/webdav/http.go +++ b/webdav/http.go @@ -5,10 +5,10 @@ import ( "net/http" "github.com/distribyted/distribyted/fs" - "github.com/sirupsen/logrus" + "github.com/rs/zerolog/log" ) func NewWebDAVServer(fss map[string]fs.Filesystem, port int) error { - logrus.WithField("host", fmt.Sprintf("0.0.0.0:%d", port)).Info("starting webDAV server") + log.Info().Str("host", fmt.Sprintf("0.0.0.0:%d", port)).Msg("starting webDAV server") return http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), newHandler(fss)) }