add logger to options

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-08-29 17:44:49 +03:00
parent 2382446e10
commit 53654185ba
16 changed files with 239 additions and 48 deletions

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"time" "time"
"github.com/unistack-org/micro/v3/logger"
"github.com/unistack-org/micro/v3/store" "github.com/unistack-org/micro/v3/store"
) )
@ -34,6 +35,8 @@ type Options struct {
Store store.Store Store store.Store
// Addrs sets the addresses of auth // Addrs sets the addresses of auth
Addrs []string Addrs []string
// Logger sets the logger
Logger logger.Logger
// Context to store other options // Context to store other options
Context context.Context Context context.Context
} }

View File

@ -5,6 +5,7 @@ import (
"crypto/tls" "crypto/tls"
"github.com/unistack-org/micro/v3/codec" "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/registry"
) )
@ -13,6 +14,8 @@ type Options struct {
Secure bool Secure bool
Codec codec.Marshaler Codec codec.Marshaler
// Logger
Logger logger.Logger
// Handler executed when errors occur processing messages // Handler executed when errors occur processing messages
ErrorHandler Handler 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 // SubscribeContext set context
func SubscribeContext(ctx context.Context) SubscribeOption { func SubscribeContext(ctx context.Context) SubscribeOption {
return func(o *SubscribeOptions) { return func(o *SubscribeOptions) {

View File

@ -6,6 +6,7 @@ import (
"github.com/unistack-org/micro/v3/broker" "github.com/unistack-org/micro/v3/broker"
"github.com/unistack-org/micro/v3/codec" "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/registry"
"github.com/unistack-org/micro/v3/router" "github.com/unistack-org/micro/v3/router"
"github.com/unistack-org/micro/v3/selector" "github.com/unistack-org/micro/v3/selector"
@ -25,7 +26,7 @@ type Options struct {
Router router.Router Router router.Router
Selector selector.Selector Selector selector.Selector
Transport transport.Transport Transport transport.Transport
Logger logger.Logger
// Lookup used for looking up routes // Lookup used for looking up routes
Lookup LookupFunc 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 // Codec to be used to encode/decode requests for a given content type
func Codec(contentType string, c codec.NewCodec) Option { func Codec(contentType string, c codec.NewCodec) Option {
return func(o *Options) { return func(o *Options) {
@ -182,8 +189,10 @@ func Transport(t transport.Transport) Option {
// Registry sets the routers registry // Registry sets the routers registry
func Registry(r registry.Registry) Option { func Registry(r registry.Registry) Option {
return func(o *Options) { return func(o *Options) {
if o.Router != nil {
o.Router.Init(router.Registry(r)) o.Router.Init(router.Registry(r))
} }
}
} }
// Router is used to lookup routes for a service // Router is used to lookup routes for a service

View File

@ -1,5 +1,7 @@
package metrics package metrics
import "github.com/unistack-org/micro/v3/logger"
var ( var (
// The Prometheus metrics will be made available on this port: // The Prometheus metrics will be made available on this port:
defaultPrometheusListenAddress = ":9000" defaultPrometheusListenAddress = ":9000"
@ -18,6 +20,7 @@ type Options struct {
Path string Path string
DefaultTags Tags DefaultTags Tags
TimingObjectives map[float64]float64 TimingObjectives map[float64]float64
Logger logger.Logger
} }
// NewOptions prepares a set of options: // NewOptions prepares a set of options:
@ -63,3 +66,10 @@ func TimingObjectives(value map[float64]float64) Option {
o.TimingObjectives = value o.TimingObjectives = value
} }
} }
// Logger sets the logger
func Logger(l logger.Logger) Option {
return func(o *Options) {
o.Logger = l
}
}

View File

@ -2,6 +2,7 @@ package network
import ( import (
"github.com/google/uuid" "github.com/google/uuid"
"github.com/unistack-org/micro/v3/logger"
"github.com/unistack-org/micro/v3/proxy" "github.com/unistack-org/micro/v3/proxy"
"github.com/unistack-org/micro/v3/router" "github.com/unistack-org/micro/v3/router"
"github.com/unistack-org/micro/v3/tunnel" "github.com/unistack-org/micro/v3/tunnel"
@ -27,6 +28,8 @@ type Options struct {
Router router.Router Router router.Router
// Proxy is network proxy // Proxy is network proxy
Proxy proxy.Proxy Proxy proxy.Proxy
// Logger
Logger logger.Logger
} }
// Id sets the id of the network node // 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 // DefaultOptions returns network default options
func DefaultOptions() Options { func DefaultOptions() Options {
return Options{ return Options{

View File

@ -12,6 +12,7 @@ import (
"github.com/unistack-org/micro/v3/config" "github.com/unistack-org/micro/v3/config"
"github.com/unistack-org/micro/v3/debug/profile" "github.com/unistack-org/micro/v3/debug/profile"
"github.com/unistack-org/micro/v3/debug/trace" "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/registry"
"github.com/unistack-org/micro/v3/router" "github.com/unistack-org/micro/v3/router"
"github.com/unistack-org/micro/v3/runtime" "github.com/unistack-org/micro/v3/runtime"
@ -25,6 +26,7 @@ import (
type Options struct { type Options struct {
Auth auth.Auth Auth auth.Auth
Broker broker.Broker Broker broker.Broker
Logger logger.Logger
Cmd cmd.Cmd Cmd cmd.Cmd
Config config.Config Config config.Config
Client client.Client Client client.Client
@ -53,6 +55,18 @@ func newOptions(opts ...Option) Options {
opt := Options{ opt := Options{
Context: context.Background(), Context: context.Background(),
Signal: true, 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 { for _, o := range opts {
@ -68,10 +82,14 @@ type Option func(*Options)
func Broker(b broker.Broker) Option { func Broker(b broker.Broker) Option {
return func(o *Options) { return func(o *Options) {
o.Broker = b o.Broker = b
if o.Client != nil {
// Update Client and Server // Update Client and Server
o.Client.Init(client.Broker(b)) o.Client.Init(client.Broker(b))
}
if o.Server != nil {
o.Server.Init(server.Broker(b)) o.Server.Init(server.Broker(b))
} }
}
} }
func Cmd(c cmd.Cmd) Option { func Cmd(c cmd.Cmd) Option {
@ -125,33 +143,51 @@ 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 // Registry sets the registry for the service
// and the underlying components // and the underlying components
func Registry(r registry.Registry) Option { func Registry(r registry.Registry) Option {
return func(o *Options) { return func(o *Options) {
o.Registry = r o.Registry = r
if o.Router != nil {
// Update router // Update router
o.Router.Init(router.Registry(r)) o.Router.Init(router.Registry(r))
}
if o.Server != nil {
// Update server // Update server
o.Server.Init(server.Registry(r)) o.Server.Init(server.Registry(r))
}
if o.Broker != nil {
// Update Broker // Update Broker
o.Broker.Init(broker.Registry(r)) o.Broker.Init(broker.Registry(r))
} }
}
} }
// Tracer sets the tracer for the service // Tracer sets the tracer for the service
func Tracer(t trace.Tracer) Option { func Tracer(t trace.Tracer) Option {
return func(o *Options) { return func(o *Options) {
if o.Server != nil {
//todo client trace
o.Server.Init(server.Tracer(t)) o.Server.Init(server.Tracer(t))
} }
}
} }
// Auth sets the auth for the service // Auth sets the auth for the service
func Auth(a auth.Auth) Option { func Auth(a auth.Auth) Option {
return func(o *Options) { return func(o *Options) {
o.Auth = a o.Auth = a
if o.Server != nil {
o.Server.Init(server.Auth(a)) o.Server.Init(server.Auth(a))
} }
}
} }
// Config sets the config for the service // Config sets the config for the service
@ -164,8 +200,10 @@ func Config(c config.Config) Option {
// Selector sets the selector for the service client // Selector sets the selector for the service client
func Selector(s selector.Selector) Option { func Selector(s selector.Selector) Option {
return func(o *Options) { return func(o *Options) {
if o.Client != nil {
o.Client.Init(client.Selector(s)) o.Client.Init(client.Selector(s))
} }
}
} }
// Transport sets the transport for the service // Transport sets the transport for the service
@ -174,9 +212,13 @@ func Transport(t transport.Transport) Option {
return func(o *Options) { return func(o *Options) {
o.Transport = t o.Transport = t
// Update Client and Server // Update Client and Server
if o.Client != nil {
o.Client.Init(client.Transport(t)) o.Client.Init(client.Transport(t))
}
if o.Server != nil {
o.Server.Init(server.Transport(t)) o.Server.Init(server.Transport(t))
} }
}
} }
// Runtime sets the runtime // Runtime sets the runtime
@ -191,8 +233,10 @@ func Router(r router.Router) Option {
return func(o *Options) { return func(o *Options) {
o.Router = r o.Router = r
// Update client // Update client
if o.Client != nil {
o.Client.Init(client.Router(r)) o.Client.Init(client.Router(r))
} }
}
} }
// Convenience options // Convenience options
@ -200,57 +244,73 @@ func Router(r router.Router) Option {
// Address sets the address of the server // Address sets the address of the server
func Address(addr string) Option { func Address(addr string) Option {
return func(o *Options) { return func(o *Options) {
if o.Server != nil {
o.Server.Init(server.Address(addr)) o.Server.Init(server.Address(addr))
} }
}
} }
// Name of the service // Name of the service
func Name(n string) Option { func Name(n string) Option {
return func(o *Options) { return func(o *Options) {
if o.Server != nil {
o.Server.Init(server.Name(n)) o.Server.Init(server.Name(n))
} }
}
} }
// Version of the service // Version of the service
func Version(v string) Option { func Version(v string) Option {
return func(o *Options) { return func(o *Options) {
if o.Server != nil {
o.Server.Init(server.Version(v)) o.Server.Init(server.Version(v))
} }
}
} }
// Metadata associated with the service // Metadata associated with the service
func Metadata(md map[string]string) Option { func Metadata(md map[string]string) Option {
return func(o *Options) { return func(o *Options) {
if o.Server != nil {
o.Server.Init(server.Metadata(md)) o.Server.Init(server.Metadata(md))
} }
}
} }
// Flags that can be passed to service // Flags that can be passed to service
func Flags(flags ...cli.Flag) Option { func Flags(flags ...cli.Flag) Option {
return func(o *Options) { return func(o *Options) {
if o.Cmd != nil {
o.Cmd.App().Flags = append(o.Cmd.App().Flags, flags...) o.Cmd.App().Flags = append(o.Cmd.App().Flags, flags...)
} }
}
} }
// Action can be used to parse user provided cli options // Action can be used to parse user provided cli options
func Action(a func(*cli.Context) error) Option { func Action(a func(*cli.Context) error) Option {
return func(o *Options) { return func(o *Options) {
if o.Cmd != nil {
o.Cmd.App().Action = a o.Cmd.App().Action = a
} }
}
} }
// RegisterTTL specifies the TTL to use when registering the service // RegisterTTL specifies the TTL to use when registering the service
func RegisterTTL(t time.Duration) Option { func RegisterTTL(t time.Duration) Option {
return func(o *Options) { return func(o *Options) {
if o.Server != nil {
o.Server.Init(server.RegisterTTL(t)) o.Server.Init(server.RegisterTTL(t))
} }
}
} }
// RegisterInterval specifies the interval on which to re-register // RegisterInterval specifies the interval on which to re-register
func RegisterInterval(t time.Duration) Option { func RegisterInterval(t time.Duration) Option {
return func(o *Options) { return func(o *Options) {
if o.Server != nil {
o.Server.Init(server.RegisterInterval(t)) o.Server.Init(server.RegisterInterval(t))
} }
}
} }
// WrapClient is a convenience method for wrapping a Client with // WrapClient is a convenience method for wrapping a Client with

View File

@ -3,6 +3,7 @@ package proxy
import ( import (
"github.com/unistack-org/micro/v3/client" "github.com/unistack-org/micro/v3/client"
"github.com/unistack-org/micro/v3/logger"
"github.com/unistack-org/micro/v3/router" "github.com/unistack-org/micro/v3/router"
) )
@ -16,6 +17,8 @@ type Options struct {
Router router.Router Router router.Router
// Extra links for different clients // Extra links for different clients
Links map[string]client.Client Links map[string]client.Client
// Logger
Logger logger.Logger
} }
// Option func signature // 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 // WithLink sets a link for outbound requests
func WithLink(name string, c client.Client) Option { func WithLink(name string, c client.Client) Option {
return func(o *Options) { return func(o *Options) {

View File

@ -4,6 +4,8 @@ import (
"context" "context"
"crypto/tls" "crypto/tls"
"time" "time"
"github.com/unistack-org/micro/v3/logger"
) )
type Options struct { type Options struct {
@ -11,6 +13,7 @@ type Options struct {
Timeout time.Duration Timeout time.Duration
Secure bool Secure bool
TLSConfig *tls.Config TLSConfig *tls.Config
Logger logger.Logger
// Other options for implementations of the interface // Other options for implementations of the interface
// can be stored in a context // can be stored in a context
Context context.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 // Specify TLS Config
func TLSConfig(t *tls.Config) Option { func TLSConfig(t *tls.Config) Option {
return func(o *Options) { return func(o *Options) {

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/unistack-org/micro/v3/logger"
"github.com/unistack-org/micro/v3/registry" "github.com/unistack-org/micro/v3/registry"
) )
@ -19,10 +20,12 @@ type Options struct {
Network string Network string
// Registry is the local registry // Registry is the local registry
Registry registry.Registry Registry registry.Registry
// Context for additional options
Context context.Context
// Precache routes // Precache routes
Precache bool Precache bool
// Logger
Logger logger.Logger
// Context for additional options
Context context.Context
} }
// Id sets Router Id // 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 // Registry sets the local registry
func Registry(r registry.Registry) Option { func Registry(r registry.Registry) Option {
return func(o *Options) { return func(o *Options) {

View File

@ -5,6 +5,7 @@ import (
"io" "io"
"github.com/unistack-org/micro/v3/client" "github.com/unistack-org/micro/v3/client"
"github.com/unistack-org/micro/v3/logger"
) )
type Option func(o *Options) type Option func(o *Options)
@ -21,6 +22,15 @@ type Options struct {
Image string Image string
// Client to use when making requests // Client to use when making requests
Client client.Client 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 // WithSource sets the base image / repository

View File

@ -10,6 +10,7 @@ import (
"github.com/unistack-org/micro/v3/broker" "github.com/unistack-org/micro/v3/broker"
"github.com/unistack-org/micro/v3/codec" "github.com/unistack-org/micro/v3/codec"
"github.com/unistack-org/micro/v3/debug/trace" "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/registry"
"github.com/unistack-org/micro/v3/transport" "github.com/unistack-org/micro/v3/transport"
) )
@ -20,6 +21,7 @@ type Options struct {
Registry registry.Registry Registry registry.Registry
Tracer trace.Tracer Tracer trace.Tracer
Auth auth.Auth Auth auth.Auth
Logger logger.Logger
Transport transport.Transport Transport transport.Transport
Metadata map[string]string Metadata map[string]string
Name 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 // Unique server id
func Id(id string) Option { func Id(id string) Option {
return func(o *Options) { return func(o *Options) {

View File

@ -3,6 +3,8 @@ package store
import ( import (
"context" "context"
"time" "time"
"github.com/unistack-org/micro/v3/logger"
) )
// Options contains configuration for the Store // Options contains configuration for the Store
@ -15,6 +17,8 @@ type Options struct {
Database string Database string
// Table is analag for a table in database backends or a key prefix in KV backends // Table is analag for a table in database backends or a key prefix in KV backends
Table string Table string
// Logger
Logger logger.Logger
// Context should contain all implementation specific options, using context.WithValue. // Context should contain all implementation specific options, using context.WithValue.
Context context.Context Context context.Context
} }
@ -22,6 +26,13 @@ type Options struct {
// Option sets values in Options // Option sets values in Options
type Option func(o *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. // Nodes contains the addresses or other connection information of the backing storage.
// For example, an etcd implementation would contain the nodes of the cluster. // For example, an etcd implementation would contain the nodes of the cluster.
// A SQL implementation could contain one or more connection strings. // A SQL implementation could contain one or more connection strings.

View File

@ -2,8 +2,36 @@ package sync
import ( import (
"time" "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 // Nodes sets the addresses to use
func Nodes(a ...string) Option { func Nodes(a ...string) Option {
return func(o *Options) { return func(o *Options) {

View File

@ -3,7 +3,6 @@ package sync
import ( import (
"errors" "errors"
"time"
) )
var ( var (
@ -33,21 +32,3 @@ type Leader interface {
// status returns when leadership is lost // status returns when leadership is lost
Status() chan bool 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)

View File

@ -6,6 +6,7 @@ import (
"time" "time"
"github.com/unistack-org/micro/v3/codec" "github.com/unistack-org/micro/v3/codec"
"github.com/unistack-org/micro/v3/logger"
) )
type Options struct { type Options struct {
@ -23,6 +24,8 @@ type Options struct {
TLSConfig *tls.Config TLSConfig *tls.Config
// Timeout sets the timeout for Send/Recv // Timeout sets the timeout for Send/Recv
Timeout time.Duration Timeout time.Duration
// Logger
Logger logger.Logger
// Other options for implementations of the interface // Other options for implementations of the interface
// can be stored in a context // can be stored in a context
Context context.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 // Codec sets the codec used for encoding where the transport
// does not support message headers // does not support message headers
func Codec(c codec.Marshaler) Option { func Codec(c codec.Marshaler) Option {

View File

@ -4,6 +4,7 @@ import (
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/unistack-org/micro/v3/logger"
"github.com/unistack-org/micro/v3/transport" "github.com/unistack-org/micro/v3/transport"
) )
@ -28,6 +29,8 @@ type Options struct {
Token string Token string
// Transport listens to incoming connections // Transport listens to incoming connections
Transport transport.Transport Transport transport.Transport
// Logger
Logger logger.Logger
} }
type DialOption func(*DialOptions) 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 // The tunnel address
func Address(a string) Option { func Address(a string) Option {
return func(o *Options) { return func(o *Options) {