mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 08:45:16 +00:00
cmd: add log formatter option and refactor the logrus setup code
This commit is contained in:
parent
e8fe8082cc
commit
37da9dee0e
|
@ -51,6 +51,30 @@ var RootCmd = &cobra.Command{
|
||||||
env = "development"
|
env = "development"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logFormatter, err := cmd.PersistentFlags().GetString("log-formatter")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(logFormatter) == 0 {
|
||||||
|
switch env {
|
||||||
|
case "production", "prod", "stag", "staging":
|
||||||
|
// always use json formatter for production and staging
|
||||||
|
log.SetFormatter(&log.JSONFormatter{})
|
||||||
|
default:
|
||||||
|
log.SetFormatter(&prefixed.TextFormatter{})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
switch logFormatter {
|
||||||
|
case "prefixed":
|
||||||
|
log.SetFormatter(&prefixed.TextFormatter{})
|
||||||
|
case "text":
|
||||||
|
log.SetFormatter(&log.TextFormatter{})
|
||||||
|
case "json":
|
||||||
|
log.SetFormatter(&log.JSONFormatter{})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if token := viper.GetString("rollbar-token"); token != "" {
|
if token := viper.GetString("rollbar-token"); token != "" {
|
||||||
log.Infof("found rollbar token %q, setting up rollbar hook...", util.MaskKey(token))
|
log.Infof("found rollbar token %q, setting up rollbar hook...", util.MaskKey(token))
|
||||||
|
|
||||||
|
@ -165,6 +189,8 @@ func init() {
|
||||||
|
|
||||||
RootCmd.PersistentFlags().String("config", "bbgo.yaml", "config file")
|
RootCmd.PersistentFlags().String("config", "bbgo.yaml", "config file")
|
||||||
|
|
||||||
|
RootCmd.PersistentFlags().String("log-formatter", "", "configure log formatter")
|
||||||
|
|
||||||
RootCmd.PersistentFlags().String("rollbar-token", "", "rollbar token")
|
RootCmd.PersistentFlags().String("rollbar-token", "", "rollbar token")
|
||||||
|
|
||||||
// A flag can be 'persistent' meaning that this flag will be available to
|
// A flag can be 'persistent' meaning that this flag will be available to
|
||||||
|
@ -211,10 +237,6 @@ func init() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.SetFormatter(&prefixed.TextFormatter{})
|
|
||||||
}
|
|
||||||
|
|
||||||
func Execute() {
|
|
||||||
environment := os.Getenv("BBGO_ENV")
|
environment := os.Getenv("BBGO_ENV")
|
||||||
logDir := "log"
|
logDir := "log"
|
||||||
switch environment {
|
switch environment {
|
||||||
|
@ -231,8 +253,8 @@ func Execute() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
logger := log.StandardLogger()
|
|
||||||
logger.AddHook(
|
log.AddHook(
|
||||||
lfshook.NewHook(
|
lfshook.NewHook(
|
||||||
lfshook.WriterMap{
|
lfshook.WriterMap{
|
||||||
log.DebugLevel: writer,
|
log.DebugLevel: writer,
|
||||||
|
@ -240,12 +262,13 @@ func Execute() {
|
||||||
log.WarnLevel: writer,
|
log.WarnLevel: writer,
|
||||||
log.ErrorLevel: writer,
|
log.ErrorLevel: writer,
|
||||||
log.FatalLevel: writer,
|
log.FatalLevel: writer,
|
||||||
},
|
}, &log.JSONFormatter{}),
|
||||||
&log.JSONFormatter{},
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Execute() {
|
||||||
if err := RootCmd.Execute(); err != nil {
|
if err := RootCmd.Execute(); err != nil {
|
||||||
log.WithError(err).Fatalf("cannot execute command")
|
log.WithError(err).Fatalf("cannot execute command")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user