From 595b78f4876885cff31ba2a63cbe0f75b297d87c Mon Sep 17 00:00:00 2001 From: Adam Janikowski <12255597+ajanikow@users.noreply.github.com> Date: Mon, 23 Nov 2020 08:58:40 +0100 Subject: [PATCH] [Feature] Enable logger overrides (#672) --- CHANGELOG.md | 1 + main.go | 6 +++--- pkg/logging/logger.go | 34 ++++++++++++++++++++++------------ storage.go | 2 +- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 061a23f4c..8edbe3b93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## [master](https://github.com/arangodb/kube-arangodb/tree/master) (N/A) - Add v2alpha1 API for ArangoDeployment and ArangoDeploymentReplication - Migrate CRD to apiextensions.k8s.io/v1 +- Add customizable log levels per service ## [1.1.2](https://github.com/arangodb/kube-arangodb/tree/1.1.2) (2020-11-11) - Fix Bootstrap phase and move it under Plan diff --git a/main.go b/main.go index 9ac0b3009..faf4fc3a7 100644 --- a/main.go +++ b/main.go @@ -92,7 +92,7 @@ var ( Run: cmdMainRun, } - logLevel string + logLevels []string cliLog = logging.NewRootLogger() logService logging.Service serverOptions struct { @@ -131,7 +131,7 @@ func init() { f.StringVar(&serverOptions.tlsSecretName, "server.tls-secret-name", "", "Name of secret containing tls.crt & tls.key for HTTPS server (if empty, self-signed certificate is used)") f.StringVar(&serverOptions.adminSecretName, "server.admin-secret-name", defaultAdminSecretName, "Name of secret containing username + password for login to the dashboard") f.BoolVar(&serverOptions.allowAnonymous, "server.allow-anonymous-access", false, "Allow anonymous access to the dashboard") - f.StringVar(&logLevel, "log.level", defaultLogLevel, "Set initial log level") + f.StringArrayVar(&logLevels, "log.level", []string{defaultLogLevel}, "Set log levels in format or =") f.BoolVar(&operatorOptions.enableDeployment, "operator.deployment", false, "Enable to run the ArangoDeployment operator") f.BoolVar(&operatorOptions.enableDeploymentReplication, "operator.deployment-replication", false, "Enable to run the ArangoDeploymentReplication operator") f.BoolVar(&operatorOptions.enableStorage, "operator.storage", false, "Enable to run the ArangoLocalStorage operator") @@ -170,7 +170,7 @@ func cmdMainRun(cmd *cobra.Command, args []string) { // Prepare log service var err error - logService, err = logging.NewService(logLevel) + logService, err = logging.NewService(defaultLogLevel, logLevels) if err != nil { cliLog.Fatal().Err(err).Msg("Failed to initialize log service") } diff --git a/pkg/logging/logger.go b/pkg/logging/logger.go index 42a62e813..4386fd937 100644 --- a/pkg/logging/logger.go +++ b/pkg/logging/logger.go @@ -29,16 +29,9 @@ import ( "sync" "time" - "github.com/rs/zerolog" -) + "github.com/pkg/errors" -var ( - // The defaultLevels list is used during development to increase the - // default level for components that we care a little less about. - defaultLevels = map[string]string{ - //"operator": "info", - //"something.status": "info", - } + "github.com/rs/zerolog" ) // Service exposes the interfaces for a logger service @@ -70,7 +63,7 @@ func NewRootLogger() zerolog.Logger { } // NewService creates a new Service. -func NewService(defaultLevel string) (Service, error) { +func NewService(defaultLevel string, overrides []string) (Service, error) { l, err := stringToLevel(defaultLevel) if err != nil { return nil, maskAny(err) @@ -81,8 +74,25 @@ func NewService(defaultLevel string) (Service, error) { defaultLevel: l, levels: make(map[string]zerolog.Level), } - for k, v := range defaultLevels { - s.MustSetLevel(k, v) + + for _, override := range overrides { + levelParts := strings.Split(override, "=") + switch size := len(levelParts); size { + case 1: + level, err := stringToLevel(levelParts[0]) + if err != nil { + return nil, maskAny(err) + } + s.defaultLevel = level + case 2: + level, err := stringToLevel(levelParts[1]) + if err != nil { + return nil, maskAny(err) + } + s.levels[levelParts[0]] = level + default: + return nil, errors.Errorf("invalid log definition %s: Length %d is not equal 1 or 2", override, size) + } } return s, nil } diff --git a/storage.go b/storage.go index 012add478..36f39c9c3 100644 --- a/storage.go +++ b/storage.go @@ -64,7 +64,7 @@ func init() { func cmdStorageProvisionerRun(cmd *cobra.Command, args []string) { //goflag.CommandLine.Parse([]string{"-logtostderr"}) var err error - logService, err = logging.NewService(logLevel) + logService, err = logging.NewService(defaultLogLevel, logLevels) if err != nil { cliLog.Fatal().Err(err).Msg("Failed to initialize log service") }