From b7fb179decca93af25605d4e11f1a95b61e52188 Mon Sep 17 00:00:00 2001 From: Ilja Neumann Date: Wed, 29 Apr 2020 13:55:13 +0200 Subject: [PATCH 1/4] Make logging configurable --- changelog/unreleased/configurable-log.md | 5 +++++ pkg/command/root.go | 18 +++++++++------- pkg/config/config.go | 8 +++++++ pkg/flagset/flagset.go | 27 ++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 changelog/unreleased/configurable-log.md diff --git a/changelog/unreleased/configurable-log.md b/changelog/unreleased/configurable-log.md new file mode 100644 index 0000000000..41a4d5e466 --- /dev/null +++ b/changelog/unreleased/configurable-log.md @@ -0,0 +1,5 @@ +Enhancement: Logging is configurable + +ACCOUNTS_LOG_* env-vars or cli-flags can be used for logging configuration. See --help for more details. + +https://github.com/owncloud/ocis-accounts/pull/24 diff --git a/pkg/command/root.go b/pkg/command/root.go index bc0062a4f5..85308f7bdb 100644 --- a/pkg/command/root.go +++ b/pkg/command/root.go @@ -1,6 +1,7 @@ package command import ( + "github.com/owncloud/ocis-accounts/pkg/flagset" "os" "os/user" "path" @@ -24,25 +25,26 @@ var ( // Execute is the entry point for the ocis-accounts command. func Execute() error { + rootCfg := config.New() app := &cli.App{ Name: "ocis-accounts", Version: version.String, Usage: "Example service for Reva/oCIS", - + Flags: flagset.RootWithConfig(rootCfg), Before: func(c *cli.Context) error { - log := NewLogger(config.New()) + logger := NewLogger(config.New()) for _, v := range defaultConfigPaths { // location is the user's home if v[0] == '$' || v[0] == '~' { usr, _ := user.Current() err := godotenv.Load(path.Join(usr.HomeDir, ".ocis", defaultFilename+".env")) if err != nil { - log.Debug().Msgf("ignoring missing env file on dir: %v", v) + logger.Debug().Msgf("ignoring missing env file on dir: %v", v) } } else { err := godotenv.Load(path.Join(v, defaultFilename+".env")) if err != nil { - log.Debug().Msgf("ignoring missing env file on dir: %v", v) + logger.Debug().Msgf("ignoring missing env file on dir: %v", v) } } } @@ -57,7 +59,7 @@ func Execute() error { }, Commands: []*cli.Command{ - Server(config.New()), + Server(rootCfg), }, } @@ -78,9 +80,9 @@ func Execute() error { func NewLogger(cfg *config.Config) log.Logger { return log.NewLogger( log.Name("accounts"), - log.Level("info"), - log.Pretty(true), - log.Color(true), + log.Level(cfg.Log.Level), + log.Pretty(cfg.Log.Pretty), + log.Color(cfg.Log.Color), ) } diff --git a/pkg/config/config.go b/pkg/config/config.go index 66dadf2ad6..6c419c987d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -8,11 +8,19 @@ type Server struct { Address string } +// Log defines the available logging configuration. +type Log struct { + Level string + Pretty bool + Color bool +} + // Config merges all Account config parameters. type Config struct { MountPath string Manager string Server Server + Log Log } // New returns a new config. diff --git a/pkg/flagset/flagset.go b/pkg/flagset/flagset.go index bbf5012e41..16dbe5d7d1 100644 --- a/pkg/flagset/flagset.go +++ b/pkg/flagset/flagset.go @@ -3,6 +3,33 @@ package flagset import "github.com/micro/cli/v2" import "github.com/owncloud/ocis-accounts/pkg/config" +// RootWithConfig applies cfg to the root flagset +func RootWithConfig(cfg *config.Config) []cli.Flag { + return []cli.Flag{ + &cli.StringFlag{ + Name: "log-level", + Value: "info", + Usage: "Set logging level", + EnvVars: []string{"ACCOUNTS_LOG_LEVEL"}, + Destination: &cfg.Log.Level, + }, + &cli.BoolFlag{ + Value: true, + Name: "log-pretty", + Usage: "Enable pretty logging", + EnvVars: []string{"ACCOUNTS_LOG_PRETTY"}, + Destination: &cfg.Log.Pretty, + }, + &cli.BoolFlag{ + Value: true, + Name: "log-color", + Usage: "Enable colored logging", + EnvVars: []string{"ACCOUNTS_LOG_COLOR"}, + Destination: &cfg.Log.Color, + }, + } +} + // ServerWithConfig applies cfg to the root flagset func ServerWithConfig(cfg *config.Config) []cli.Flag { return []cli.Flag{ From 17eaaa52886d950ea1f0841029f985179571f598 Mon Sep 17 00:00:00 2001 From: Ilja Neumann Date: Wed, 29 Apr 2020 12:21:21 +0000 Subject: [PATCH 2/4] Automated changelog update [skip ci] --- CHANGELOG.md | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cf2749f84..b6ceba077b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,27 @@ -# Changes in 0.1.0 +# Changelog for [unreleased] (UNRELEASED) + +The following sections list the changes in ocis-accounts unreleased. + +[unreleased]: https://github.com/owncloud/ocis-accounts/compare/v0.1.0...master + +## Summary + +* Enhancement - Logging is configurable: [#24](https://github.com/owncloud/ocis-accounts/pull/24) + +## Details + +* Enhancement - Logging is configurable: [#24](https://github.com/owncloud/ocis-accounts/pull/24) + + ACCOUNTS_LOG_* env-vars or cli-flags can be used for logging configuration. See --help for + more details. + + https://github.com/owncloud/ocis-accounts/pull/24 + +# Changelog for [0.1.0] (2020-03-18) + +The following sections list the changes in ocis-accounts 0.1.0. + +[0.1.0]: https://github.com/owncloud/ocis-accounts/compare/500e303cb544ed93d84153f01219d77eeee44929...v0.1.0 ## Summary From 430f7a9f86ade1fb6331cdbc82712331f3f65367 Mon Sep 17 00:00:00 2001 From: Ilja Neumann Date: Wed, 29 Apr 2020 18:05:43 +0200 Subject: [PATCH 3/4] Prepare release 0.1.1 --- changelog/{unreleased => 0.1.1_2020-04-29}/configurable-log.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename changelog/{unreleased => 0.1.1_2020-04-29}/configurable-log.md (100%) diff --git a/changelog/unreleased/configurable-log.md b/changelog/0.1.1_2020-04-29/configurable-log.md similarity index 100% rename from changelog/unreleased/configurable-log.md rename to changelog/0.1.1_2020-04-29/configurable-log.md From 34c252647382af89ccb5f1fc4a464c642a8c42f1 Mon Sep 17 00:00:00 2001 From: Michael Barz Date: Wed, 29 Apr 2020 20:13:39 +0000 Subject: [PATCH 4/4] Automated changelog update [skip ci] --- CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6ceba077b..1f2be334d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,8 @@ -# Changelog for [unreleased] (UNRELEASED) +# Changelog for [0.1.1] (2020-04-29) -The following sections list the changes in ocis-accounts unreleased. +The following sections list the changes in ocis-accounts 0.1.1. -[unreleased]: https://github.com/owncloud/ocis-accounts/compare/v0.1.0...master +[0.1.1]: https://github.com/owncloud/ocis-accounts/compare/v0.1.0...v0.1.1 ## Summary