Skip to content

Commit

Permalink
Add --log-format and --log-level flags to start cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
krhubert committed Aug 15, 2018
1 parent 5f369c7 commit 3228394
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
37 changes: 37 additions & 0 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (

"github.com/logrusorgru/aurora"
"github.com/mesg-foundation/core/cmd/utils"
"github.com/mesg-foundation/core/config"
"github.com/mesg-foundation/core/container"
"github.com/mesg-foundation/core/daemon"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// Start the MESG Core.
Expand All @@ -18,7 +21,41 @@ var Start = &cobra.Command{
DisableAutoGenTag: true,
}

// logFormatValue reprenets log format flag value.
type logFormatValue string

func (v *logFormatValue) Set(value string) error {
if value != "text" && value != "json" {
return fmt.Errorf("%s is not valid log format", value)
}
viper.Set(config.LogFormat, value)
*v = logFormatValue(value)
return nil
}
func (v *logFormatValue) Type() string { return "string" }
func (v *logFormatValue) String() string { return string(*v) }

// logLevelValue reprenets log level flag value.
type logLevelValue string

func (v *logLevelValue) Set(value string) error {
if _, err := logrus.ParseLevel(value); err != nil {
return fmt.Errorf("%s is not valid log level", value)
}
viper.Set(config.LogLevel, value)
*v = logLevelValue(value)
return nil
}
func (v *logLevelValue) Type() string { return "string" }
func (v *logLevelValue) String() string { return string(*v) }

func init() {
lfv := logFormatValue("text")
Start.Flags().Var(&lfv, "log-format", "log format [text|json]")

llv := logLevelValue("info")
Start.Flags().Var(&llv, "log-level", "log level [debug|info|warn|error|fatal|panic]")

RootCmd.AddCommand(Start)
}

Expand Down
2 changes: 2 additions & 0 deletions daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func serviceSpec() (spec container.ServiceOptions, err error) {
Image: viper.GetString(config.CoreImage),
Env: container.MapToEnv(map[string]string{
config.ToEnv(config.MESGPath): "/mesg",
config.ToEnv(config.LogFormat): viper.GetString(config.LogFormat),
config.ToEnv(config.LogLevel): viper.GetString(config.LogLevel),
config.ToEnv(config.APIServiceSocketPath): filepath.Join(viper.GetString(config.MESGPath), "server.sock"),
config.ToEnv(config.ServicePathHost): filepath.Join(viper.GetString(config.MESGPath), "services"),
}),
Expand Down

0 comments on commit 3228394

Please sign in to comment.