Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Make logging configurable #24

Merged
merged 1 commit into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/unreleased/configurable-log.md
Original file line number Diff line number Diff line change
@@ -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
18 changes: 10 additions & 8 deletions pkg/command/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package command

import (
"github.com/owncloud/ocis-accounts/pkg/flagset"
"os"
"os/user"
"path"
Expand All @@ -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)
}
}
}
Expand All @@ -57,7 +59,7 @@ func Execute() error {
},

Commands: []*cli.Command{
Server(config.New()),
Server(rootCfg),
},
}

Expand All @@ -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),
)
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 27 additions & 0 deletions pkg/flagset/flagset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down