diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cf2749f84..1f2be334d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,27 @@ -# Changes in 0.1.0 +# Changelog for [0.1.1] (2020-04-29) + +The following sections list the changes in ocis-accounts 0.1.1. + +[0.1.1]: https://github.com/owncloud/ocis-accounts/compare/v0.1.0...v0.1.1 + +## 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 diff --git a/changelog/0.1.1_2020-04-29/configurable-log.md b/changelog/0.1.1_2020-04-29/configurable-log.md new file mode 100644 index 0000000000..41a4d5e466 --- /dev/null +++ b/changelog/0.1.1_2020-04-29/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{