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

Commit

Permalink
Make logging configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
IljaN committed Apr 29, 2020
1 parent 8847024 commit 86554fc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
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

0 comments on commit 86554fc

Please sign in to comment.