From fe08e8b6c9f28771a7be62dc2ed826564bb46ad3 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Tue, 20 Jun 2023 15:12:35 +0800 Subject: [PATCH] zk logger --- pkg/common/log/zap.go | 12 +++++++++++ pkg/discoveryregistry/zookeeper/discover.go | 22 ++++++++------------- pkg/discoveryregistry/zookeeper/resolver.go | 6 +++--- pkg/discoveryregistry/zookeeper/zk.go | 4 +--- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/pkg/common/log/zap.go b/pkg/common/log/zap.go index e6fc8fae8..1fc9448e1 100644 --- a/pkg/common/log/zap.go +++ b/pkg/common/log/zap.go @@ -44,18 +44,30 @@ func InitFromConfig(loggerPrefixName, moduleName string, logLevel int, isStdout } func ZDebug(ctx context.Context, msg string, keysAndValues ...interface{}) { + if pkgLogger == nil { + return + } pkgLogger.Debug(ctx, msg, keysAndValues...) } func ZInfo(ctx context.Context, msg string, keysAndValues ...interface{}) { + if pkgLogger == nil { + return + } pkgLogger.Info(ctx, msg, keysAndValues...) } func ZWarn(ctx context.Context, msg string, err error, keysAndValues ...interface{}) { + if pkgLogger == nil { + return + } pkgLogger.Warn(ctx, msg, err, keysAndValues...) } func ZError(ctx context.Context, msg string, err error, keysAndValues ...interface{}) { + if pkgLogger == nil { + return + } pkgLogger.Error(ctx, msg, err, keysAndValues...) } diff --git a/pkg/discoveryregistry/zookeeper/discover.go b/pkg/discoveryregistry/zookeeper/discover.go index d51b0531d..8a9045a63 100644 --- a/pkg/discoveryregistry/zookeeper/discover.go +++ b/pkg/discoveryregistry/zookeeper/discover.go @@ -6,7 +6,6 @@ import ( "strings" "sync" - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/log" "github.com/pkg/errors" "github.com/go-zookeeper/zk" @@ -22,10 +21,10 @@ func (s *ZkClient) watch(wg *sync.WaitGroup) { event := <-s.eventChan switch event.Type { case zk.EventSession: - log.ZDebug(context.Background(), "zk session event", "event", event) + s.logger.Printf("zk session event: %+v", event) case zk.EventNodeCreated: case zk.EventNodeChildrenChanged: - log.ZDebug(context.Background(), "zk event", "event", event.Path) + s.logger.Printf("zk event: %s", event.Path) l := strings.Split(event.Path, "/") if len(l) > 1 { s.lock.Lock() @@ -35,9 +34,8 @@ func (s *ZkClient) watch(wg *sync.WaitGroup) { delete(s.localConns, rpcName) } s.lock.Unlock() - } - log.ZDebug(context.Background(), "zk event handle success", "event", event.Path) + s.logger.Printf("zk event handle success: %s", event.Path) case zk.EventNodeDataChanged: case zk.EventNodeDeleted: case zk.EventNotWatching: @@ -74,13 +72,13 @@ func (s *ZkClient) GetConnsRemote(serviceName string) (conns []resolver.Address, } func (s *ZkClient) GetConns(ctx context.Context, serviceName string, opts ...grpc.DialOption) ([]*grpc.ClientConn, error) { - log.ZDebug(ctx, "get conns from client", "serviceName", serviceName) + s.logger.Printf("get conns from client, serviceName: %s", serviceName) s.lock.Lock() opts = append(s.options, opts...) conns := s.localConns[serviceName] if len(conns) == 0 { var err error - log.ZDebug(ctx, "get conns from zk remote", "serviceName", serviceName) + s.logger.Printf("get conns from zk remote, serviceName: %s", serviceName) conns, err = s.GetConnsRemote(serviceName) if err != nil { s.lock.Unlock() @@ -90,7 +88,7 @@ func (s *ZkClient) GetConns(ctx context.Context, serviceName string, opts ...grp } s.lock.Unlock() var ret []*grpc.ClientConn - log.ZDebug(ctx, "get conns from zk success", "conns", conns) + s.logger.Printf("get conns from zk success, serviceName: %s", serviceName) for _, conn := range conns { c, err := grpc.DialContext(ctx, conn.Addr, append(s.options, opts...)...) if err != nil { @@ -98,14 +96,10 @@ func (s *ZkClient) GetConns(ctx context.Context, serviceName string, opts ...grp } ret = append(ret, c) } - log.ZDebug(ctx, "dial ctx success", "conns", ret) + s.logger.Printf("dial ctx success, serviceName: %s", serviceName) return ret, nil } -// func (s *ZkClient) GetConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) { -// newOpts := append(s.options, grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"LoadBalancingPolicy": "%s"}`, s.balancerName))) -// return grpc.DialContext(ctx, fmt.Sprintf("%s:///%s", s.scheme, serviceName), append(newOpts, opts...)...) -// } func (s *ZkClient) GetConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) { conns, err := s.GetConns(ctx, serviceName, opts...) if err != nil { @@ -114,7 +108,7 @@ func (s *ZkClient) GetConn(ctx context.Context, serviceName string, opts ...grpc if len(conns) == 0 { return nil, ErrConnIsNil } - log.ZDebug(ctx, "get conn from conns", "conns", conns) + s.logger.Printf("get conn from conns, serviceName: %s", serviceName) return s.getConnBalance(conns) } diff --git a/pkg/discoveryregistry/zookeeper/resolver.go b/pkg/discoveryregistry/zookeeper/resolver.go index 0effcd461..8a9002974 100644 --- a/pkg/discoveryregistry/zookeeper/resolver.go +++ b/pkg/discoveryregistry/zookeeper/resolver.go @@ -2,9 +2,9 @@ package zookeeper import ( "context" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/log" "strings" - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/log" "google.golang.org/grpc/resolver" ) @@ -33,7 +33,7 @@ func (r *Resolver) ResolveNow(o resolver.ResolveNowOptions) { func (s *Resolver) Close() {} func (s *ZkClient) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) { - log.ZDebug(context.Background(), "build resolver", "target", target, "cc", cc) + s.logger.Printf("build resolver: %+v, cc: %+v", target, cc) r := &Resolver{} r.target = target r.cc = cc @@ -42,7 +42,7 @@ func (s *ZkClient) Build(target resolver.Target, cc resolver.ClientConn, opts re s.lock.Lock() defer s.lock.Unlock() s.resolvers[strings.TrimLeft(target.URL.Path, "/")] = r - log.ZDebug(context.Background(), "build resolver finished", "target", target, "cc", cc) + s.logger.Printf("build resolver finished: %+v, cc: %+v", target, cc) return r, nil } diff --git a/pkg/discoveryregistry/zookeeper/zk.go b/pkg/discoveryregistry/zookeeper/zk.go index 36b75f0af..cd09e028e 100644 --- a/pkg/discoveryregistry/zookeeper/zk.go +++ b/pkg/discoveryregistry/zookeeper/zk.go @@ -1,13 +1,11 @@ package zookeeper import ( - "context" "net" "strconv" "sync" "time" - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/log" "github.com/go-zookeeper/zk" "google.golang.org/grpc" "google.golang.org/grpc/resolver" @@ -142,7 +140,7 @@ func (s *ZkClient) ensureAndCreate(node string) error { func (s *ZkClient) refresh(wg *sync.WaitGroup) { for range s.ticker.C { - log.ZDebug(context.Background(), "refresh local conns") + s.logger.Printf("refresh local conns") s.lock.Lock() for rpcName := range s.resolvers { s.flushResolver(rpcName)