all: refactor log formatter functions

This commit is contained in:
c9s 2023-12-13 09:47:18 +08:00
parent f3ce4c2cc6
commit 6cbb17fb76
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 53 additions and 32 deletions

48
pkg/bbgo/envvar.go Normal file
View File

@ -0,0 +1,48 @@
package bbgo
import (
"os"
log "github.com/sirupsen/logrus"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
)
func GetCurrentEnv() string {
env := os.Getenv("BBGO_ENV")
if env == "" {
env = "development"
}
return env
}
func NewLogFormatterWithEnv(env string) log.Formatter {
switch env {
case "production", "prod", "stag", "staging":
// always use json formatter for production and staging
return &log.JSONFormatter{}
}
return &prefixed.TextFormatter{}
}
type LogFormatterType string
const (
LogFormatterTypePrefixed LogFormatterType = "prefixed"
LogFormatterTypeText LogFormatterType = "text"
LogFormatterTypeJson LogFormatterType = "json"
)
func NewLogFormatter(logFormatter LogFormatterType) log.Formatter {
switch logFormatter {
case LogFormatterTypePrefixed:
return &prefixed.TextFormatter{}
case LogFormatterTypeText:
return &log.TextFormatter{}
case LogFormatterTypeJson:
return &log.JSONFormatter{}
}
return &prefixed.TextFormatter{}
}

View File

@ -17,7 +17,6 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/util"
@ -46,10 +45,7 @@ var RootCmd = &cobra.Command{
log.SetLevel(log.DebugLevel)
}
env := os.Getenv("BBGO_ENV")
if env == "" {
env = "development"
}
env := bbgo.GetCurrentEnv()
logFormatter, err := cmd.Flags().GetString("log-formatter")
if err != nil {
@ -57,22 +53,11 @@ var RootCmd = &cobra.Command{
}
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{})
}
formatter := bbgo.NewLogFormatterWithEnv(env)
log.SetFormatter(formatter)
} else {
switch logFormatter {
case "prefixed":
log.SetFormatter(&prefixed.TextFormatter{})
case "text":
log.SetFormatter(&log.TextFormatter{})
case "json":
log.SetFormatter(&log.JSONFormatter{})
}
formatter := bbgo.NewLogFormatter(bbgo.LogFormatterType(logFormatter))
log.SetFormatter(formatter)
}
if token := viper.GetString("rollbar-token"); token != "" {

View File

@ -8,7 +8,6 @@ import (
"time"
"github.com/sirupsen/logrus"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
"go.uber.org/multierr"
"golang.org/x/time/rate"
@ -69,19 +68,8 @@ type LogFunction func(msg string, args ...interface{})
var debugf LogFunction
func isPrefixFormatterConfigured() bool {
_, isPrefixFormatter := logrus.StandardLogger().Formatter.(*prefixed.TextFormatter)
return isPrefixFormatter
}
func getDebugFunction() LogFunction {
if v, ok := util.GetEnvVarBool("DEBUG_BITGET"); ok && v {
if isPrefixFormatterConfigured() {
return func(msg string, args ...interface{}) {
log.Infof("[BITGET] "+msg, args...)
}
}
return log.Infof
}