diff --git a/Makefile b/Makefile index cc1b1bf..706cee6 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,9 @@ ALL: lint run +.PHONY: gomod +gomod: + rm go.sum && go mod tidy + .PHONY: lint lint: golangci-lint run diff --git a/cache/redis.go b/cache/redis.go index 3a89c4c..9d771eb 100644 --- a/cache/redis.go +++ b/cache/redis.go @@ -4,9 +4,9 @@ import ( "time" "github.com/gxxgle/go-utils/json" - "github.com/gxxgle/go-utils/log" "github.com/go-redis/redis/v7" + "github.com/phuslu/log" ) // RedisConfig is config struct of redis. @@ -95,6 +95,6 @@ func (c *RedisCacher) HGet(key, field string, value interface{}) error { func (c *RedisCacher) Close() { if err := c.Client.Close(); err != nil { - log.L.WithError(err).Error("go-utils redis close") + log.Error().Err(err).Msg("go-utils redis close") } } diff --git a/db/db.go b/db/db.go index a2edc56..1685bfc 100644 --- a/db/db.go +++ b/db/db.go @@ -6,11 +6,11 @@ import ( "time" "github.com/gxxgle/go-utils/env" - "github.com/gxxgle/go-utils/log" "github.com/doug-martin/goqu/v9" _ "github.com/doug-martin/goqu/v9/dialect/mysql" _ "github.com/go-sql-driver/mysql" + "github.com/phuslu/log" "xorm.io/xorm" ) @@ -95,7 +95,7 @@ func Transaction(s *xorm.Session, fn func(*xorm.Session) (error, error)) (dbErr, dbErr, retErr = fn(s) if retErr != nil { if err := s.Rollback(); err != nil { - log.L.WithError(err).Error("go-utils db transaction rollback") + log.Error().Err(err).Msg("go-utils db transaction rollback") } return diff --git a/errors/errors.go b/errors/errors.go deleted file mode 100644 index 5883c74..0000000 --- a/errors/errors.go +++ /dev/null @@ -1,67 +0,0 @@ -package errors - -import ( - "fmt" - - "github.com/gxxgle/go-utils/json" - "github.com/gxxgle/go-utils/log" -) - -var ( - UnrecognizedErrorCode = int32(1) - InternalErrorCode = int32(2) - - allErrors = map[int32]error{ - UnrecognizedErrorCode: &Error{Code: UnrecognizedErrorCode, Detail: "unrecognized error"}, - InternalErrorCode: &Error{Code: InternalErrorCode, Detail: "internal error"}, - } -) - -type Error struct { - Code int32 `json:"code"` - Detail string `json:"detail"` - Internal string `json:"internal,omitempty"` -} - -func (e *Error) Error() string { - return json.MustMarshalToString(e) -} - -func Parse(err error) *Error { - e := new(Error) - if errr := json.UnmarshalFromString(err.Error(), e); errr != nil { - e.Code = UnrecognizedErrorCode - e.Detail = err.Error() - } - - return e -} - -func GetErrorCode(err error) int32 { - return Parse(err).Code -} - -func addError(err *Error) *Error { - e, ok := allErrors[err.Code] - if ok { - log.L.WithFields(log.F{"exists_err": e, "new_err": err}).Fatal("duplate error code") - } - - allErrors[err.Code] = err - return err -} - -func New(code int32, detail string) error { - return addError(&Error{ - Code: code, - Detail: detail, - }) -} - -func Internal(format string, a ...interface{}) error { - return &Error{ - Code: InternalErrorCode, - Detail: "internal error", - Internal: fmt.Sprintf(format, a...), - } -} diff --git a/example/main.go b/example/main.go index 1904ead..3ef3d5b 100644 --- a/example/main.go +++ b/example/main.go @@ -5,7 +5,6 @@ import ( _ "github.com/gxxgle/go-utils/config" _ "github.com/gxxgle/go-utils/db" _ "github.com/gxxgle/go-utils/env" - _ "github.com/gxxgle/go-utils/errors" _ "github.com/gxxgle/go-utils/ip" _ "github.com/gxxgle/go-utils/iriser" _ "github.com/gxxgle/go-utils/json" diff --git a/go.mod b/go.mod index e174c0c..1b7da11 100644 --- a/go.mod +++ b/go.mod @@ -29,11 +29,11 @@ require ( 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/phuslu/log v1.0.39 github.com/robfig/cron/v3 v3.0.1 github.com/sergi/go-diff v1.1.0 // indirect github.com/shopspring/decimal v1.2.0 github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect - github.com/sirupsen/logrus v1.6.0 github.com/smartystreets/goconvey v1.6.4 // indirect github.com/streadway/amqp v1.0.0 github.com/stretchr/objx v0.3.0 // indirect diff --git a/go.sum b/go.sum index 341fa17..b94b9ad 100644 --- a/go.sum +++ b/go.sum @@ -129,8 +129,6 @@ github.com/klauspost/compress v1.9.7/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0 github.com/klauspost/compress v1.10.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= github.com/klauspost/compress v1.10.10 h1:a/y8CglcM7gLGYmlbP/stPE5sR3hbhFRUjCBfd/0B3I= github.com/klauspost/compress v1.10.10/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= -github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8= -github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= @@ -180,6 +178,8 @@ github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1Cpa github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/phuslu/log v1.0.39 h1:yjqyIR1ahRGlHtlLwvZMlwwAzcrcEaVOCng6eN8wHBc= +github.com/phuslu/log v1.0.39/go.mod h1:BSFbUpGk1VoydawCb63LBAGwv/q6qbaItgrBpehbPsk= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -196,8 +196,6 @@ github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXY github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= -github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I= -github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= @@ -278,7 +276,6 @@ golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/iriser/iriser.go b/iriser/iriser.go index 0c82ab9..d6090ce 100644 --- a/iriser/iriser.go +++ b/iriser/iriser.go @@ -5,11 +5,10 @@ import ( "strings" "time" - "github.com/gxxgle/go-utils/log" - "github.com/kataras/iris/v12" "github.com/kataras/iris/v12/core/router" "github.com/kataras/iris/v12/middleware/logger" + "github.com/phuslu/log" ) func Register(r router.Party, h interface{}) { @@ -34,7 +33,7 @@ func Register(r router.Party, h interface{}) { mValue.Call([]reflect.Value{reflect.ValueOf(ctx)}) }) - log.L.WithField("path", prefix+"/"+mType.Name).Debug("register handler") + log.Debug().Str("path", prefix+"/"+mType.Name).Msg("go-utils iris register handler") } } @@ -47,17 +46,18 @@ func NewLogger(ignore func(method, path string) bool) iris.Handler { if ignore != nil && ignore(ctx.Method(), ctx.Path()) { return } - fields := log.F{ - "method": ctx.Method(), - "path": ctx.Path(), - "latency_ms": latency.Milliseconds(), - } + + lctx := log.NewContext(nil). + Str("method", ctx.Method()). + Str("path", ctx.Path()). + Int64("latency_ms", latency.Milliseconds()) ctx.Values().Visit(func(key string, value interface{}) { if strings.HasPrefix(key, "log.") { - fields[strings.TrimPrefix(key, "log.")] = value + lctx.Interface(strings.TrimPrefix(key, "log."), value) } }) - log.L.WithFields(fields).Info("api request") + + log.Info().Context(lctx.Value()).Msg("api request") } return logger.New(cfg) } diff --git a/log/example/bin/example b/log/example/bin/example deleted file mode 100755 index b5cb93c..0000000 Binary files a/log/example/bin/example and /dev/null differ diff --git a/log/example/main.go b/log/example/main.go deleted file mode 100644 index 1b02a4d..0000000 --- a/log/example/main.go +++ /dev/null @@ -1,37 +0,0 @@ -package main - -import ( - "time" - - "github.com/gxxgle/go-utils/log" -) - -type person struct { - Name string - Age int -} - -func (p *person) String() string { - return "xxx" -} - -func main() { - log.TextFormat() - // log.File() - log.Debug() - fields := log.F{ - "c": "c", - "z": "z", - "a": "a", - "b": "b", - "t": "t", - "persion": &person{}, - } - 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 58501b2..f84eb64 100644 --- a/log/log.go +++ b/log/log.go @@ -4,67 +4,27 @@ import ( "fmt" "os" "path/filepath" - "runtime" - "strings" "github.com/gxxgle/go-utils/path" - "github.com/sirupsen/logrus" -) - -type ( - F = logrus.Fields + "github.com/phuslu/log" ) var ( - logfile *os.File - Logger = logrus.StandardLogger() - L = logrus.WithField("@pid", os.Getpid()) + fileWriter *log.FileWriter ) func init() { - JSONFormat() -} - -func TextFormat() { - logrus.SetFormatter(&logrus.TextFormatter{ - FullTimestamp: true, - TimestampFormat: "2006-01-02T15:04:05.000", - CallerPrettyfier: callerPrettyfier, - }) + log.DefaultLogger.Level = log.InfoLevel + log.DefaultLogger.Caller = 1 } -func JSONFormat() { - logrus.SetFormatter(&logrus.JSONFormatter{ - FieldMap: logrus.FieldMap{ - logrus.FieldKeyTime: "@time", - logrus.FieldKeyLevel: "@level", - logrus.FieldKeyFunc: "@func", - logrus.FieldKeyFile: "@file", - logrus.FieldKeyMsg: "msg", - }, - 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 +func ColorConsole() { + log.DefaultLogger.Writer = &log.ConsoleWriter{ + ColorOutput: true, + QuoteString: false, + EndWithMessage: false, } - 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) { @@ -81,43 +41,52 @@ func File(logpaths ...string) { err := os.MkdirAll(filepath.Dir(logpath), os.ModePerm) if err != nil { - L.WithField("path", logpath).WithError(err).Error("go-utils log mkdir failed") + log.Error().Err(err).Str("path", logpath).Msg("go-utils log mkdir failed") return } - logfile, err = os.OpenFile(logpath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) - if err != nil { - L.WithField("path", logpath).WithError(err).Error("go-utils log open file failed") - return + fileWriter = &log.FileWriter{ + Filename: logpath, + FileMode: 0600, + LocalTime: true, } - - logrus.SetOutput(logfile) + log.DefaultLogger.Writer = fileWriter } -func Debug() { - logrus.SetReportCaller(true) - logrus.SetLevel(logrus.DebugLevel) +func SetDebug() { + log.DefaultLogger.Level = log.DebugLevel } -func LogIfError(err error, msg ...interface{}) { +func LogIfError(err error, msgs ...string) { if err == nil { return } - L.WithError(err).Error(msg...) + msg := "" + if len(msgs) > 0 { + msg = msgs[0] + } + + log.Error().Caller(log.DefaultLogger.Caller + 1).Err(err).Msg(msg) } -func FatalIfError(err error, msg ...interface{}) { +func FatalIfError(err error, msgs ...string) { if err == nil { return } - L.WithError(err).Fatal(msg...) + msg := "" + if len(msgs) > 0 { + msg = msgs[0] + } + + log.Fatal().Caller(log.DefaultLogger.Caller + 1).Err(err).Msg(msg) } -func Close() { - if logfile != nil { - logfile.Close() - logfile = nil +func Close() error { + if fileWriter != nil { + return fileWriter.Close() } + + return nil } diff --git a/mq/rabbitmq.go b/mq/rabbitmq.go index ebc57ea..9dc0ccb 100644 --- a/mq/rabbitmq.go +++ b/mq/rabbitmq.go @@ -3,9 +3,10 @@ package mq import ( "sync" - "github.com/gxxgle/go-utils/log" + ulog "github.com/gxxgle/go-utils/log" "github.com/assembla/cony" + "github.com/phuslu/log" "github.com/streadway/amqp" ) @@ -66,7 +67,7 @@ func (c *rabbitmq) run() { select { case err := <-c.Errors(): if err != nil { - log.L.WithError(err).Error("go-utils mq rabbitmq client run") + log.Error().Err(err).Msg("go-utils rabbitmq client run") } case <-c.exit: @@ -93,11 +94,11 @@ func newRabbitmqPublisher(cli *rabbitmq, exchange string) *rabbitmqPublisher { func (p *rabbitmqPublisher) send(msg *Message) { err := p.puber.PublishWithRoutingKey(amqp.Publishing{Body: msg.Body}, msg.Key) if err != nil { - log.L.WithError(err).WithFields(log.F{ - "exchange": p.exchange, - "key": msg.Key, - "body": string(msg.Body), - }).Error("go-utils mq rabbitmq publisher send message") + log.Error(). + Str("exchange", p.exchange). + Str("key", msg.Key). + Str("body", string(msg.Body)). + Msg("go-utils rabbitmq publisher send message") } } @@ -116,7 +117,7 @@ func (p *rabbitmqPublisher) run() { close(p.msgs) p.cli.wg.Done() - log.L.WithField("exchange", p.exchange).Info("go-utils mq rabbitmq publisher stopped") + log.Info().Str("exchange", p.exchange).Msg("go-utils rabbitmq publisher stopped") }() } @@ -148,17 +149,18 @@ func (s *rabbitmqSubscriber) Subscribe(handler func([]byte) error) { select { case msg := <-s.coner.Deliveries(): if err := handler(msg.Body); err != nil { - log.L.WithError(err).WithFields(log.F{ - "queue": s.queue, - "body": string(msg.Body), - }).Error("go-utils mq rabbitmq subscriber handler message") + log.Error(). + Err(err). + Str("queue", s.queue). + Str("body", string(msg.Body)). + Msg("go-utils rabbitmq subscriber handler message") continue } - log.LogIfError(msg.Ack(false)) + ulog.LogIfError(msg.Ack(false)) case err := <-s.coner.Errors(): if err != nil { - log.L.WithError(err).WithField("queue", s.queue).Error("go-utils mq rabbitmq subscriber") + log.Error().Err(err).Str("queue", s.queue).Msg("go-utils rabbitmq subscriber") } case <-s.cli.exit: @@ -166,6 +168,6 @@ func (s *rabbitmqSubscriber) Subscribe(handler func([]byte) error) { } s.cli.wg.Done() - log.L.WithField("queue", s.queue).Info("go-utils mq rabbitmq subscriber stopped") + log.Info().Str("queue", s.queue).Msg("go-utils rabbitmq subscriber stopped") }() } diff --git a/pool/task.go b/pool/task.go index 5c840ff..994641c 100644 --- a/pool/task.go +++ b/pool/task.go @@ -4,7 +4,7 @@ import ( "sync" "time" - "github.com/gxxgle/go-utils/log" + "github.com/phuslu/log" ) type TaskPool struct { @@ -44,10 +44,10 @@ func (tp *TaskPool) Stop() { start := time.Now() defer func() { - log.L.WithFields(log.F{ - "taskpool_name": tp.name, - "cost": time.Since(start), - }).Info("go-utils stop taskpool") + log.Info(). + Str("taskpool_name", tp.name). + Dur("cost", time.Since(start)). + Msg("go-utils stop taskpool") }() tp.stopped = true @@ -67,7 +67,7 @@ func (tp *TaskPool) loopRun() { func (tp *TaskPool) run() { defer func() { if err := recover(); err != nil { - log.L.WithField("error", err).Error("go-utils task crash") + log.Error().Interface("error", err).Msg("go-utils task crash") } }() diff --git a/sync/example/main.go b/sync/example/main.go index 26f759f..03c0dda 100644 --- a/sync/example/main.go +++ b/sync/example/main.go @@ -5,8 +5,10 @@ import ( "time" "github.com/gxxgle/go-utils/cache" - "github.com/gxxgle/go-utils/log" + ulog "github.com/gxxgle/go-utils/log" "github.com/gxxgle/go-utils/sync" + + "github.com/phuslu/log" ) var ( @@ -14,7 +16,7 @@ var ( ) func init() { - log.LogIfError(sync.InitRedis(&cache.RedisConfig{ + ulog.LogIfError(sync.InitRedis(&cache.RedisConfig{ URL: "devhost:6379", Password: "KgqvdOdYV5", Retries: 10, @@ -33,11 +35,11 @@ func main() { func print(i int) { begin := time.Now() mu := sync.NewMutex("LOCKER:TEST") - log.L.Info("lock getting index:", i, ", cost:", time.Since(begin).String()) + log.Info().Int("index", i).Dur("cost", time.Since(begin)).Msg("lock getting") mu.Lock() - log.L.Info("lock got index:", i, ", cost:", time.Since(begin).String()) + log.Info().Int("index", i).Dur("cost", time.Since(begin)).Msg("lock got") defer wg.Done() defer mu.Unlock() time.Sleep(time.Second * 5) - log.L.Info("lock un index:", i, ", cost:", time.Since(begin).String()) + log.Info().Int("index", i).Dur("cost", time.Since(begin)).Msg("lock un") } diff --git a/sync/redis.go b/sync/redis.go index 591747a..fe4a350 100644 --- a/sync/redis.go +++ b/sync/redis.go @@ -2,9 +2,9 @@ package sync import ( "github.com/gxxgle/go-utils/cache" - "github.com/gxxgle/go-utils/log" "github.com/bsm/redislock" + "github.com/phuslu/log" ) type redisMutexer struct { @@ -68,12 +68,12 @@ func (m *redisMutex) Lock() { return } - log.L.WithError(err).WithField("key", m.key).Error("go-utils redislock lock error") + log.Error().Err(err).Str("key", m.key).Msg("go-utils redislock lock error") } } func (m *redisMutex) Unlock() { if err := m.lock.Release(); err != nil { - log.L.WithError(err).WithField("key", m.key).Error("go-utils redislock unlock error") + log.Error().Err(err).Str("key", m.key).Msg("go-utils redislock unlock error") } }