master
Evan Chen 2021-12-21 01:07:33 +08:00
parent 1a6235741b
commit 601e596980
2 changed files with 9 additions and 4 deletions

View File

@ -6,13 +6,17 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"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/system"
"kumoly.io/kumoly/app/util" "kumoly.io/kumoly/app/util"
) )
type SkipLogFunc func(c *gin.Context) bool type SkipLogFunc func(c *gin.Context) bool
var l zerolog.Logger
type Service struct { type Service struct {
Name string Name string
Server *gin.Engine Server *gin.Engine
@ -51,19 +55,20 @@ func New(name string) *Service {
return srv return srv
} }
func (srv *Service) Init() error { func (srv *Service) Init() error {
srv.l = util.Klog.With().Str("service", "server.Service").Str("name", srv.Name).Logger() l = util.Klog.With().Str("service", "server.Service").Str("name", srv.Name).Logger()
srv.l = system.SetupLogger(log.Logger, 2)
return nil return nil
} }
func (srv *Service) Load() error { func (srv *Service) Load() error {
return nil return nil
} }
func (srv *Service) Main() error { func (srv *Service) Main() error {
srv.l.Info().Msgf("Server started on %s", srv.Addr) l.Info().Msgf("Server started on %s", srv.Addr)
go func() { go func() {
err := srv.Server.Run(srv.Addr) err := srv.Server.Run(srv.Addr)
if err != nil { if err != nil {
history.Send(history.Error().Nm("ServerStartError").Msg(err.Error())) history.Send(history.Error().Nm("ServerStartError").Msg(err.Error()))
srv.l.Panic().Err(err).Msg("Server.Run error") l.Panic().Err(err).Msg("Server.Run error")
} }
}() }()
return nil return nil

View File

@ -48,7 +48,7 @@ func init() {
var setupped bool = false var setupped bool = false
func SetupLogger(l zerolog.Logger, skip ...int) zerolog.Logger { func SetupLogger(l zerolog.Logger, skip ...int) zerolog.Logger {
if !util.PROD && len(skip) > 0 { if !util.PROD && len(skip) == 0 {
l = l.With().Caller().Logger() l = l.With().Caller().Logger()
} }
if viper.GetBool("log.pretty") { if viper.GetBool("log.pretty") {