From e7f9fb3bcb58eb8ac8bfab4ef99874f746e0c4e1 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Mon, 8 Nov 2021 19:38:40 +0100 Subject: [PATCH] refactor NewLogger away to ocis-pkg --- accounts/pkg/command/root.go | 12 ------------ accounts/pkg/command/server.go | 4 +++- accounts/pkg/proto/v0/accounts.pb.micro_test.go | 5 ++--- ocis-pkg/log/log.go | 13 +++++++++++++ ocs/pkg/server/http/svc_test.go | 4 ++-- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/accounts/pkg/command/root.go b/accounts/pkg/command/root.go index 5b380faea5..910a9240c1 100644 --- a/accounts/pkg/command/root.go +++ b/accounts/pkg/command/root.go @@ -8,7 +8,6 @@ import ( "github.com/owncloud/ocis/accounts/pkg/config" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/ocis-pkg/version" "github.com/thejerf/suture/v4" "github.com/urfave/cli/v2" @@ -57,17 +56,6 @@ func Execute(cfg *config.Config) error { return app.Run(os.Args) } -// NewLogger initializes a service-specific logger instance. -func NewLogger(cfg *config.Config) log.Logger { - return log.NewLogger( - log.Name("accounts"), - log.Level(cfg.Log.Level), - log.Pretty(cfg.Log.Pretty), - log.Color(cfg.Log.Color), - log.File(cfg.Log.File), - ) -} - // ParseConfig loads accounts configuration from known paths. func ParseConfig(c *cli.Context, cfg *config.Config) error { conf, err := ociscfg.BindSourcesToStructs("accounts", cfg) diff --git a/accounts/pkg/command/server.go b/accounts/pkg/command/server.go index 360e9c7ac9..bb820b921f 100644 --- a/accounts/pkg/command/server.go +++ b/accounts/pkg/command/server.go @@ -4,6 +4,8 @@ import ( "context" "strings" + "github.com/owncloud/ocis/ocis-pkg/log" + gofig "github.com/gookit/config/v2" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" "github.com/owncloud/ocis/ocis-pkg/shared" @@ -55,7 +57,7 @@ func Server(cfg *config.Config) *cli.Command { return nil }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := log.LoggerFromConfig("accounts", cfg.Log) err := tracing.Configure(cfg) if err != nil { return err diff --git a/accounts/pkg/proto/v0/accounts.pb.micro_test.go b/accounts/pkg/proto/v0/accounts.pb.micro_test.go index deb52596e8..3a4054d041 100644 --- a/accounts/pkg/proto/v0/accounts.pb.micro_test.go +++ b/accounts/pkg/proto/v0/accounts.pb.micro_test.go @@ -12,11 +12,10 @@ import ( mgrpcc "github.com/asim/go-micro/plugins/client/grpc/v4" empty "github.com/golang/protobuf/ptypes/empty" - - "github.com/owncloud/ocis/accounts/pkg/command" "github.com/owncloud/ocis/accounts/pkg/config" "github.com/owncloud/ocis/accounts/pkg/proto/v0" svc "github.com/owncloud/ocis/accounts/pkg/service/v0" + oclog "github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/ocis-pkg/service/grpc" settings "github.com/owncloud/ocis/settings/pkg/proto/v0" "github.com/stretchr/testify/assert" @@ -86,7 +85,7 @@ func init() { var hdlr *svc.Service var err error - if hdlr, err = svc.New(svc.Logger(command.NewLogger(cfg)), svc.Config(cfg), svc.RoleService(buildRoleServiceMock())); err != nil { + if hdlr, err = svc.New(svc.Logger(oclog.LoggerFromConfig("accounts", cfg.Log)), svc.Config(cfg), svc.RoleService(buildRoleServiceMock())); err != nil { log.Fatalf("Could not create new service") } diff --git a/ocis-pkg/log/log.go b/ocis-pkg/log/log.go index 0436391955..42ae508f82 100644 --- a/ocis-pkg/log/log.go +++ b/ocis-pkg/log/log.go @@ -6,6 +6,8 @@ import ( "strings" "time" + "github.com/owncloud/ocis/ocis-pkg/shared" + "github.com/rs/zerolog" "github.com/rs/zerolog/log" mdlog "go-micro.dev/v4/debug/log" @@ -18,6 +20,17 @@ type Logger struct { zerolog.Logger } +// LoggerFromConfig initializes a service-specific logger instance. +func LoggerFromConfig(name string, cfg shared.Log) Logger { + return NewLogger( + Name(name), + Level(cfg.Level), + Pretty(cfg.Pretty), + Color(cfg.Color), + File(cfg.File), + ) +} + // NewLogger initializes a new logger instance. func NewLogger(opts ...Option) Logger { options := newOptions(opts...) diff --git a/ocs/pkg/server/http/svc_test.go b/ocs/pkg/server/http/svc_test.go index 24ffb6e1d5..32e38a9c11 100644 --- a/ocs/pkg/server/http/svc_test.go +++ b/ocs/pkg/server/http/svc_test.go @@ -22,11 +22,11 @@ import ( "github.com/cs3org/reva/pkg/token" "github.com/cs3org/reva/pkg/token/manager/jwt" "github.com/golang/protobuf/ptypes/empty" - accountsCmd "github.com/owncloud/ocis/accounts/pkg/command" accountsCfg "github.com/owncloud/ocis/accounts/pkg/config" accountsProto "github.com/owncloud/ocis/accounts/pkg/proto/v0" accountsSvc "github.com/owncloud/ocis/accounts/pkg/service/v0" ocisLog "github.com/owncloud/ocis/ocis-pkg/log" + oclog "github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/ocis-pkg/service/grpc" "github.com/owncloud/ocis/ocs/pkg/config" svc "github.com/owncloud/ocis/ocs/pkg/service/v0" @@ -562,7 +562,7 @@ func init() { var err error if hdlr, err = accountsSvc.New( - accountsSvc.Logger(accountsCmd.NewLogger(c)), + accountsSvc.Logger(oclog.LoggerFromConfig("accounts", c.Log)), accountsSvc.Config(c), accountsSvc.RoleService(buildRoleServiceMock()), ); err != nil {