From ad5c76bed542e58274ceeff6f6ab9c309ab42807 Mon Sep 17 00:00:00 2001 From: miaolz123 Date: Thu, 6 Aug 2020 16:32:55 +0800 Subject: [PATCH] feat: update log --- db/db.go | 21 ++++++++++++++-- go.mod | 2 ++ go.sum | 5 +++- log/example/main.go | 16 +++++++++--- log/log.go | 61 +++++++++++++++++++++++++++++++-------------- 5 files changed, 80 insertions(+), 25 deletions(-) diff --git a/db/db.go b/db/db.go index 1fd6bdd..a2edc56 100644 --- a/db/db.go +++ b/db/db.go @@ -24,7 +24,7 @@ const ( var ( DefaultRetries = 5 DefaullRetrySleep = time.Second - DefaultNeedRetry = isDeadlock + DefaultNeedRetry = IsDeadlockErr ) var ( @@ -72,6 +72,15 @@ func OpenDB(cfg *Config) (*xorm.Engine, error) { return db, err } +func ExecSQL(session *xorm.Session, sql string) (int64, error) { + rst, err := session.Exec(sql) + if err != nil { + return 0, err + } + + return rst.RowsAffected() +} + // Transaction for db // fn return [db error] and [export error] // if [export error] is not nil transaction will rollback @@ -121,10 +130,18 @@ func Transaction(s *xorm.Session, fn func(*xorm.Session) (error, error)) (dbErr, // return dbErr, retErr // } -func isDeadlock(err error) bool { +func IsDeadlockErr(err error) bool { if err == nil { return false } return strings.Contains(err.Error(), "Error 1213: Deadlock found") } + +func IsDuplicateEntryErr(err error) bool { + if err == nil { + return false + } + + return strings.Contains(err.Error(), "Error 1062: Duplicate entry") +} diff --git a/go.mod b/go.mod index cbc12c3..e174c0c 100644 --- a/go.mod +++ b/go.mod @@ -27,6 +27,7 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.1 // indirect github.com/moul/http2curl v1.0.0 // indirect + github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect github.com/patrickmn/go-cache v2.1.0+incompatible github.com/robfig/cron/v3 v3.0.1 github.com/sergi/go-diff v1.1.0 // indirect @@ -45,6 +46,7 @@ require ( github.com/yudai/pp v2.0.1+incompatible // indirect golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899 // indirect golang.org/x/net v0.0.0-20200707034311-ab3426394381 + gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect gopkg.in/yaml.v2 v2.3.0 // indirect xorm.io/xorm v1.0.3 ) diff --git a/go.sum b/go.sum index 22177ed..341fa17 100644 --- a/go.sum +++ b/go.sum @@ -168,6 +168,8 @@ github.com/nats-io/jwt v0.3.0/go.mod h1:fRYCDE99xlTsqUzISS1Bi75UBJ6ljOJQOAAu5Vgl github.com/nats-io/nats.go v1.9.1/go.mod h1:ZjDU1L/7fJ09jvUSRVBR2e7+RnLiiIQyqyzEE/Zbp4w= github.com/nats-io/nkeys v0.1.0/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w= github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.10.1 h1:q/mM8GF/n0shIN8SaAZ0V+jnLPzen6WIVZdiwrRlMlo= @@ -305,8 +307,9 @@ google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzi google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM= google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/ini.v1 v1.51.1 h1:GyboHr4UqMiLUybYjd22ZjQIKEJEpgtLXtuGbR21Oho= diff --git a/log/example/main.go b/log/example/main.go index 7e28f46..1b02a4d 100644 --- a/log/example/main.go +++ b/log/example/main.go @@ -1,6 +1,8 @@ package main import ( + "time" + "github.com/gxxgle/go-utils/log" ) @@ -14,14 +16,22 @@ func (p *person) String() string { } func main() { - log.File() + log.TextFormat() + // log.File() log.Debug() - log.L.WithFields(log.F{ + fields := log.F{ "c": "c", "z": "z", "a": "a", "b": "b", "t": "t", "persion": &person{}, - }).Debug("test") + } + log.L.WithFields(fields).Debug("test debug") + time.Sleep(time.Millisecond * 347) + log.L.WithFields(fields).Info("test info") + time.Sleep(time.Millisecond * 347) + log.L.WithFields(fields).Warn("test warn") + time.Sleep(time.Millisecond * 347) + log.L.WithFields(fields).Error("test error") } diff --git a/log/log.go b/log/log.go index df245cb..58501b2 100644 --- a/log/log.go +++ b/log/log.go @@ -18,10 +18,23 @@ type ( var ( logfile *os.File + Logger = logrus.StandardLogger() L = logrus.WithField("@pid", os.Getpid()) ) func init() { + JSONFormat() +} + +func TextFormat() { + logrus.SetFormatter(&logrus.TextFormatter{ + FullTimestamp: true, + TimestampFormat: "2006-01-02T15:04:05.000", + CallerPrettyfier: callerPrettyfier, + }) +} + +func JSONFormat() { logrus.SetFormatter(&logrus.JSONFormatter{ FieldMap: logrus.FieldMap{ logrus.FieldKeyTime: "@time", @@ -30,28 +43,30 @@ func init() { logrus.FieldKeyFile: "@file", logrus.FieldKeyMsg: "msg", }, - CallerPrettyfier: func(rf *runtime.Frame) (string, string) { - file := fmt.Sprintf(":%d", rf.Line) - files := strings.Split(rf.File, "/") - if len(files) > 0 { - file = files[len(files)-1] + file - } - if len(files) > 1 { - file = files[len(files)-2] + "/" + file - } - function := "" - functions := strings.Split(rf.Function, "/") - if len(functions) > 0 { - function = functions[len(functions)-1] + function - } - if len(functions) > 1 { - function = functions[len(functions)-2] + "/" + function - } - return function, file - }, + CallerPrettyfier: callerPrettyfier, }) } +func callerPrettyfier(rf *runtime.Frame) (string, string) { + file := fmt.Sprintf(":%d", rf.Line) + files := strings.Split(rf.File, "/") + if len(files) > 0 { + file = files[len(files)-1] + file + } + if len(files) > 1 { + file = files[len(files)-2] + "/" + file + } + function := "" + // functions := strings.Split(rf.Function, "/") + // if len(functions) > 0 { + // function = functions[len(functions)-1] + function + // } + // if len(functions) > 1 { + // function = functions[len(functions)-2] + "/" + function + // } + return function, file +} + func File(logpaths ...string) { logpath := "" if len(logpaths) > 0 { @@ -92,6 +107,14 @@ func LogIfError(err error, msg ...interface{}) { L.WithError(err).Error(msg...) } +func FatalIfError(err error, msg ...interface{}) { + if err == nil { + return + } + + L.WithError(err).Fatal(msg...) +} + func Close() { if logfile != nil { logfile.Close()