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 }