bbgo_origin/pkg/bbgo/envvar.go

49 lines
1001 B
Go
Raw Permalink Normal View History

2023-12-13 01:47:18 +00:00
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{}
}