master
Evan Chen 2021-12-19 18:52:40 +08:00
parent c276c276c4
commit 93c09aa414
8 changed files with 18 additions and 12 deletions

View File

@ -9,7 +9,7 @@ import (
func Error() *History { func Error() *History {
h := getBase() h := getBase()
h.Type = ERROR h.Type = ERROR
if !PROD { if !util.PROD {
h.Trace = util.Stack() h.Trace = util.Stack()
} }
return h return h

View File

@ -12,8 +12,6 @@ import (
"kumoly.io/kumoly/app/store" "kumoly.io/kumoly/app/store"
) )
var PROD = false
const ( const (
ERROR = "ERROR" ERROR = "ERROR"
INFO = "INFO" INFO = "INFO"

View File

@ -2,7 +2,6 @@ package history
import ( import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/viper"
"kumoly.io/kumoly/app/store" "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) GetDependencies() []string { return []string{"server.Service"} }
func (srv Service) Init() error { func (srv Service) Init() error {
PROD = viper.GetBool("prod")
l := log.With().Str("mod", "history"). l := log.With().Str("mod", "history").
Str("service", "history.Service"). Str("service", "history.Service").
Logger() Logger()

View File

@ -8,6 +8,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"kumoly.io/kumoly/app/errors" "kumoly.io/kumoly/app/errors"
"kumoly.io/kumoly/app/util"
) )
func (srv *Service) Default(c *gin.Context) { func (srv *Service) Default(c *gin.Context) {
@ -31,6 +32,9 @@ func (srv *Service) Default(c *gin.Context) {
c.Abort() c.Abort()
cl.Str("error", fmt.Sprint(err)) cl.Str("error", fmt.Sprint(err))
} }
if !util.PROD {
cl.Str("trace", util.Stack())
}
} else if c.Writer.Status() >= 400 { } else if c.Writer.Status() >= 400 {
cl = srv.l.Error().Strs("error", c.Errors.Errors()) cl = srv.l.Error().Strs("error", c.Errors.Errors())
} else { } else {

View File

@ -27,7 +27,7 @@ func init() {
viper.SetDefault("domain", "kumoly.io") viper.SetDefault("domain", "kumoly.io")
viper.SetDefault("timezone", "Asia/Taipei") viper.SetDefault("timezone", "Asia/Taipei")
// mode [prod, dev] // mode [prod, dev]
viper.SetDefault("prod", PROD) viper.SetDefault("prod", true)
// log.level : [-1(trace):5(panic)] 7 to disable // log.level : [-1(trace):5(panic)] 7 to disable
viper.SetDefault("log.level", int(zerolog.InfoLevel)) viper.SetDefault("log.level", int(zerolog.InfoLevel))
@ -50,7 +50,8 @@ func Setup() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.DurationFieldInteger = true zerolog.DurationFieldInteger = true
zerolog.SetGlobalLevel(zerolog.Level(viper.GetInt("log.level"))) 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() log.Logger = log.With().Caller().Logger()
} }
if viper.GetBool("log.pretty") { if viper.GetBool("log.pretty") {

View File

@ -11,9 +11,9 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/viper" "github.com/spf13/viper"
"kumoly.io/kumoly/app/history" "kumoly.io/kumoly/app/history"
"kumoly.io/kumoly/app/util"
) )
var PROD = true
var l zerolog.Logger var l zerolog.Logger
func init() { func init() {
@ -77,7 +77,7 @@ func (sys *System) order() {
for i := range nodes { for i := range nodes {
sys.services = append(sys.services, *nodes[i].srv) sys.services = append(sys.services, *nodes[i].srv)
} }
if !PROD { if !util.PROD {
seq := "" seq := ""
for i := range sys.services { for i := range sys.services {
seq += sys.services[i].GetName() seq += sys.services[i].GetName()

View File

@ -61,9 +61,12 @@ func (t *_task) Run() {
} }
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
l.Error().Str("task_id", t.Task.ID). le := l.Error().Str("task_id", t.Task.ID).Str("task_name", t.name).
Str("task_name", t.name).Str("trace", util.Stack()). Str("error", fmt.Sprint(err))
Str("error", fmt.Sprint(err)).Msg("task paniced") if !util.PROD {
le.Str("trace", util.Stack())
}
le.Msg("task paniced")
} else if t.once { } else if t.once {
c.Remove(t.id) c.Remove(t.id)

View File

@ -5,6 +5,8 @@ import (
"path/filepath" "path/filepath"
) )
var PROD bool = true
func Mkdir(args ...interface{}) error { func Mkdir(args ...interface{}) error {
var path string var path string
var mode os.FileMode var mode os.FileMode