diff --git a/app/comment/service/cmd/main.go b/app/comment/service/cmd/main.go index 562e01a..5d77d40 100644 --- a/app/comment/service/cmd/main.go +++ b/app/comment/service/cmd/main.go @@ -2,7 +2,6 @@ package main import ( "flag" - "os" "github.com/toomanysource/atreus/app/comment/service/internal/conf" "github.com/toomanysource/atreus/pkg/logX" @@ -39,9 +38,7 @@ func newApp(logger log.Logger, gs *grpc.Server, hs *http.Server) *kratos.App { func main() { flag.Parse() - l := logX.NewDefaultLogger() - l.SetOutput(os.Stdout) - l.SetLevel(log.LevelDebug) + l := logX.Logger() logger := log.With(l, "service", Name, "caller", log.DefaultCaller, diff --git a/app/favorite/service/cmd/main.go b/app/favorite/service/cmd/main.go index 7566377..8b11be1 100644 --- a/app/favorite/service/cmd/main.go +++ b/app/favorite/service/cmd/main.go @@ -2,7 +2,6 @@ package main import ( "flag" - "os" "github.com/go-kratos/kratos/v2" "github.com/go-kratos/kratos/v2/config" @@ -40,9 +39,7 @@ func newApp(logger log.Logger, gs *grpc.Server, hs *http.Server) *kratos.App { func main() { flag.Parse() - l := logX.NewDefaultLogger() - l.SetOutput(os.Stdout) - l.SetLevel(log.LevelDebug) + l := logX.Logger() logger := log.With(l, "service", Name, "caller", log.DefaultCaller, diff --git a/app/message/service/cmd/main.go b/app/message/service/cmd/main.go index 8427f29..b8668f4 100644 --- a/app/message/service/cmd/main.go +++ b/app/message/service/cmd/main.go @@ -2,7 +2,6 @@ package main import ( "flag" - "os" "github.com/toomanysource/atreus/pkg/logX" @@ -41,9 +40,7 @@ func newApp(logger log.Logger, gs *grpc.Server, hs *http.Server) *kratos.App { func main() { flag.Parse() - l := logX.NewDefaultLogger() - l.SetOutput(os.Stdout) - l.SetLevel(log.LevelDebug) + l := logX.Logger() logger := log.With(l, "service", Name, "caller", log.DefaultCaller, diff --git a/app/publish/service/cmd/main.go b/app/publish/service/cmd/main.go index 9998b13..03296a8 100644 --- a/app/publish/service/cmd/main.go +++ b/app/publish/service/cmd/main.go @@ -2,7 +2,6 @@ package main import ( "flag" - "os" "github.com/toomanysource/atreus/pkg/logX" @@ -41,9 +40,7 @@ func newApp(logger log.Logger, gs *grpc.Server, hs *http.Server) *kratos.App { func main() { flag.Parse() - l := logX.NewDefaultLogger() - l.SetOutput(os.Stdout) - l.SetLevel(log.LevelDebug) + l := logX.Logger() logger := log.With(l, "service", Name, "caller", log.DefaultCaller, diff --git a/app/relation/service/cmd/main.go b/app/relation/service/cmd/main.go index 9547842..e2b79f7 100644 --- a/app/relation/service/cmd/main.go +++ b/app/relation/service/cmd/main.go @@ -2,7 +2,6 @@ package main import ( "flag" - "os" "github.com/toomanysource/atreus/app/relation/service/internal/conf" "github.com/toomanysource/atreus/pkg/logX" @@ -38,9 +37,7 @@ func newApp(logger log.Logger, gs *grpc.Server, hs *http.Server) *kratos.App { func main() { flag.Parse() - l := logX.NewDefaultLogger() - l.SetOutput(os.Stdout) - l.SetLevel(log.LevelDebug) + l := logX.Logger() logger := log.With(l, "service", Name, "caller", log.DefaultCaller, diff --git a/app/user/service/cmd/main.go b/app/user/service/cmd/main.go index 309e950..c13acca 100644 --- a/app/user/service/cmd/main.go +++ b/app/user/service/cmd/main.go @@ -2,7 +2,6 @@ package main import ( "flag" - "os" "github.com/go-kratos/kratos/v2" "github.com/go-kratos/kratos/v2/config" @@ -41,9 +40,7 @@ func newApp(logger log.Logger, gs *grpc.Server, hs *http.Server) *kratos.App { func main() { flag.Parse() - l := logX.NewDefaultLogger() - l.SetOutput(os.Stdout) - l.SetLevel(log.LevelDebug) + l := logX.Logger() logger := log.With(l, "service", Name, "caller", log.DefaultCaller, diff --git a/go.mod b/go.mod index eb84775..f364e55 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/toomanysource/atreus go 1.20 require ( - github.com/antonfisher/nested-logrus-formatter v1.3.1 github.com/disintegration/imaging v1.6.2 github.com/envoyproxy/protoc-gen-validate v0.10.1 github.com/go-kratos/kratos/v2 v2.7.0 @@ -13,13 +12,14 @@ require ( github.com/jinzhu/copier v0.4.0 github.com/minio/minio-go/v7 v7.0.62 github.com/segmentio/kafka-go v0.4.42 - github.com/sirupsen/logrus v1.9.3 - github.com/stretchr/testify v1.8.2 + github.com/stretchr/testify v1.8.3 github.com/u2takey/ffmpeg-go v0.5.0 go.uber.org/automaxprocs v1.5.3 + go.uber.org/zap v1.25.0 google.golang.org/genproto/googleapis/api v0.0.0-20230815205213-6bfd019c3878 google.golang.org/grpc v1.57.0 google.golang.org/protobuf v1.31.0 + gopkg.in/natefinch/lumberjack.v2 v2.2.1 gorm.io/driver/mysql v1.5.1 gorm.io/gorm v1.25.4 ) @@ -27,7 +27,7 @@ require ( require ( github.com/aws/aws-sdk-go v1.38.20 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect + github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect @@ -44,15 +44,16 @@ require ( github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/compress v1.16.7 // indirect github.com/klauspost/cpuid/v2 v2.2.5 // indirect - github.com/kr/text v0.2.0 // indirect github.com/minio/md5-simd v1.1.2 // indirect github.com/minio/sha256-simd v1.0.1 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pierrec/lz4/v4 v4.1.15 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/rs/xid v1.5.0 // indirect + github.com/sirupsen/logrus v1.9.3 // indirect github.com/u2takey/go-utils v0.3.1 // indirect + go.uber.org/multierr v1.10.0 // indirect golang.org/x/crypto v0.12.0 // indirect golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect golang.org/x/net v0.14.0 // indirect diff --git a/go.sum b/go.sum index 55a8a3f..54f28b9 100644 --- a/go.sum +++ b/go.sum @@ -1,15 +1,14 @@ -github.com/antonfisher/nested-logrus-formatter v1.3.1 h1:NFJIr+pzwv5QLHTPyKz9UMEoHck02Q9L0FP13b/xSbQ= -github.com/antonfisher/nested-logrus-formatter v1.3.1/go.mod h1:6WTfyWFkBc9+zyBaKIqRrg/KwMqBbodBjgbHjDz7zjA= github.com/aws/aws-sdk-go v1.38.20 h1:QbzNx/tdfATbdKfubBpkt84OM6oBkxQZRw6+bW2GyeA= github.com/aws/aws-sdk-go v1.38.20/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro= +github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A= github.com/census-instrumentation/opencensus-proto v0.4.1 h1:iKLQ0xPNFxR/2hzXZMrBo8f1j86j5WHzznCCQxV/b8g= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 h1:/inchEIKaYC1Akx+H+gqO04wryn5h75LSazbRlnya1k= -github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c= @@ -79,7 +78,6 @@ github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/q github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34= github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM= github.com/minio/minio-go/v7 v7.0.62 h1:qNYsFZHEzl+NfH8UxW4jpmlKav1qUAgfY30YNRneVhc= @@ -101,8 +99,9 @@ github.com/pierrec/lz4/v4 v4.1.15 h1:MO0/ucJhngq7299dKLwIMtgTfbkoSPF6AoMYDd8Q4q0 github.com/pierrec/lz4/v4 v4.1.15/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.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= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc= @@ -114,15 +113,14 @@ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVs github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= -github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= +github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/u2takey/ffmpeg-go v0.5.0 h1:r7d86XuL7uLWJ5mzSeQ03uvjfIhiJYvsRAJFCW4uklU= github.com/u2takey/ffmpeg-go v0.5.0/go.mod h1:ruZWkvC1FEiUNjmROowOAps3ZcWxEiOpFoHCvk97kGc= github.com/u2takey/go-utils v0.3.1 h1:TaQTgmEZZeDHQFYfd+AdUT1cT4QJgJn/XVPELhHw4ys= @@ -136,6 +134,11 @@ github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gi github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8= go.uber.org/automaxprocs v1.5.3/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0= +go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= +go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= +go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.25.0 h1:4Hvk6GtkucQ790dqmj7l1eEnRdKm3k3ZUrUMS2d5+5c= +go.uber.org/zap v1.25.0/go.mod h1:JIAUzQIH94IC4fOJQm7gMmBJP5k7wQfdcnYdPoEXJYk= gocv.io/x/gocv v0.25.0/go.mod h1:Rar2PS6DV+T4FL+PM535EImD/h13hGVaHhnCu1xarBs= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= @@ -206,6 +209,8 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc= +gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/pkg/logX/logX.go b/pkg/logX/logX.go index 638789d..03df3d5 100644 --- a/pkg/logX/logX.go +++ b/pkg/logX/logX.go @@ -2,73 +2,118 @@ package logX import ( "fmt" - "io" "os" + "strings" "time" - nested "github.com/antonfisher/nested-logrus-formatter" - kralog "github.com/go-kratos/kratos/v2/log" - log "github.com/sirupsen/logrus" + "github.com/go-kratos/kratos/v2/log" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "gopkg.in/natefinch/lumberjack.v2" ) -type Log struct { - Logger *log.Logger -} +const ( + LogMaxSize = 10 + LogMaxBackups = 5 + LogMaxAge = 30 + SkipCaller = 2 +) -func NewDefaultLogger() *Log { - logger := log.New() - logger.SetFormatter(&nested.Formatter{ - HideKeys: false, - NoColors: false, - ShowFullLevel: true, - TimestampFormat: "2006-01-02 15:04:05", - FieldsOrder: []string{"service", "caller", "module", "msg"}, - }) - return &Log{ - Logger: logger, - } +var _ log.Logger = (*ZapLogger)(nil) + +type ZapLogger struct { + log *zap.Logger + Sync func() error } -func (l *Log) Log(level kralog.Level, keyVal ...interface{}) error { - entry := l.Logger.WithFields(log.Fields{}) - for i := 0; i < len(keyVal); i += 2 { - key := keyVal[i] - val := keyVal[i+1] - entry = entry.WithField(key.(string), val) +// Logger 配置zap日志,将zap日志库引入 +func Logger() log.Logger { + timeEncoder := func(t time.Time, e zapcore.PrimitiveArrayEncoder) { + var builder strings.Builder + builder.WriteString("[") + builder.WriteString(t.Format("2006-01-02 15:04:05.000")) + builder.WriteString("]") + e.AppendString(builder.String()) } - - switch level { - case kralog.LevelDebug: - entry.Debug() - case kralog.LevelInfo: - entry.Info() - case kralog.LevelWarn: - entry.Warn() - case kralog.LevelError: - entry.Error() - default: - entry.Print() + levelEncoder := func(l zapcore.Level, e zapcore.PrimitiveArrayEncoder) { + var builder strings.Builder + builder.WriteString("[") + builder.WriteString(l.CapitalString()) + builder.WriteString("]") + e.AppendString(builder.String()) } - return nil + encoder := zapcore.EncoderConfig{ + TimeKey: "time", + LevelKey: "level", + StacktraceKey: "stack", + EncodeTime: timeEncoder, + EncodeLevel: levelEncoder, + LineEnding: zapcore.DefaultLineEnding, + EncodeDuration: zapcore.SecondsDurationEncoder, + } + return NewZapLogger( + encoder, + zap.NewAtomicLevelAt(zapcore.DebugLevel), + zap.AddStacktrace( + zap.NewAtomicLevelAt(zapcore.ErrorLevel)), + zap.AddCaller(), + zap.AddCallerSkip(SkipCaller), + zap.Development(), + ) } -func (l *Log) SetOutput(w io.Writer) { - l.Logger.SetOutput(w) +// 日志自动切割,采用 lumberjack 实现的 +func getLogWriter() zapcore.WriteSyncer { + lumberJackLogger := &lumberjack.Logger{ + Filename: "../../../../logs/zap.log", + MaxSize: LogMaxSize, // 日志的最大大小(M) + MaxBackups: LogMaxBackups, // 日志的最大保存数量 + MaxAge: LogMaxAge, // 日志文件存储最大天数 + Compress: false, // 是否执行压缩 + } + return zapcore.AddSync(lumberJackLogger) } -func (l *Log) SetLevel(level kralog.Level) { - l.Logger.SetLevel(log.Level(level)) +// NewZapLogger return a zap logger. +func NewZapLogger(encoder zapcore.EncoderConfig, level zap.AtomicLevel, opts ...zap.Option) *ZapLogger { + // 日志切割 + writeSyncer := getLogWriter() + // 设置日志级别 + level.SetLevel(zap.InfoLevel) + core := zapcore.NewCore( + // 编码器配置 + zapcore.NewConsoleEncoder(encoder), + // 打印到控制台和文件 + zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(writeSyncer)), + // 日志级别 + level, + ) + zapLogger := zap.New(core, opts...) + return &ZapLogger{log: zapLogger, Sync: zapLogger.Sync} } -func (l *Log) FilePath(path string) (*os.File, error) { - mode := 0o666 - return os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, os.FileMode(mode)) -} +// Log 实现log接口 +func (l *ZapLogger) Log(level log.Level, keyvals ...interface{}) error { + if len(keyvals) == 0 || len(keyvals)%2 != 0 { + l.log.Warn(fmt.Sprint("Keyvalues must appear in pairs: ", keyvals)) + return nil + } + var data []zap.Field + for i := 0; i < len(keyvals); i += 2 { + data = append(data, zap.Any(fmt.Sprint(keyvals[i]), keyvals[i+1])) + } -func (l *Log) SetTimeFileName(name string, flag bool) string { - timeState := time.Now() - if flag { - return fmt.Sprint(name, timeState.Format("2006-01-02 15:04:05"), ".log") + switch level { + case log.LevelDebug: + l.log.Debug("", data...) + case log.LevelInfo: + l.log.Info("", data...) + case log.LevelWarn: + l.log.Warn("", data...) + case log.LevelError: + l.log.Error("", data...) + case log.LevelFatal: + l.log.Fatal("", data...) } - return fmt.Sprint(name, timeState.Format("2006-01-02"), ".log") + return nil }