diff --git a/auth/options.go b/auth/options.go index fceede8d..fd77d72e 100644 --- a/auth/options.go +++ b/auth/options.go @@ -4,6 +4,7 @@ import ( "context" "time" + "github.com/unistack-org/micro/v3/logger" "github.com/unistack-org/micro/v3/store" ) @@ -34,6 +35,8 @@ type Options struct { Store store.Store // Addrs sets the addresses of auth Addrs []string + // Logger sets the logger + Logger logger.Logger // Context to store other options Context context.Context } diff --git a/broker/options.go b/broker/options.go index 889b4df4..2a0bbecb 100644 --- a/broker/options.go +++ b/broker/options.go @@ -5,6 +5,7 @@ import ( "crypto/tls" "github.com/unistack-org/micro/v3/codec" + "github.com/unistack-org/micro/v3/logger" "github.com/unistack-org/micro/v3/registry" ) @@ -13,6 +14,8 @@ type Options struct { Secure bool Codec codec.Marshaler + // Logger + Logger logger.Logger // Handler executed when errors occur processing messages ErrorHandler Handler @@ -145,6 +148,13 @@ func TLSConfig(t *tls.Config) Option { } } +// Logger sets the logger +func Logger(l logger.Logger) Option { + return func(o *Options) { + o.Logger = l + } +} + // SubscribeContext set context func SubscribeContext(ctx context.Context) SubscribeOption { return func(o *SubscribeOptions) { diff --git a/client/options.go b/client/options.go index c17eaf6c..59de1580 100644 --- a/client/options.go +++ b/client/options.go @@ -6,6 +6,7 @@ import ( "github.com/unistack-org/micro/v3/broker" "github.com/unistack-org/micro/v3/codec" + "github.com/unistack-org/micro/v3/logger" "github.com/unistack-org/micro/v3/registry" "github.com/unistack-org/micro/v3/router" "github.com/unistack-org/micro/v3/selector" @@ -25,7 +26,7 @@ type Options struct { Router router.Router Selector selector.Selector Transport transport.Transport - + Logger logger.Logger // Lookup used for looking up routes Lookup LookupFunc @@ -137,6 +138,12 @@ func Broker(b broker.Broker) Option { } } +func Logger(l logger.Logger) Option { + return func(o *Options) { + o.Logger = l + } +} + // Codec to be used to encode/decode requests for a given content type func Codec(contentType string, c codec.NewCodec) Option { return func(o *Options) { @@ -182,7 +189,9 @@ func Transport(t transport.Transport) Option { // Registry sets the routers registry func Registry(r registry.Registry) Option { return func(o *Options) { - o.Router.Init(router.Registry(r)) + if o.Router != nil { + o.Router.Init(router.Registry(r)) + } } } diff --git a/metrics/options.go b/metrics/options.go index e760ef1a..a38e183f 100644 --- a/metrics/options.go +++ b/metrics/options.go @@ -1,5 +1,7 @@ package metrics +import "github.com/unistack-org/micro/v3/logger" + var ( // The Prometheus metrics will be made available on this port: defaultPrometheusListenAddress = ":9000" @@ -18,6 +20,7 @@ type Options struct { Path string DefaultTags Tags TimingObjectives map[float64]float64 + Logger logger.Logger } // NewOptions prepares a set of options: @@ -63,3 +66,10 @@ func TimingObjectives(value map[float64]float64) Option { o.TimingObjectives = value } } + +// Logger sets the logger +func Logger(l logger.Logger) Option { + return func(o *Options) { + o.Logger = l + } +} diff --git a/network/options.go b/network/options.go index 000dc36c..2ab05100 100644 --- a/network/options.go +++ b/network/options.go @@ -2,6 +2,7 @@ package network import ( "github.com/google/uuid" + "github.com/unistack-org/micro/v3/logger" "github.com/unistack-org/micro/v3/proxy" "github.com/unistack-org/micro/v3/router" "github.com/unistack-org/micro/v3/tunnel" @@ -27,6 +28,8 @@ type Options struct { Router router.Router // Proxy is network proxy Proxy proxy.Proxy + // Logger + Logger logger.Logger } // Id sets the id of the network node @@ -85,6 +88,13 @@ func Proxy(p proxy.Proxy) Option { } } +// Logger sets the network logger +func Logger(l logger.Logger) Option { + return func(o *Options) { + o.Logger = l + } +} + // DefaultOptions returns network default options func DefaultOptions() Options { return Options{ diff --git a/options.go b/options.go index 74b2825e..d07c59ff 100644 --- a/options.go +++ b/options.go @@ -12,6 +12,7 @@ import ( "github.com/unistack-org/micro/v3/config" "github.com/unistack-org/micro/v3/debug/profile" "github.com/unistack-org/micro/v3/debug/trace" + "github.com/unistack-org/micro/v3/logger" "github.com/unistack-org/micro/v3/registry" "github.com/unistack-org/micro/v3/router" "github.com/unistack-org/micro/v3/runtime" @@ -25,6 +26,7 @@ import ( type Options struct { Auth auth.Auth Broker broker.Broker + Logger logger.Logger Cmd cmd.Cmd Config config.Config Client client.Client @@ -51,8 +53,20 @@ type Options struct { func newOptions(opts ...Option) Options { opt := Options{ - Context: context.Background(), - Signal: true, + Context: context.Background(), + Signal: true, + Server: server.DefaultServer, + Client: client.DefaultClient, + Broker: broker.DefaultBroker, + Registry: registry.DefaultRegistry, + Router: router.DefaultRouter, + Auth: auth.DefaultAuth, + Logger: logger.DefaultLogger, + Config: config.DefaultConfig, + Store: store.DefaultStore, + Transport: transport.DefaultTransport, + //Runtime runtime.Runtime + //Profile profile.Profile } for _, o := range opts { @@ -68,9 +82,13 @@ type Option func(*Options) func Broker(b broker.Broker) Option { return func(o *Options) { o.Broker = b - // Update Client and Server - o.Client.Init(client.Broker(b)) - o.Server.Init(server.Broker(b)) + if o.Client != nil { + // Update Client and Server + o.Client.Init(client.Broker(b)) + } + if o.Server != nil { + o.Server.Init(server.Broker(b)) + } } } @@ -125,24 +143,40 @@ func Store(s store.Store) Option { } } +// Logger set the logger to use +func Logger(l logger.Logger) Option { + return func(o *Options) { + o.Logger = l + } +} + // Registry sets the registry for the service // and the underlying components func Registry(r registry.Registry) Option { return func(o *Options) { o.Registry = r - // Update router - o.Router.Init(router.Registry(r)) - // Update server - o.Server.Init(server.Registry(r)) - // Update Broker - o.Broker.Init(broker.Registry(r)) + if o.Router != nil { + // Update router + o.Router.Init(router.Registry(r)) + } + if o.Server != nil { + // Update server + o.Server.Init(server.Registry(r)) + } + if o.Broker != nil { + // Update Broker + o.Broker.Init(broker.Registry(r)) + } } } // Tracer sets the tracer for the service func Tracer(t trace.Tracer) Option { return func(o *Options) { - o.Server.Init(server.Tracer(t)) + if o.Server != nil { + //todo client trace + o.Server.Init(server.Tracer(t)) + } } } @@ -150,7 +184,9 @@ func Tracer(t trace.Tracer) Option { func Auth(a auth.Auth) Option { return func(o *Options) { o.Auth = a - o.Server.Init(server.Auth(a)) + if o.Server != nil { + o.Server.Init(server.Auth(a)) + } } } @@ -164,7 +200,9 @@ func Config(c config.Config) Option { // Selector sets the selector for the service client func Selector(s selector.Selector) Option { return func(o *Options) { - o.Client.Init(client.Selector(s)) + if o.Client != nil { + o.Client.Init(client.Selector(s)) + } } } @@ -174,8 +212,12 @@ func Transport(t transport.Transport) Option { return func(o *Options) { o.Transport = t // Update Client and Server - o.Client.Init(client.Transport(t)) - o.Server.Init(server.Transport(t)) + if o.Client != nil { + o.Client.Init(client.Transport(t)) + } + if o.Server != nil { + o.Server.Init(server.Transport(t)) + } } } @@ -191,7 +233,9 @@ func Router(r router.Router) Option { return func(o *Options) { o.Router = r // Update client - o.Client.Init(client.Router(r)) + if o.Client != nil { + o.Client.Init(client.Router(r)) + } } } @@ -200,56 +244,72 @@ func Router(r router.Router) Option { // Address sets the address of the server func Address(addr string) Option { return func(o *Options) { - o.Server.Init(server.Address(addr)) + if o.Server != nil { + o.Server.Init(server.Address(addr)) + } } } // Name of the service func Name(n string) Option { return func(o *Options) { - o.Server.Init(server.Name(n)) + if o.Server != nil { + o.Server.Init(server.Name(n)) + } } } // Version of the service func Version(v string) Option { return func(o *Options) { - o.Server.Init(server.Version(v)) + if o.Server != nil { + o.Server.Init(server.Version(v)) + } } } // Metadata associated with the service func Metadata(md map[string]string) Option { return func(o *Options) { - o.Server.Init(server.Metadata(md)) + if o.Server != nil { + o.Server.Init(server.Metadata(md)) + } } } // Flags that can be passed to service func Flags(flags ...cli.Flag) Option { return func(o *Options) { - o.Cmd.App().Flags = append(o.Cmd.App().Flags, flags...) + if o.Cmd != nil { + o.Cmd.App().Flags = append(o.Cmd.App().Flags, flags...) + } } } // Action can be used to parse user provided cli options func Action(a func(*cli.Context) error) Option { return func(o *Options) { - o.Cmd.App().Action = a + if o.Cmd != nil { + o.Cmd.App().Action = a + } } } // RegisterTTL specifies the TTL to use when registering the service func RegisterTTL(t time.Duration) Option { return func(o *Options) { - o.Server.Init(server.RegisterTTL(t)) + if o.Server != nil { + o.Server.Init(server.RegisterTTL(t)) + } } } // RegisterInterval specifies the interval on which to re-register func RegisterInterval(t time.Duration) Option { return func(o *Options) { - o.Server.Init(server.RegisterInterval(t)) + if o.Server != nil { + o.Server.Init(server.RegisterInterval(t)) + } } } diff --git a/proxy/options.go b/proxy/options.go index 78ed1b80..e5a518bc 100644 --- a/proxy/options.go +++ b/proxy/options.go @@ -3,6 +3,7 @@ package proxy import ( "github.com/unistack-org/micro/v3/client" + "github.com/unistack-org/micro/v3/logger" "github.com/unistack-org/micro/v3/router" ) @@ -16,6 +17,8 @@ type Options struct { Router router.Router // Extra links for different clients Links map[string]client.Client + // Logger + Logger logger.Logger } // Option func signature @@ -42,6 +45,13 @@ func WithRouter(r router.Router) Option { } } +// WithLogger specifies the logger to use +func WithLogger(l logger.Logger) Option { + return func(o *Options) { + o.Logger = l + } +} + // WithLink sets a link for outbound requests func WithLink(name string, c client.Client) Option { return func(o *Options) { diff --git a/registry/options.go b/registry/options.go index 3cc5cc43..b8f3bb42 100644 --- a/registry/options.go +++ b/registry/options.go @@ -4,6 +4,8 @@ import ( "context" "crypto/tls" "time" + + "github.com/unistack-org/micro/v3/logger" ) type Options struct { @@ -11,6 +13,7 @@ type Options struct { Timeout time.Duration Secure bool TLSConfig *tls.Config + Logger logger.Logger // Other options for implementations of the interface // can be stored in a context Context context.Context @@ -74,6 +77,13 @@ func Secure(b bool) Option { } } +// Logger sets the logger +func Logger(l logger.Logger) Option { + return func(o *Options) { + o.Logger = l + } +} + // Specify TLS Config func TLSConfig(t *tls.Config) Option { return func(o *Options) { diff --git a/router/options.go b/router/options.go index 3b76bd78..a2ee355a 100644 --- a/router/options.go +++ b/router/options.go @@ -4,6 +4,7 @@ import ( "context" "github.com/google/uuid" + "github.com/unistack-org/micro/v3/logger" "github.com/unistack-org/micro/v3/registry" ) @@ -19,10 +20,12 @@ type Options struct { Network string // Registry is the local registry Registry registry.Registry - // Context for additional options - Context context.Context // Precache routes Precache bool + // Logger + Logger logger.Logger + // Context for additional options + Context context.Context } // Id sets Router Id @@ -53,6 +56,13 @@ func Network(n string) Option { } } +// Logger sets the logger +func Logger(l logger.Logger) Option { + return func(o *Options) { + o.Logger = l + } +} + // Registry sets the local registry func Registry(r registry.Registry) Option { return func(o *Options) { diff --git a/runtime/options.go b/runtime/options.go index a14dfbb9..0bdcf58f 100644 --- a/runtime/options.go +++ b/runtime/options.go @@ -5,6 +5,7 @@ import ( "io" "github.com/unistack-org/micro/v3/client" + "github.com/unistack-org/micro/v3/logger" ) type Option func(o *Options) @@ -21,6 +22,15 @@ type Options struct { Image string // Client to use when making requests Client client.Client + // Logger + Logger logger.Logger +} + +// WithLogger sets the logger +func WithLogger(l logger.Logger) Option { + return func(o *Options) { + o.Logger = l + } } // WithSource sets the base image / repository diff --git a/server/options.go b/server/options.go index 18f6a8cd..603dc415 100644 --- a/server/options.go +++ b/server/options.go @@ -10,6 +10,7 @@ import ( "github.com/unistack-org/micro/v3/broker" "github.com/unistack-org/micro/v3/codec" "github.com/unistack-org/micro/v3/debug/trace" + "github.com/unistack-org/micro/v3/logger" "github.com/unistack-org/micro/v3/registry" "github.com/unistack-org/micro/v3/transport" ) @@ -20,6 +21,7 @@ type Options struct { Registry registry.Registry Tracer trace.Tracer Auth auth.Auth + Logger logger.Logger Transport transport.Transport Metadata map[string]string Name string @@ -98,6 +100,13 @@ func Namespace(n string) Option { } } +// Logger +func Logger(l logger.Logger) Option { + return func(o *Options) { + o.Logger = l + } +} + // Unique server id func Id(id string) Option { return func(o *Options) { diff --git a/store/options.go b/store/options.go index 2107be2f..e6972ff5 100644 --- a/store/options.go +++ b/store/options.go @@ -3,6 +3,8 @@ package store import ( "context" "time" + + "github.com/unistack-org/micro/v3/logger" ) // Options contains configuration for the Store @@ -15,6 +17,8 @@ type Options struct { Database string // Table is analag for a table in database backends or a key prefix in KV backends Table string + // Logger + Logger logger.Logger // Context should contain all implementation specific options, using context.WithValue. Context context.Context } @@ -22,6 +26,13 @@ type Options struct { // Option sets values in Options type Option func(o *Options) +// Logger sets the logger +func Logger(l logger.Logger) Option { + return func(o *Options) { + o.Logger = l + } +} + // Nodes contains the addresses or other connection information of the backing storage. // For example, an etcd implementation would contain the nodes of the cluster. // A SQL implementation could contain one or more connection strings. diff --git a/sync/options.go b/sync/options.go index 6c546356..d4f9d1c4 100644 --- a/sync/options.go +++ b/sync/options.go @@ -2,8 +2,36 @@ package sync import ( "time" + + "github.com/unistack-org/micro/v3/logger" ) +type Options struct { + Nodes []string + Prefix string + Logger logger.Logger +} + +type Option func(o *Options) + +type LeaderOptions struct{} + +type LeaderOption func(o *LeaderOptions) + +type LockOptions struct { + TTL time.Duration + Wait time.Duration +} + +type LockOption func(o *LockOptions) + +// Logger sets the logger +func Logger(l logger.Logger) Option { + return func(o *Options) { + o.Logger = l + } +} + // Nodes sets the addresses to use func Nodes(a ...string) Option { return func(o *Options) { diff --git a/sync/sync.go b/sync/sync.go index 0c5203b7..f15eb706 100644 --- a/sync/sync.go +++ b/sync/sync.go @@ -3,7 +3,6 @@ package sync import ( "errors" - "time" ) var ( @@ -33,21 +32,3 @@ type Leader interface { // status returns when leadership is lost Status() chan bool } - -type Options struct { - Nodes []string - Prefix string -} - -type Option func(o *Options) - -type LeaderOptions struct{} - -type LeaderOption func(o *LeaderOptions) - -type LockOptions struct { - TTL time.Duration - Wait time.Duration -} - -type LockOption func(o *LockOptions) diff --git a/transport/options.go b/transport/options.go index 0fc1617c..7cfe3a3c 100644 --- a/transport/options.go +++ b/transport/options.go @@ -6,6 +6,7 @@ import ( "time" "github.com/unistack-org/micro/v3/codec" + "github.com/unistack-org/micro/v3/logger" ) type Options struct { @@ -23,6 +24,8 @@ type Options struct { TLSConfig *tls.Config // Timeout sets the timeout for Send/Recv Timeout time.Duration + // Logger + Logger logger.Logger // Other options for implementations of the interface // can be stored in a context Context context.Context @@ -59,6 +62,13 @@ func Addrs(addrs ...string) Option { } } +// Logger sets the logger +func Logger(l logger.Logger) Option { + return func(o *Options) { + o.Logger = l + } +} + // Codec sets the codec used for encoding where the transport // does not support message headers func Codec(c codec.Marshaler) Option { diff --git a/tunnel/options.go b/tunnel/options.go index 5d1c1df7..ded756a0 100644 --- a/tunnel/options.go +++ b/tunnel/options.go @@ -4,6 +4,7 @@ import ( "time" "github.com/google/uuid" + "github.com/unistack-org/micro/v3/logger" "github.com/unistack-org/micro/v3/transport" ) @@ -28,6 +29,8 @@ type Options struct { Token string // Transport listens to incoming connections Transport transport.Transport + // Logger + Logger logger.Logger } type DialOption func(*DialOptions) @@ -59,6 +62,13 @@ func Id(id string) Option { } } +// Logger sets the logger +func Logger(l logger.Logger) Option { + return func(o *Options) { + o.Logger = l + } +} + // The tunnel address func Address(a string) Option { return func(o *Options) {