From 2fdd6dfdc3408396c155d873bea12cc364f7a557 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 19 Jan 2021 21:59:14 +0300 Subject: [PATCH] fix init Signed-off-by: Vasiliy Tolstov --- service.go | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/service.go b/service.go index 4789cd4..bf1aad2 100644 --- a/service.go +++ b/service.go @@ -2,19 +2,22 @@ package service import ( "context" + "fmt" "os" "github.com/google/uuid" pb "github.com/unistack-org/micro-logger-service/v3/proto" + "github.com/unistack-org/micro/v3/client" "github.com/unistack-org/micro/v3/logger" "github.com/unistack-org/micro/v3/store" ) type serviceLogger struct { - opts logger.Options - client pb.LoggerService - store store.Store - fields map[string]interface{} + opts logger.Options + service string + client pb.LoggerService + store store.Store + fields map[string]interface{} } func (l *serviceLogger) Init(opts ...logger.Option) error { @@ -22,6 +25,26 @@ func (l *serviceLogger) Init(opts ...logger.Option) error { o(&l.opts) } + var cli client.Client + if l.opts.Context != nil { + if v, ok := l.opts.Context.Value(clientKey{}).(client.Client); ok && v != nil { + cli = v + } + if v, ok := l.opts.Context.Value(serviceKey{}).(string); ok && v != "" { + l.service = v + } + } + + if l.service == "" { + return fmt.Errorf("missing Service option") + } + + if cli == nil { + return fmt.Errorf("missing Client option") + } + + l.client = pb.NewLoggerService(l.service, cli) + return nil }