diff --git a/history/helper.go b/history/helper.go index da3e7ca..3a37ddb 100644 --- a/history/helper.go +++ b/history/helper.go @@ -9,7 +9,7 @@ import ( func Error() *History { h := getBase() h.Type = ERROR - if !PROD { + if !util.PROD { h.Trace = util.Stack() } return h diff --git a/history/history.go b/history/history.go index cb19465..d583992 100644 --- a/history/history.go +++ b/history/history.go @@ -12,8 +12,6 @@ import ( "kumoly.io/kumoly/app/store" ) -var PROD = false - const ( ERROR = "ERROR" INFO = "INFO" diff --git a/history/service.go b/history/service.go index bc6e54a..33afdf6 100644 --- a/history/service.go +++ b/history/service.go @@ -2,7 +2,6 @@ package history import ( "github.com/rs/zerolog/log" - "github.com/spf13/viper" "kumoly.io/kumoly/app/store" ) @@ -14,7 +13,6 @@ func (srv Service) IsService() bool { return true } func (srv Service) GetDependencies() []string { return []string{"server.Service"} } func (srv Service) Init() error { - PROD = viper.GetBool("prod") l := log.With().Str("mod", "history"). Str("service", "history.Service"). Logger() diff --git a/server/middleware.go b/server/middleware.go index 6b5eb68..d5f9ec1 100644 --- a/server/middleware.go +++ b/server/middleware.go @@ -8,6 +8,7 @@ import ( "github.com/gin-gonic/gin" "github.com/rs/zerolog" "kumoly.io/kumoly/app/errors" + "kumoly.io/kumoly/app/util" ) func (srv *Service) Default(c *gin.Context) { @@ -31,6 +32,9 @@ func (srv *Service) Default(c *gin.Context) { c.Abort() cl.Str("error", fmt.Sprint(err)) } + if !util.PROD { + cl.Str("trace", util.Stack()) + } } else if c.Writer.Status() >= 400 { cl = srv.l.Error().Strs("error", c.Errors.Errors()) } else { diff --git a/system/setup.go b/system/setup.go index 1e4803c..cf3f53f 100644 --- a/system/setup.go +++ b/system/setup.go @@ -27,7 +27,7 @@ func init() { viper.SetDefault("domain", "kumoly.io") viper.SetDefault("timezone", "Asia/Taipei") // mode [prod, dev] - viper.SetDefault("prod", PROD) + viper.SetDefault("prod", true) // log.level : [-1(trace):5(panic)] 7 to disable viper.SetDefault("log.level", int(zerolog.InfoLevel)) @@ -50,7 +50,8 @@ func Setup() { zerolog.TimeFieldFormat = zerolog.TimeFormatUnix zerolog.DurationFieldInteger = true zerolog.SetGlobalLevel(zerolog.Level(viper.GetInt("log.level"))) - if !viper.GetBool("prod") { + util.PROD = viper.GetBool("prod") + if util.PROD { log.Logger = log.With().Caller().Logger() } if viper.GetBool("log.pretty") { diff --git a/system/system.go b/system/system.go index e8fbc17..52cc9ab 100644 --- a/system/system.go +++ b/system/system.go @@ -11,9 +11,9 @@ import ( "github.com/rs/zerolog/log" "github.com/spf13/viper" "kumoly.io/kumoly/app/history" + "kumoly.io/kumoly/app/util" ) -var PROD = true var l zerolog.Logger func init() { @@ -77,7 +77,7 @@ func (sys *System) order() { for i := range nodes { sys.services = append(sys.services, *nodes[i].srv) } - if !PROD { + if !util.PROD { seq := "" for i := range sys.services { seq += sys.services[i].GetName() diff --git a/task/task.go b/task/task.go index 0a9e355..9f4388c 100644 --- a/task/task.go +++ b/task/task.go @@ -61,9 +61,12 @@ func (t *_task) Run() { } defer func() { if err := recover(); err != nil { - l.Error().Str("task_id", t.Task.ID). - Str("task_name", t.name).Str("trace", util.Stack()). - Str("error", fmt.Sprint(err)).Msg("task paniced") + le := l.Error().Str("task_id", t.Task.ID).Str("task_name", t.name). + Str("error", fmt.Sprint(err)) + if !util.PROD { + le.Str("trace", util.Stack()) + } + le.Msg("task paniced") } else if t.once { c.Remove(t.id) diff --git a/util/util.go b/util/util.go index 6dc992f..dc2f658 100644 --- a/util/util.go +++ b/util/util.go @@ -5,6 +5,8 @@ import ( "path/filepath" ) +var PROD bool = true + func Mkdir(args ...interface{}) error { var path string var mode os.FileMode