From 074eb8d9823975a68f2b9eb1f0c5b090e56f8cd2 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Fri, 5 Mar 2021 14:28:38 +0100 Subject: [PATCH] propagate logging using config options --- ocis/pkg/runtime/runtime.go | 45 ++++++++--------------- storage/pkg/command/authbasic.go | 12 +++++- storage/pkg/command/authbearer.go | 12 +++++- storage/pkg/command/frontend.go | 12 +++++- storage/pkg/command/gateway.go | 12 +++++- storage/pkg/command/groups.go | 12 +++++- storage/pkg/command/options.go | 47 ++++++++++++++++++++++++ storage/pkg/command/sharing.go | 12 +++++- storage/pkg/command/storagehome.go | 12 +++++- storage/pkg/command/storagemetadata.go | 11 +++++- storage/pkg/command/storagepubliclink.go | 12 +++++- storage/pkg/command/storageusers.go | 12 +++++- storage/pkg/command/users.go | 12 +++++- 13 files changed, 183 insertions(+), 40 deletions(-) create mode 100644 storage/pkg/command/options.go diff --git a/ocis/pkg/runtime/runtime.go b/ocis/pkg/runtime/runtime.go index bd83771995..b079ff4a06 100644 --- a/ocis/pkg/runtime/runtime.go +++ b/ocis/pkg/runtime/runtime.go @@ -88,28 +88,15 @@ func (r *Runtime) Start() error { supervisor := suture.NewSimple("ocis") globalCtx, globalCancel := context.WithCancel(context.Background()) - // TODO(refs + jfd) - // - to avoid this getting out of hands, a supervisor would need to be injected on each supervised service. - // - each service would then add its execute func to the supervisor, and return its token (?) - // - this runtime should only care about start / stop services, for that we use serviceTokens. - // - normalize the use of panics so that suture can restart services that die. - // - shutting down a service implies iterating over all the serviceToken for the given service name and terminating them. - // - config file parsing with Viper is no longer possible as viper is not thread-safe (https://github.com/spf13/viper/issues/19) - // - replace occurrences of log.Fatal in favor of panic() since the supervisor relies on panics. - // - the runtime should ideally run as an rpc service one can do requests, like the good ol' pman, rest in pieces. - // - establish on suture a max number of retries before all initialization comes to a halt. - // - remove default log flagset values. - // - subcommands MUST also set MICRO_LOG_LEVEL to error. - // - 2021-03-04T14:06:37+01:00 FTL failed to read config error="open /Users/aunger/.ocis/idp.env: no such file or directory" service=idp still exists - // - normalize flag parsing (and fix hack of renaming ocis top level for the destination side effect) - // propagate reva log config to storage services - r.c.Storage.Log.Level = r.c.Log.Level - r.c.Storage.Log.Color = r.c.Log.Color - r.c.Storage.Log.Pretty = r.c.Log.Pretty + inheritedOptions := []storage.Option{ + storage.WithLogPretty(r.c.Log.Pretty), + storage.WithLogColor(r.c.Log.Color), + storage.WithLogLevel(r.c.Log.Level), + } addServiceToken("settings", supervisor.Add(settings.NewSutureService(globalCtx, r.c.Settings))) - addServiceToken("storage-metadata", supervisor.Add(storage.NewStorageMetadata(globalCtx))) + addServiceToken("storage-metadata", supervisor.Add(storage.NewStorageMetadata(globalCtx, inheritedOptions...))) addServiceToken("accounts", supervisor.Add(accounts.NewSutureService(globalCtx, r.c.Accounts))) addServiceToken("glauth", supervisor.Add(glauth.NewSutureService(globalCtx, r.c.GLAuth))) addServiceToken("idp", supervisor.Add(idp.NewSutureService(globalCtx, r.c.IDP))) @@ -120,16 +107,16 @@ func (r *Runtime) Start() error { addServiceToken("thumbnails", supervisor.Add(thumbnails.NewSutureService(globalCtx, r.c.Thumbnails))) addServiceToken("web", supervisor.Add(web.NewSutureService(globalCtx, r.c.Web))) addServiceToken("webdav", supervisor.Add(webdav.NewSutureService(globalCtx, r.c.WebDAV))) - addServiceToken("storage-frontend", supervisor.Add(storage.NewFrontend(globalCtx))) - addServiceToken("storage-gateway", supervisor.Add(storage.NewGateway(globalCtx))) - addServiceToken("storage-users", supervisor.Add(storage.NewUsersProviderService(globalCtx))) - addServiceToken("storage-groupsprovider", supervisor.Add(storage.NewGroupsProvider(globalCtx))) // TODO(refs) panic? are we sending to a nil / closed channel? - addServiceToken("storage-authbasic", supervisor.Add(storage.NewAuthBasic(globalCtx))) - addServiceToken("storage-authbearer", supervisor.Add(storage.NewAuthBearer(globalCtx))) - addServiceToken("storage-home", supervisor.Add(storage.NewStorageHome(globalCtx))) - addServiceToken("storage-users", supervisor.Add(storage.NewStorageUsers(globalCtx))) - addServiceToken("storage-public-link", supervisor.Add(storage.NewStoragePublicLink(globalCtx))) - addServiceToken("storage-sharing", supervisor.Add(storage.NewSharing(globalCtx))) + addServiceToken("storage-frontend", supervisor.Add(storage.NewFrontend(globalCtx, inheritedOptions...))) + addServiceToken("storage-gateway", supervisor.Add(storage.NewGateway(globalCtx, inheritedOptions...))) + addServiceToken("storage-users", supervisor.Add(storage.NewUsersProviderService(globalCtx, inheritedOptions...))) + addServiceToken("storage-groupsprovider", supervisor.Add(storage.NewGroupsProvider(globalCtx, inheritedOptions...))) // TODO(refs) panic? are we sending to a nil / closed channel? + addServiceToken("storage-authbasic", supervisor.Add(storage.NewAuthBasic(globalCtx, inheritedOptions...))) + addServiceToken("storage-authbearer", supervisor.Add(storage.NewAuthBearer(globalCtx, inheritedOptions...))) + addServiceToken("storage-home", supervisor.Add(storage.NewStorageHome(globalCtx, inheritedOptions...))) + addServiceToken("storage-users", supervisor.Add(storage.NewStorageUsers(globalCtx, inheritedOptions...))) + addServiceToken("storage-public-link", supervisor.Add(storage.NewStoragePublicLink(globalCtx, inheritedOptions...))) + addServiceToken("storage-sharing", supervisor.Add(storage.NewSharing(globalCtx, inheritedOptions...))) // TODO(refs) debug line with supervised services. go supervisor.ServeBackground() diff --git a/storage/pkg/command/authbasic.go b/storage/pkg/command/authbasic.go index 8a797a4d93..f24349a957 100644 --- a/storage/pkg/command/authbasic.go +++ b/storage/pkg/command/authbasic.go @@ -208,9 +208,19 @@ type AuthBasicSutureService struct { } // NewAuthBasicSutureService creates a new store.AuthBasicSutureService -func NewAuthBasic(ctx context.Context) AuthBasicSutureService { +func NewAuthBasic(ctx context.Context, o ...Option) AuthBasicSutureService { sctx, cancel := context.WithCancel(ctx) cfg := config.New() + + opts := newOptions(o...) + + // merge config and options + cfg.Context = sctx + + cfg.Log.Level = opts.LogLevel + cfg.Log.Pretty = opts.LogPretty + cfg.Log.Color = opts.LogColor + cfg.Context = sctx return AuthBasicSutureService{ ctx: sctx, diff --git a/storage/pkg/command/authbearer.go b/storage/pkg/command/authbearer.go index 35a37c1b43..44548664f0 100644 --- a/storage/pkg/command/authbearer.go +++ b/storage/pkg/command/authbearer.go @@ -188,9 +188,19 @@ type AuthBearerSutureService struct { } // NewAuthBearerSutureService creates a new gateway.AuthBearerSutureService -func NewAuthBearer(ctx context.Context) AuthBearerSutureService { +func NewAuthBearer(ctx context.Context, o ...Option) AuthBearerSutureService { sctx, cancel := context.WithCancel(ctx) cfg := config.New() + + opts := newOptions(o...) + + // merge config and options + cfg.Context = sctx + + cfg.Log.Level = opts.LogLevel + cfg.Log.Pretty = opts.LogPretty + cfg.Log.Color = opts.LogColor + cfg.Context = sctx return AuthBearerSutureService{ ctx: sctx, diff --git a/storage/pkg/command/frontend.go b/storage/pkg/command/frontend.go index f15cd2ccfd..bcae5e92c6 100644 --- a/storage/pkg/command/frontend.go +++ b/storage/pkg/command/frontend.go @@ -336,9 +336,19 @@ type FrontendSutureService struct { } // NewFrontendSutureService creates a new frontend.FrontendSutureService -func NewFrontend(ctx context.Context) FrontendSutureService { +func NewFrontend(ctx context.Context, o ...Option) FrontendSutureService { sctx, cancel := context.WithCancel(ctx) cfg := config.New() + + opts := newOptions(o...) + + // merge config and options + cfg.Context = sctx + + cfg.Log.Level = opts.LogLevel + cfg.Log.Pretty = opts.LogPretty + cfg.Log.Color = opts.LogColor + cfg.Context = sctx return FrontendSutureService{ ctx: sctx, diff --git a/storage/pkg/command/gateway.go b/storage/pkg/command/gateway.go index 1e996613a4..ba04f15dc0 100644 --- a/storage/pkg/command/gateway.go +++ b/storage/pkg/command/gateway.go @@ -265,9 +265,19 @@ type GatewaySutureService struct { } // NewGatewaySutureService creates a new gateway.GatewaySutureService -func NewGateway(ctx context.Context) GatewaySutureService { +func NewGateway(ctx context.Context, o ...Option) GatewaySutureService { sctx, cancel := context.WithCancel(ctx) cfg := config.New() + + opts := newOptions(o...) + + // merge config and options + cfg.Context = sctx + + cfg.Log.Level = opts.LogLevel + cfg.Log.Pretty = opts.LogPretty + cfg.Log.Color = opts.LogColor + cfg.Context = sctx return GatewaySutureService{ ctx: sctx, diff --git a/storage/pkg/command/groups.go b/storage/pkg/command/groups.go index e4998d33e3..ebef95c9a0 100644 --- a/storage/pkg/command/groups.go +++ b/storage/pkg/command/groups.go @@ -222,9 +222,19 @@ type GroupsProvider struct { } // NewGroupsProvider creates a new storage.GroupsProvider -func NewGroupsProvider(ctx context.Context) GroupsProvider { +func NewGroupsProvider(ctx context.Context, o ...Option) GroupsProvider { sctx, cancel := context.WithCancel(ctx) cfg := config.New() + + opts := newOptions(o...) + + // merge config and options + cfg.Context = sctx + + cfg.Log.Level = opts.LogLevel + cfg.Log.Pretty = opts.LogPretty + cfg.Log.Color = opts.LogColor + cfg.Context = sctx return GroupsProvider{ ctx: sctx, diff --git a/storage/pkg/command/options.go b/storage/pkg/command/options.go new file mode 100644 index 0000000000..e9e4467de4 --- /dev/null +++ b/storage/pkg/command/options.go @@ -0,0 +1,47 @@ +package command + +// Option defines a single modifier to an Options attribute. +type Option func(o *Options) + +type Options struct { + // LogPretty toggles pretty logging lines. + LogPretty bool + + // LogColor toggles colored output. + LogColor bool + + // LogLevel raises / decreases logging levels. + LogLevel string +} + +// newOptions initializes the available default options. +func newOptions(opts ...Option) Options { + opt := Options{} + + for _, o := range opts { + o(&opt) + } + + return opt +} + +// WithLogPretty toggles pretty output for a storage logger. +func WithLogPretty(v bool) Option { + return func(o *Options) { + o.LogPretty = v + } +} + +// WithLogColor toggles colored output for a storage logger. +func WithLogColor(v bool) Option { + return func(o *Options) { + o.LogColor = v + } +} + +// WithLogLevel toggles colored output for a storage logger. +func WithLogLevel(v string) Option { + return func(o *Options) { + o.LogLevel = v + } +} diff --git a/storage/pkg/command/sharing.go b/storage/pkg/command/sharing.go index 1d78c5dbd3..38cde98010 100644 --- a/storage/pkg/command/sharing.go +++ b/storage/pkg/command/sharing.go @@ -211,9 +211,19 @@ type SharingSutureService struct { } // NewSharingSutureService creates a new store.SharingSutureService -func NewSharing(ctx context.Context) SharingSutureService { +func NewSharing(ctx context.Context, o ...Option) SharingSutureService { sctx, cancel := context.WithCancel(ctx) cfg := config.New() + + opts := newOptions(o...) + + // merge config and options + cfg.Context = sctx + + cfg.Log.Level = opts.LogLevel + cfg.Log.Pretty = opts.LogPretty + cfg.Log.Color = opts.LogColor + cfg.Context = sctx return SharingSutureService{ ctx: sctx, diff --git a/storage/pkg/command/storagehome.go b/storage/pkg/command/storagehome.go index 02333e712f..51aad24a89 100644 --- a/storage/pkg/command/storagehome.go +++ b/storage/pkg/command/storagehome.go @@ -207,9 +207,19 @@ type StorageHomeSutureService struct { } // NewStorageHomeSutureService creates a new storage.StorageHomeSutureService -func NewStorageHome(ctx context.Context) StorageHomeSutureService { +func NewStorageHome(ctx context.Context, o ...Option) StorageHomeSutureService { sctx, cancel := context.WithCancel(ctx) cfg := config.New() + + opts := newOptions(o...) + + // merge config and options + cfg.Context = sctx + + cfg.Log.Level = opts.LogLevel + cfg.Log.Pretty = opts.LogPretty + cfg.Log.Color = opts.LogColor + cfg.Context = sctx return StorageHomeSutureService{ ctx: sctx, diff --git a/storage/pkg/command/storagemetadata.go b/storage/pkg/command/storagemetadata.go index 3c1589ddab..4517efc014 100644 --- a/storage/pkg/command/storagemetadata.go +++ b/storage/pkg/command/storagemetadata.go @@ -228,10 +228,19 @@ type SutureService struct { } // NewSutureService creates a new storagemetadata.SutureService -func NewStorageMetadata(ctx context.Context) SutureService { +func NewStorageMetadata(ctx context.Context, o ...Option) SutureService { sctx, cancel := context.WithCancel(ctx) cfg := config.New() + + opts := newOptions(o...) + + // merge config and options cfg.Context = sctx + + cfg.Log.Level = opts.LogLevel + cfg.Log.Pretty = opts.LogPretty + cfg.Log.Color = opts.LogColor + return SutureService{ ctx: sctx, cancel: cancel, diff --git a/storage/pkg/command/storagepubliclink.go b/storage/pkg/command/storagepubliclink.go index f2c3febd1f..cbd3d8e87f 100644 --- a/storage/pkg/command/storagepubliclink.go +++ b/storage/pkg/command/storagepubliclink.go @@ -185,9 +185,19 @@ type StoragePublicLinkSutureService struct { } // NewStoragePublicLinkSutureService creates a new storage.StoragePublicLinkSutureService -func NewStoragePublicLink(ctx context.Context) StoragePublicLinkSutureService { +func NewStoragePublicLink(ctx context.Context, o ...Option) StoragePublicLinkSutureService { sctx, cancel := context.WithCancel(ctx) cfg := config.New() + + opts := newOptions(o...) + + // merge config and options + cfg.Context = sctx + + cfg.Log.Level = opts.LogLevel + cfg.Log.Pretty = opts.LogPretty + cfg.Log.Color = opts.LogColor + cfg.Context = sctx return StoragePublicLinkSutureService{ ctx: sctx, diff --git a/storage/pkg/command/storageusers.go b/storage/pkg/command/storageusers.go index c0daccb3e0..76036e5327 100644 --- a/storage/pkg/command/storageusers.go +++ b/storage/pkg/command/storageusers.go @@ -207,9 +207,19 @@ type StorageUsersSutureService struct { } // NewStorageUsersSutureService creates a new storage.StorageUsersSutureService -func NewStorageUsers(ctx context.Context) StorageUsersSutureService { +func NewStorageUsers(ctx context.Context, o ...Option) StorageUsersSutureService { sctx, cancel := context.WithCancel(ctx) cfg := config.New() + + opts := newOptions(o...) + + // merge config and options + cfg.Context = sctx + + cfg.Log.Level = opts.LogLevel + cfg.Log.Pretty = opts.LogPretty + cfg.Log.Color = opts.LogColor + cfg.Context = sctx return StorageUsersSutureService{ ctx: sctx, diff --git a/storage/pkg/command/users.go b/storage/pkg/command/users.go index 59edaedcfe..f76270692a 100644 --- a/storage/pkg/command/users.go +++ b/storage/pkg/command/users.go @@ -223,9 +223,19 @@ type UsersProviderService struct { } // NewUsersProviderService creates a new storage.UsersProviderService -func NewUsersProviderService(ctx context.Context) UsersProviderService { +func NewUsersProviderService(ctx context.Context, o ...Option) UsersProviderService { sctx, cancel := context.WithCancel(ctx) cfg := config.New() + + opts := newOptions(o...) + + // merge config and options + cfg.Context = sctx + + cfg.Log.Level = opts.LogLevel + cfg.Log.Pretty = opts.LogPretty + cfg.Log.Color = opts.LogColor + cfg.Context = sctx return UsersProviderService{ ctx: sctx,