From bb7e4b124be58ffe3868afce440332bcaba40a2a Mon Sep 17 00:00:00 2001 From: Adphi Date: Sat, 2 Jul 2022 12:39:52 +0200 Subject: [PATCH] logger: add FromLogrus service: use logger instead of logrus Signed-off-by: Adphi --- logger/logger.go | 4 ++++ registry/mdns/server.go | 4 ++-- service/service.go | 19 ++++++++++--------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/logger/logger.go b/logger/logger.go index 98a1231..7c4a5e3 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -21,6 +21,10 @@ func New() Logger { return &logger{fl: logrus.New()} } +func FromLogrus(fl logrus.FieldLogger) Logger { + return &logger{fl: fl} +} + type Logger interface { WithField(key string, value interface{}) Logger WithFields(kv ...interface{}) Logger diff --git a/registry/mdns/server.go b/registry/mdns/server.go index bdd9bf6..55d6573 100644 --- a/registry/mdns/server.go +++ b/registry/mdns/server.go @@ -194,7 +194,7 @@ func (s *Server) recv(c *net.UDPConn) { continue } if err := s.parsePacket(buf[:n], from); err != nil { - logrus.Errorf("[ERR] mdns: Failed to handle query: %v", err) + logrus.Errorf("mdns: Failed to handle query: %v", err) } } } @@ -203,7 +203,7 @@ func (s *Server) recv(c *net.UDPConn) { func (s *Server) parsePacket(packet []byte, from net.Addr) error { var msg dns.Msg if err := msg.Unpack(packet); err != nil { - logrus.Errorf("[ERR] mdns: Failed to unpack packet: %v", err) + logrus.Errorf("mdns: Failed to unpack packet: %v", err) return err } // TODO: This is a bit of a hack diff --git a/service/service.go b/service/service.go index 3e2cdd4..e132976 100644 --- a/service/service.go +++ b/service/service.go @@ -20,7 +20,6 @@ import ( "github.com/jinzhu/gorm" "github.com/justinas/alice" "github.com/rs/cors" - "github.com/sirupsen/logrus" "github.com/soheilhy/cmux" "go.uber.org/multierr" "google.golang.org/grpc" @@ -29,6 +28,7 @@ import ( greflect "google.golang.org/grpc/reflection" "go.linka.cloud/grpc/interceptors/metadata" + "go.linka.cloud/grpc/logger" "go.linka.cloud/grpc/registry" "go.linka.cloud/grpc/registry/noop" ) @@ -240,11 +240,11 @@ func (s *service) run() error { select { case sig := <-sigs: fmt.Println() - logrus.Warnf("received %v", sig) + logger.C(s.opts.ctx).Warnf("received %v", sig) return s.Close() case err := <-errs: if err != nil && !ignoreMuxError(err) { - logrus.Error(err) + logger.C(s.opts.ctx).Error(err) return err } return nil @@ -256,6 +256,7 @@ func (s *service) Start() error { } func (s *service) Stop() error { + log := logger.C(s.opts.ctx) s.mu.Lock() defer s.mu.Unlock() if !s.running { @@ -267,7 +268,7 @@ func (s *service) Stop() error { } } if err := s.opts.registry.Deregister(s.regSvc); err != nil { - logrus.Errorf("failed to deregister service: %v", err) + log.Errorf("failed to deregister service: %v", err) } defer close(s.closed) sigs := s.notify() @@ -279,14 +280,14 @@ func (s *service) Stop() error { // catch: Drain() is not implemented recover() }() - logrus.Warn("shutting down gracefully") + log.Warn("shutting down gracefully") s.server.GracefulStop() }() select { case sig := <-sigs: fmt.Println() - logrus.Warnf("received %v", sig) - logrus.Warn("forcing shutdown") + log.Warnf("received %v", sig) + log.Warn("forcing shutdown") s.server.Stop() case <-done: } @@ -297,7 +298,7 @@ func (s *service) Stop() error { return err } } - logrus.Info("server stopped") + log.Info("server stopped") return nil } @@ -322,7 +323,7 @@ func (s *service) registerService(sd *grpc.ServiceDesc, ss interface{}) { s.inproc.RegisterService(sd, ss) if _, ok := s.services[sd.ServiceName]; ok { - logrus.Fatalf("grpc: Service.RegisterService found duplicate service registration for %q", sd.ServiceName) + logger.C(s.opts.ctx).Fatalf("grpc: Service.RegisterService found duplicate service registration for %q", sd.ServiceName) } info := &serviceInfo{ serviceImpl: ss,