Skip to content

Commit

Permalink
feat: update log
Browse files Browse the repository at this point in the history
  • Loading branch information
miaolz123 committed Aug 6, 2020
1 parent 4aa9a52 commit ad5c76b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 25 deletions.
21 changes: 19 additions & 2 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
var (
DefaultRetries = 5
DefaullRetrySleep = time.Second
DefaultNeedRetry = isDeadlock
DefaultNeedRetry = IsDeadlockErr
)

var (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
16 changes: 13 additions & 3 deletions log/example/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"time"

"github.com/gxxgle/go-utils/log"
)

Expand All @@ -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")
}
61 changes: 42 additions & 19 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit ad5c76b

Please sign in to comment.