master
Evan Chen 2021-12-20 10:23:08 +08:00
parent 86fb20cb2c
commit 07f530301d
15 changed files with 93 additions and 76 deletions

View File

@ -4,8 +4,8 @@ import (
"time" "time"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"kumoly.io/kumoly/app/store" "kumoly.io/kumoly/app/store"
"kumoly.io/kumoly/app/util"
) )
type Attribute struct { type Attribute struct {
@ -23,7 +23,7 @@ type Attribute struct {
var l zerolog.Logger var l zerolog.Logger
func Init() { func Init() {
l = log.With().Str("mod", "attribute").Logger() l = util.Klog.With().Str("mod", "attribute").Logger()
l.Debug().Str("service", "attribute.Service"). l.Debug().Str("service", "attribute.Service").
Msg("Migrating database for attribute.Service ...") Msg("Migrating database for attribute.Service ...")

View File

@ -6,7 +6,6 @@ import (
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"kumoly.io/kumoly/app/errors" "kumoly.io/kumoly/app/errors"
"kumoly.io/kumoly/app/history" "kumoly.io/kumoly/app/history"
"kumoly.io/kumoly/app/server" "kumoly.io/kumoly/app/server"
@ -27,7 +26,7 @@ func ApiLogin(c *gin.Context) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
// log.Trace().Str("mod", "auth"). // l.Trace().Str("mod", "auth").
// Str("username", data.Name).Str("password", string(pwd)). // Str("username", data.Name).Str("password", string(pwd)).
// Msg("user login") // Msg("user login")
usr := &User{} usr := &User{}
@ -53,7 +52,7 @@ func ApiLogin(c *gin.Context) {
} }
err = usr.ValidatePassword(string(pwd)) err = usr.ValidatePassword(string(pwd))
if err != nil { if err != nil {
log.Error().Str("mod", "auth").Err(err).Msg("wrong password") l.Error().Str("mod", "auth").Err(err).Msg("wrong password")
usr.LoginFailed += 1 usr.LoginFailed += 1
DB.Model(&usr).Update("login_failed", usr.LoginFailed) DB.Model(&usr).Update("login_failed", usr.LoginFailed)
panic(ErrorLoginFailed) panic(ErrorLoginFailed)
@ -62,7 +61,7 @@ func ApiLogin(c *gin.Context) {
panic(ErrorUserIsSSO) panic(ErrorUserIsSSO)
} }
if !usr.Activated { if !usr.Activated {
log.Error().Str("mod", "auth"). l.Error().Str("mod", "auth").
Err(ErrorUserNotActivated). Err(ErrorUserNotActivated).
Str("user", usr.Username).Str("uid", usr.ID). Str("user", usr.Username).Str("uid", usr.ID).
Msg("not activated") Msg("not activated")

View File

@ -23,7 +23,7 @@ func (c *Claims) HasGroup(grps ...string) bool {
} }
func (srv Service) SetDefaultGroups() error { func (srv Service) SetDefaultGroups() error {
srv.Logger.Debug().Msg("Setup default groups") l.Debug().Msg("Setup default groups")
for _, g := range []string{SYSTEM, ADMIN, USER} { for _, g := range []string{SYSTEM, ADMIN, USER} {
grp := &Group{} grp := &Group{}
if err := DB.Where("name = ?", g).First(grp).Error; err != nil { if err := DB.Where("name = ?", g).First(grp).Error; err != nil {
@ -31,7 +31,7 @@ func (srv Service) SetDefaultGroups() error {
Name: g, Name: g,
}).Error }).Error
if err != nil { if err != nil {
srv.Logger.Error().Err(err).Msg("create group error") l.Error().Err(err).Msg("create group error")
return err return err
} }
} }
@ -43,7 +43,7 @@ func (srv Service) SetDefaultAdmin(username, password string) error {
admin := &Group{} admin := &Group{}
err := DB.Where("name = ?", ADMIN).First(admin).Error err := DB.Where("name = ?", ADMIN).First(admin).Error
if err != nil { if err != nil {
srv.Logger.Error().Err(err).Msg("SetDefaultAdmin") l.Error().Err(err).Msg("SetDefaultAdmin")
return err return err
} }
usrgrp := struct { usrgrp := struct {
@ -54,12 +54,12 @@ func (srv Service) SetDefaultAdmin(username, password string) error {
Raw("select * from user_groups where group_id = ?", admin.ID). Raw("select * from user_groups where group_id = ?", admin.ID).
Scan(&usrgrp) Scan(&usrgrp)
if result.Error != nil { if result.Error != nil {
srv.Logger.Error().Err(result.Error).Msg("SetDefaultAdmin") l.Error().Err(result.Error).Msg("SetDefaultAdmin")
return result.Error return result.Error
} }
usr := &User{} usr := &User{}
if result.RowsAffected == 0 { if result.RowsAffected == 0 {
srv.Logger.Debug().Msg("Setting up admin account") l.Debug().Msg("Setting up admin account")
pwd, _ := bcrypt.GenerateFromPassword([]byte(password), 14) pwd, _ := bcrypt.GenerateFromPassword([]byte(password), 14)
usr.Username = username usr.Username = username
usr.Password = string(pwd) usr.Password = string(pwd)

View File

@ -8,10 +8,11 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt" "github.com/golang-jwt/jwt"
"github.com/rs/xid" "github.com/rs/xid"
"github.com/rs/zerolog/log" "github.com/rs/zerolog"
"github.com/spf13/viper" "github.com/spf13/viper"
"kumoly.io/kumoly/app/errors" "kumoly.io/kumoly/app/errors"
"kumoly.io/kumoly/app/system" "kumoly.io/kumoly/app/system"
"kumoly.io/kumoly/app/util"
) )
const GinClaimKey = "claim" const GinClaimKey = "claim"
@ -37,7 +38,10 @@ type Auth struct {
Secret string Secret string
} }
var l zerolog.Logger
func NewAuth() *Auth { func NewAuth() *Auth {
l = util.Klog.With().Str("mod", "auth").Logger()
return &Auth{ return &Auth{
CookieMode: true, CookieMode: true,
CookieSecure: strings.HasPrefix(viper.GetString("server.url"), "https"), CookieSecure: strings.HasPrefix(viper.GetString("server.url"), "https"),
@ -148,7 +152,7 @@ func (srv Auth) NewToken(claims Claims) (tok string, err error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
tok, err = token.SignedString([]byte(srv.Secret)) tok, err = token.SignedString([]byte(srv.Secret))
if err != nil { if err != nil {
log.Error().Str("mod", "auth").Err(err).Msg("NewToken") l.Error().Err(err).Msg("NewToken")
} }
return return
} }
@ -162,13 +166,13 @@ func (srv Auth) Middleware(c *gin.Context) {
claims.ExpiresAt = time.Now().Unix() + srv.TokenExpire claims.ExpiresAt = time.Now().Unix() + srv.TokenExpire
tok, err := srv.NewToken(*claims) tok, err := srv.NewToken(*claims)
if err != nil { if err != nil {
log.Debug().Str("mod", "auth").Err(err).Msg("Middleware") l.Debug().Err(err).Msg("Middleware")
} else { } else {
srv.SetToken(c, tok) srv.SetToken(c, tok)
} }
} }
} else { } else {
log.Trace().Err(err).Msg("") l.Trace().Err(err).Msg("")
} }
} }

View File

@ -1,13 +1,12 @@
package auth package auth
import ( import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/viper" "github.com/spf13/viper"
"gorm.io/gorm" "gorm.io/gorm"
"kumoly.io/kumoly/app/server" "kumoly.io/kumoly/app/server"
"kumoly.io/kumoly/app/store" "kumoly.io/kumoly/app/store"
"kumoly.io/kumoly/app/system" "kumoly.io/kumoly/app/system"
"kumoly.io/kumoly/app/util"
) )
var DB *gorm.DB var DB *gorm.DB
@ -18,13 +17,10 @@ func SetDB(db *gorm.DB) {
type Service struct { type Service struct {
system.BaseService system.BaseService
Logger zerolog.Logger
server *server.Service
} }
func New(s *server.Service) *Service { func New() *Service {
return &Service{server: s} return &Service{}
} }
func (srv Service) GetName() string { return "auth.Service" } func (srv Service) GetName() string { return "auth.Service" }
@ -32,15 +28,14 @@ func (srv Service) GetDependencies() []string { return []string{"server.Service"
func (srv Service) IsService() bool { return true } func (srv Service) IsService() bool { return true }
func (srv Service) Init() error { func (srv Service) Init() error {
srv.Logger = log.With().Str("mod", "auth").Str("service", "auth.Service").Logger() l = util.Klog.With().Str("mod", "auth").Logger()
l.Debug().Msg("Migrating database for auth.Service ...")
srv.Logger.Debug().Msg("Migrating database for auth.Service ...")
if err := store.Migrate(&User{}, &Profile{}, &Group{}); err != nil { if err := store.Migrate(&User{}, &Profile{}, &Group{}); err != nil {
srv.Logger.Error().Err(err).Msg("Migrating database") l.Error().Err(err).Msg("Migrating database")
return err return err
} }
srv.Logger.Debug().Msg("Checking db state") l.Debug().Msg("Checking db state")
// add default group // add default group
if err := srv.SetDefaultGroups(); err != nil { if err := srv.SetDefaultGroups(); err != nil {
@ -59,10 +54,10 @@ func (srv Service) Init() error {
} }
func (srv Service) Load() error { func (srv Service) Load() error {
srv.server.API.POST("/login", ApiLogin) server.API.POST("/login", ApiLogin)
srv.server.API.POST("/logout", ApiLogout) server.API.POST("/logout", ApiLogout)
srv.server.API.GET("/whoami", ApiMe) server.API.GET("/whoami", ApiMe)
usrAPI := srv.server.API.Group("usr") usrAPI := server.API.Group("usr")
usrAPI.POST("/", ACAdmin(), ApiUserNew) usrAPI.POST("/", ACAdmin(), ApiUserNew)
usrAPI.GET("/", ACAdmin(), ApiUsers) usrAPI.GET("/", ACAdmin(), ApiUsers)
usrAPI.DELETE("/:id", ACAdmin(), ApiUserDelete) usrAPI.DELETE("/:id", ACAdmin(), ApiUserDelete)
@ -70,7 +65,7 @@ func (srv Service) Load() error {
usrAPI.PUT("/activate/:id", ACAdmin(), ApiUserActivate) usrAPI.PUT("/activate/:id", ACAdmin(), ApiUserActivate)
usrAPI.PUT("/deactivate/:id", ACAdmin(), ApiUserDeactivate) usrAPI.PUT("/deactivate/:id", ACAdmin(), ApiUserDeactivate)
grpAPI := srv.server.API.Group("grp") grpAPI := server.API.Group("grp")
grpAPI.GET("/", ACAdmin(), ApiGrps) grpAPI.GET("/", ACAdmin(), ApiGrps)
grpAPI.POST("/", ACAdmin(), ApiGrpNew) grpAPI.POST("/", ACAdmin(), ApiGrpNew)
grpAPI.PUT("/", ApiGrpUpdate) grpAPI.PUT("/", ApiGrpUpdate)

View File

@ -4,7 +4,6 @@ import (
"time" "time"
"github.com/rs/xid" "github.com/rs/xid"
"github.com/rs/zerolog/log"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -63,7 +62,7 @@ func (usr *User) ChangePassword(db *gorm.DB, to string) (err error) {
return tx.Model(usr).Update("password", string(bytes)).Error return tx.Model(usr).Update("password", string(bytes)).Error
}) })
if err != nil { if err != nil {
log.Error().Str("mod", "auth").Err(err).Msg("ChangePassword") l.Error().Err(err).Msg("ChangePassword")
} }
return err return err
} }

View File

@ -14,6 +14,7 @@ import (
) )
func Default() *system.System { func Default() *system.System {
system.Setup()
store.Setup() store.Setup()
sys := system.New() sys := system.New()
server := server.New(viper.GetString("name")) server := server.New(viper.GetString("name"))
@ -24,7 +25,7 @@ func Default() *system.System {
auth.SetDB(store.DB) auth.SetDB(store.DB)
sys.Inject(auth.Injector(server.API)) sys.Inject(auth.Injector(server.API))
sys.Append(server, auth.New(server), sys.Append(server, auth.New(),
&task.Service{}, &task.Service{},
&history.Service{}, &history.Service{},
&email.Service{}, &email.Service{},

View File

@ -6,7 +6,6 @@ import (
"sync" "sync"
"time" "time"
"github.com/rs/zerolog/log"
"gorm.io/gorm" "gorm.io/gorm"
"kumoly.io/kumoly/app/errors" "kumoly.io/kumoly/app/errors"
"kumoly.io/kumoly/app/store" "kumoly.io/kumoly/app/store"
@ -40,7 +39,7 @@ type History struct {
func (h *History) BeforeCreate(tx *gorm.DB) (err error) { func (h *History) BeforeCreate(tx *gorm.DB) (err error) {
if h.Body != nil { if h.Body != nil {
if body, err := json.Marshal(h.Body); err != nil { if body, err := json.Marshal(h.Body); err != nil {
log.Error().Str("mod", "history").Err(err).Msg("history create error") l.Error().Str("mod", "history").Err(err).Msg("history create error")
} else { } else {
h.BodyJson = string(body) h.BodyJson = string(body)
} }
@ -87,14 +86,14 @@ func Stop() {
return return
} }
quit <- struct{}{} quit <- struct{}{}
log.Debug().Str("mod", "history").Msg("stop received") l.Debug().Str("mod", "history").Msg("stop received")
wg.Wait() wg.Wait()
log.Debug().Str("mod", "history").Msg("stopped") l.Debug().Str("mod", "history").Msg("stopped")
} }
func Send(h *History) { func Send(h *History) {
if len(started) == 0 { if len(started) == 0 {
log.Warn().Str("mod", "history"). l.Warn().Str("mod", "history").
Interface("history", h). Interface("history", h).
Msg("history reporter has not started, report will be discarded") Msg("history reporter has not started, report will be discarded")
} }
@ -111,7 +110,7 @@ var Interceptor func(*History) = nil
var DBReceiver Receiver = func(h *History) { var DBReceiver Receiver = func(h *History) {
err := store.DB.Create(h).Error err := store.DB.Create(h).Error
if err != nil { if err != nil {
log.Error().Str("mod", "history"). l.Error().Str("mod", "history").
Interface("history", h). Interface("history", h).
Err(err).Msg("DBReceiver error") Err(err).Msg("DBReceiver error")
} }

View File

@ -1,21 +1,22 @@
package history package history
import ( import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog"
"kumoly.io/kumoly/app/store" "kumoly.io/kumoly/app/store"
"kumoly.io/kumoly/app/util"
) )
type Service struct { type Service struct {
} }
var l zerolog.Logger
func (srv Service) GetName() string { return "history.Service" } func (srv Service) GetName() string { return "history.Service" }
func (srv Service) IsService() bool { return true } 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 {
l := log.With().Str("mod", "history"). l = util.Klog.With().Str("mod", "history").Logger()
Str("service", "history.Service").
Logger()
l.Debug().Msg("Migrating database for history.Service ...") l.Debug().Msg("Migrating database for history.Service ...")
if err := store.Migrate(&History{}); err != nil { if err := store.Migrate(&History{}); err != nil {
l.Error().Err(err).Msg("Migrating database") l.Error().Err(err).Msg("Migrating database")

View File

@ -34,7 +34,7 @@ func (srv *Service) Default(c *gin.Context) {
} }
if !util.PROD { if !util.PROD {
cl.Caller(2) cl.Caller(2)
cl.Str("trace", util.Stack()) // 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())

View File

@ -6,9 +6,9 @@ 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/util"
) )
type SkipLogFunc func(c *gin.Context) bool type SkipLogFunc func(c *gin.Context) bool
@ -44,14 +44,14 @@ func New(name string) *Service {
if ipnetstr := viper.GetString("server.allow"); ipnetstr != "" { if ipnetstr := viper.GetString("server.allow"); ipnetstr != "" {
_, ipnet, err := net.ParseCIDR(ipnetstr) _, ipnet, err := net.ParseCIDR(ipnetstr)
if err != nil { if err != nil {
log.Panic().Str("service", "server.Service").Str("name", name).Err(err).Msg("ParseCIDR error") util.Klog.Panic().Str("service", "server.Service").Str("name", name).Err(err).Msg("ParseCIDR error")
} }
srv.Allow = ipnet srv.Allow = ipnet
} }
return srv return srv
} }
func (srv *Service) Init() error { func (srv *Service) Init() error {
srv.l = log.With().Str("service", "server.Service").Str("name", srv.Name).Logger() srv.l = util.Klog.With().Str("service", "server.Service").Str("name", srv.Name).Logger()
return nil return nil
} }
func (srv *Service) Load() error { func (srv *Service) Load() error {

View File

@ -5,7 +5,7 @@ import (
"path/filepath" "path/filepath"
"time" "time"
"github.com/rs/zerolog/log" "github.com/rs/zerolog"
"github.com/spf13/viper" "github.com/spf13/viper"
"gorm.io/driver/mysql" "gorm.io/driver/mysql"
"gorm.io/driver/postgres" "gorm.io/driver/postgres"
@ -57,13 +57,16 @@ func init() {
viper.SetDefault("db.automigrate", true) viper.SetDefault("db.automigrate", true)
} }
var l zerolog.Logger
// Init initialize default db using New() followed by Connect() // Init initialize default db using New() followed by Connect()
func Setup() { func Setup() {
l = util.Klog.With().Str("mod", "store").Logger()
var err error var err error
std = New(DBTYPE(viper.GetString("db.type"))) std = New(DBTYPE(viper.GetString("db.type")))
DB, err = std.Connect() DB, err = std.Connect()
if err != nil { if err != nil {
log.Error().Str("mod", "store").Err(err).Msg("std connection error") l.Error().Err(err).Msg("std connection error")
panic(err) panic(err)
} }
} }
@ -88,7 +91,7 @@ func New(t DBTYPE) *Store {
s.Path = viper.GetString("data") s.Path = viper.GetString("data")
} else { } else {
err := fmt.Errorf("unknown db type %s", t) err := fmt.Errorf("unknown db type %s", t)
log.Error().Str("mod", "store").Err(err).Msg("unknown db type") l.Error().Err(err).Msg("unknown db type")
panic(err) panic(err)
} }
return s return s
@ -116,7 +119,7 @@ func (s *Store) Connect() (db *gorm.DB, err error) {
} }
func (s *Store) postgresConnector() error { func (s *Store) postgresConnector() error {
log.Info().Str("mod", "store").Msg("Connecting to postgres...") l.Info().Msg("Connecting to postgres...")
dsn := fmt.Sprintf( dsn := fmt.Sprintf(
"host=%v user=%v password=%v dbname=%v port=%v sslmode=disable TimeZone=Asia/Taipei", "host=%v user=%v password=%v dbname=%v port=%v sslmode=disable TimeZone=Asia/Taipei",
s.Host, s.User, s.Password, s.Name, s.Port, s.Host, s.User, s.Password, s.Name, s.Port,
@ -134,22 +137,22 @@ func (s *Store) postgresConnector() error {
if inter <= 0 { if inter <= 0 {
inter = 5 inter = 5
} }
log.Warn().Str("mod", "store").Err(err).Msg("Unable to connect to database") l.Warn().Err(err).Msg("Unable to connect to database")
log.Warn().Str("mod", "store").Msgf("Retrying in %v second.", inter) l.Warn().Msgf("Retrying in %v second.", inter)
time.Sleep(time.Second * inter) time.Sleep(time.Second * inter)
} }
log.Info().Str("mod", "store").Msg("Connection to postgres, ok.") l.Info().Msg("Connection to postgres, ok.")
return nil return nil
} }
//mysqlConnector connection //mysqlConnector connection
func (s *Store) mysqlConnector() error { func (s *Store) mysqlConnector() error {
log.Info().Str("mod", "store").Msg("Connecting to mysql...") l.Info().Msg("Connecting to mysql...")
dsn := fmt.Sprintf( dsn := fmt.Sprintf(
"%v:%v@tcp(%v:%v)/%v?charset=utf8mb4&parseTime=True&loc=Local", "%v:%v@tcp(%v:%v)/%v?charset=utf8mb4&parseTime=True&loc=Local",
s.User, s.Password, s.Host, s.Port, s.Name, s.User, s.Password, s.Host, s.Port, s.Name,
) )
// log.Debug(dsn) // l.Debug(dsn)
for { for {
db, err := gorm.Open(mysql.New(mysql.Config{ db, err := gorm.Open(mysql.New(mysql.Config{
DSN: dsn, // data source name DSN: dsn, // data source name
@ -163,11 +166,11 @@ func (s *Store) mysqlConnector() error {
if inter <= 0 { if inter <= 0 {
inter = 5 inter = 5
} }
log.Warn().Str("mod", "store").Err(err).Msg("Unable to connect to database") l.Warn().Err(err).Msg("Unable to connect to database")
log.Warn().Str("mod", "store").Msgf("Retrying in %v second.", inter) l.Warn().Msgf("Retrying in %v second.", inter)
time.Sleep(time.Second * inter) time.Sleep(time.Second * inter)
} }
log.Info().Str("mod", "store").Msg("Connection to mysql, ok.") l.Info().Msg("Connection to mysql, ok.")
return nil return nil
} }
@ -175,10 +178,10 @@ func (s *Store) mysqlConnector() error {
func (s *Store) sqliteConnector() error { func (s *Store) sqliteConnector() error {
util.Mkdir(s.Path) util.Mkdir(s.Path)
dbPath := filepath.Join(s.Path, s.Name+".db") dbPath := filepath.Join(s.Path, s.Name+".db")
log.Info().Str("mod", "store").Str("path", dbPath).Msg("Connecting to sqlite...") l.Info().Str("path", dbPath).Msg("Connecting to sqlite...")
db, err := gorm.Open(sqlite.Open(dbPath), s.config) db, err := gorm.Open(sqlite.Open(dbPath), s.config)
if err != nil { if err != nil {
log.Error().Str("mod", "store").Err(err).Msg("failed to connect database") l.Error().Err(err).Msg("failed to connect database")
return err return err
} }
s.DB = db s.DB = db
@ -187,7 +190,7 @@ func (s *Store) sqliteConnector() error {
func (s *Store) Migrate(dst ...interface{}) error { func (s *Store) Migrate(dst ...interface{}) error {
if !s.AutoMigrate { if !s.AutoMigrate {
log.Debug().Str("mod", "store").Msg("AutoMigration is set to off, migration skipped") l.Debug().Msg("AutoMigration is set to off, migration skipped")
return nil return nil
} }
return s.DB.AutoMigrate(dst...) return s.DB.AutoMigrate(dst...)

View File

@ -10,7 +10,6 @@ import (
"github.com/fatih/color" "github.com/fatih/color"
"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/util" "kumoly.io/kumoly/app/util"
) )
@ -44,19 +43,16 @@ func init() {
viper.SetEnvKeyReplacer(replacer) viper.SetEnvKeyReplacer(replacer)
viper.AutomaticEnv() viper.AutomaticEnv()
viper.SetConfigType("json") viper.SetConfigType("json")
Setup()
} }
func Setup() { var setupped bool = false
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.DurationFieldInteger = true func SetupLogger(l zerolog.Logger, skip ...int) zerolog.Logger {
zerolog.SetGlobalLevel(zerolog.Level(viper.GetInt("log.level")))
util.PROD = viper.GetBool("prod")
if !util.PROD { if !util.PROD {
log.Logger = log.With().Caller().Logger() l = l.With().Caller().Logger()
} }
if viper.GetBool("log.pretty") { if viper.GetBool("log.pretty") {
log.Logger = log.Output(zerolog.ConsoleWriter{ l = l.Output(zerolog.ConsoleWriter{
Out: os.Stdout, Out: os.Stdout,
TimeFormat: "2006/01/02 15:04:05", TimeFormat: "2006/01/02 15:04:05",
FormatCaller: func(i interface{}) string { FormatCaller: func(i interface{}) string {
@ -90,7 +86,23 @@ func Setup() {
}, },
}) })
} }
l = log.With().Str("mod", "system").Logger() return l
}
func Setup() {
if setupped {
return
} else {
setupped = true
}
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.DurationFieldInteger = true
zerolog.SetGlobalLevel(zerolog.Level(viper.GetInt("log.level")))
util.PROD = viper.GetBool("prod")
util.Klog = SetupLogger(util.Klog)
l = util.Klog.With().Str("mod", "system").Logger()
tz, err := time.LoadLocation(viper.GetString("timezone")) tz, err := time.LoadLocation(viper.GetString("timezone"))
if err == nil { if err == nil {

View File

@ -6,7 +6,6 @@ import (
cron "github.com/robfig/cron/v3" cron "github.com/robfig/cron/v3"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"kumoly.io/kumoly/app/util" "kumoly.io/kumoly/app/util"
) )
@ -19,7 +18,7 @@ func init() {
} }
func Init() { func Init() {
l = log.With().Str("mod", "task").Logger() l = util.Klog.With().Str("mod", "task").Logger()
c = cron.New( c = cron.New(
cron.WithLogger(&clog), cron.WithLogger(&clog),
cron.WithChain(func(j cron.Job) cron.Job { cron.WithChain(func(j cron.Job) cron.Job {

View File

@ -5,8 +5,13 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"runtime" "runtime"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
) )
var Klog zerolog.Logger = log.Logger
func Stack() string { func Stack() string {
buf := make([]byte, 1024) buf := make([]byte, 1024)
for { for {