From ea396a392589d175f8663be9672dc5f4d382d8e9 Mon Sep 17 00:00:00 2001 From: houseme Date: Sun, 9 Oct 2022 09:10:02 +0800 Subject: [PATCH] feat: improve glog for polaris register (#2178) feat: improve glog --- contrib/registry/polaris/README.MD | 7 +++++-- contrib/registry/polaris/README_ZH.MD | 10 +++++++--- contrib/registry/polaris/go.mod | 2 +- contrib/registry/polaris/go.sum | 7 ++++--- contrib/registry/polaris/polaris.go | 11 +++++++++++ contrib/registry/polaris/polaris_registrar.go | 7 +++---- example/registry/polaris/client/main.go | 3 +-- example/registry/polaris/server/main.go | 3 +-- 8 files changed, 33 insertions(+), 17 deletions(-) diff --git a/contrib/registry/polaris/README.MD b/contrib/registry/polaris/README.MD index c60572b21..8a8c2c94b 100644 --- a/contrib/registry/polaris/README.MD +++ b/contrib/registry/polaris/README.MD @@ -4,22 +4,24 @@ English | [简体中文](README_ZH.MD) Use `PolarisMesh` as service registration, discovery management and heartbeat reporting. - ## Installation + ``` go get -u -v github.com/gogf/gf/contrib/registry/polaris/v2 ``` + suggested using `go.mod`: + ``` require github.com/gogf/gf/contrib/registry/polaris/v2 latest ``` - ## Example ### Reference example [server](example/registry/polaris/server/main.go) + ```go package main @@ -48,6 +50,7 @@ func main() { ``` [client](example/registry/polaris/client/main.go) + ```go package main diff --git a/contrib/registry/polaris/README_ZH.MD b/contrib/registry/polaris/README_ZH.MD index 587da4ead..543ee9e06 100644 --- a/contrib/registry/polaris/README_ZH.MD +++ b/contrib/registry/polaris/README_ZH.MD @@ -4,27 +4,30 @@ 使用`PolarisMesh`作为服务注册、发现管理和心跳上报。 +## 安装 -## Installation ``` go get -u -v github.com/gogf/gf/contrib/registry/polaris/v2 ``` + suggested using `go.mod`: + ``` require github.com/gogf/gf/contrib/registry/polaris/v2 latest ``` -## Limitation +## Golang版本限制 + ``` golang version >= 1.15 ``` - ## 示例 ### 引用示例 [服务端](example/registry/polaris/server/main.go) + ```go package main @@ -54,6 +57,7 @@ func main() { ``` [客户端](example/registry/polaris/client/main.go) + ```go package main diff --git a/contrib/registry/polaris/go.mod b/contrib/registry/polaris/go.mod index f3c6027b2..40fef153d 100644 --- a/contrib/registry/polaris/go.mod +++ b/contrib/registry/polaris/go.mod @@ -4,7 +4,7 @@ go 1.15 require ( github.com/gogf/gf/v2 v2.0.0 - github.com/polarismesh/polaris-go v1.2.0-beta.0.0.20220517041223-596a6a63b00f + github.com/polarismesh/polaris-go v1.2.0-beta.3 ) replace github.com/gogf/gf/v2 => ../../../ diff --git a/contrib/registry/polaris/go.sum b/contrib/registry/polaris/go.sum index 8a496ace5..44400aa8c 100644 --- a/contrib/registry/polaris/go.sum +++ b/contrib/registry/polaris/go.sum @@ -70,6 +70,8 @@ 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/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/dlclark/regexp2 v1.7.0 h1:7lJfhqlPssTb1WQx4yvTHN0uElPEv52sbaECrAQxjAo= +github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= @@ -82,7 +84,6 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI= github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU= -github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= @@ -251,8 +252,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= 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/polarismesh/polaris-go v1.2.0-beta.0.0.20220517041223-596a6a63b00f h1:IL2vXn/LjasI79p2X0/j8DZ+XnV5wgnjNpO1PQjn9Bc= -github.com/polarismesh/polaris-go v1.2.0-beta.0.0.20220517041223-596a6a63b00f/go.mod h1:xXTl4b5ybYkwvXZA+nc1HNyLK/bsHUg08R4ewTa9axc= +github.com/polarismesh/polaris-go v1.2.0-beta.3 h1:sqN50VGign37xVFp9nrN1RoLXacsB0QfRYUtuCWMJGI= +github.com/polarismesh/polaris-go v1.2.0-beta.3/go.mod h1:HsN0ierETIujHpmnnYJ3qkwQw4QGAECuHvBZTDaw1tI= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M= diff --git a/contrib/registry/polaris/polaris.go b/contrib/registry/polaris/polaris.go index b01f9945a..00f1e99fe 100644 --- a/contrib/registry/polaris/polaris.go +++ b/contrib/registry/polaris/polaris.go @@ -13,7 +13,9 @@ import ( "github.com/polarismesh/polaris-go" "github.com/polarismesh/polaris-go/pkg/config" + "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/gsvc" + "github.com/gogf/gf/v2/os/glog" ) var ( @@ -60,6 +62,9 @@ type options struct { // optional, retry count. Default value is global config RetryCount int + + // optional, logger for polaris + Logger glog.ILogger } // Option The option is a polaris option. @@ -123,6 +128,11 @@ func WithHeartbeat(heartbeat bool) Option { return func(o *options) { o.Heartbeat = heartbeat } } +// WithLogger with the Logger option. +func WithLogger(logger glog.ILogger) Option { + return func(o *options) { o.Logger = logger } +} + // New create a new registry. func New(provider polaris.ProviderAPI, consumer polaris.ConsumerAPI, opts ...Option) (r *Registry) { op := options{ @@ -137,6 +147,7 @@ func New(provider polaris.ProviderAPI, consumer polaris.ConsumerAPI, opts ...Opt TTL: 0, Timeout: 0, RetryCount: 0, + Logger: g.Log(), } for _, option := range opts { option(&op) diff --git a/contrib/registry/polaris/polaris_registrar.go b/contrib/registry/polaris/polaris_registrar.go index 90ce34bf1..0741f0d64 100644 --- a/contrib/registry/polaris/polaris_registrar.go +++ b/contrib/registry/polaris/polaris_registrar.go @@ -13,7 +13,6 @@ import ( "github.com/polarismesh/polaris-go" "github.com/polarismesh/polaris-go/pkg/model" - "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/gsvc" "github.com/gogf/gf/v2/text/gstr" "github.com/gogf/gf/v2/util/gconv" @@ -134,12 +133,12 @@ func (r *Registry) doHeartBeat(ctx context.Context, instanceID string, service g }, }) if err != nil { - g.Log().Error(ctx, err.Error()) + r.opt.Logger.Error(ctx, err.Error()) continue } - g.Log().Debug(ctx, "heartbeat success") + r.opt.Logger.Debug(ctx, "heartbeat success") case <-r.c: - g.Log().Debug(ctx, "stop heartbeat") + r.opt.Logger.Debug(ctx, "stop heartbeat") return } } diff --git a/example/registry/polaris/client/main.go b/example/registry/polaris/client/main.go index d45a55e8f..7be0f412e 100644 --- a/example/registry/polaris/client/main.go +++ b/example/registry/polaris/client/main.go @@ -17,8 +17,7 @@ import ( func main() { conf := config.NewDefaultConfiguration([]string{"192.168.100.222:8091"}) conf.Consumer.LocalCache.SetPersistDir("/tmp/polaris/backup") - err := api.SetLoggersDir("/tmp/polaris/log") - if err != nil { + if err := api.SetLoggersDir("/tmp/polaris/log"); err != nil { g.Log().Fatal(context.Background(), err) } diff --git a/example/registry/polaris/server/main.go b/example/registry/polaris/server/main.go index d296aff4f..7e92c697e 100644 --- a/example/registry/polaris/server/main.go +++ b/example/registry/polaris/server/main.go @@ -15,8 +15,7 @@ import ( func main() { conf := config.NewDefaultConfiguration([]string{"192.168.100.222:8091"}) conf.Consumer.LocalCache.SetPersistDir("/tmp/polaris/backup") - err := api.SetLoggersDir("/tmp/polaris/log") - if err != nil { + if err := api.SetLoggersDir("/tmp/polaris/log"); err != nil { g.Log().Fatal(context.Background(), err) }