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"
"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
}

View File

@ -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) {

View File

@ -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,8 +189,10 @@ func Transport(t transport.Transport) Option {
// Registry sets the routers registry
func Registry(r registry.Registry) Option {
return func(o *Options) {
if o.Router != nil {
o.Router.Init(router.Registry(r))
}
}
}
// Router is used to lookup routes for a service

View File

@ -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
}
}

View File

@ -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{

View File

@ -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
@ -53,6 +55,18 @@ func newOptions(opts ...Option) Options {
opt := Options{
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,10 +82,14 @@ type Option func(*Options)
func Broker(b broker.Broker) Option {
return func(o *Options) {
o.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))
}
}
}
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
// and the underlying components
func Registry(r registry.Registry) Option {
return func(o *Options) {
o.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) {
if o.Server != nil {
//todo client trace
o.Server.Init(server.Tracer(t))
}
}
}
// Auth sets the auth for the service
func Auth(a auth.Auth) Option {
return func(o *Options) {
o.Auth = a
if o.Server != nil {
o.Server.Init(server.Auth(a))
}
}
}
// 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
func Selector(s selector.Selector) Option {
return func(o *Options) {
if o.Client != nil {
o.Client.Init(client.Selector(s))
}
}
}
// Transport sets the transport for the service
@ -174,9 +212,13 @@ func Transport(t transport.Transport) Option {
return func(o *Options) {
o.Transport = t
// Update Client and Server
if o.Client != nil {
o.Client.Init(client.Transport(t))
}
if o.Server != nil {
o.Server.Init(server.Transport(t))
}
}
}
// Runtime sets the runtime
@ -191,8 +233,10 @@ func Router(r router.Router) Option {
return func(o *Options) {
o.Router = r
// Update client
if o.Client != nil {
o.Client.Init(client.Router(r))
}
}
}
// Convenience options
@ -200,57 +244,73 @@ func Router(r router.Router) Option {
// Address sets the address of the server
func Address(addr string) Option {
return func(o *Options) {
if o.Server != nil {
o.Server.Init(server.Address(addr))
}
}
}
// Name of the service
func Name(n string) Option {
return func(o *Options) {
if o.Server != nil {
o.Server.Init(server.Name(n))
}
}
}
// Version of the service
func Version(v string) Option {
return func(o *Options) {
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) {
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) {
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) {
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) {
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) {
if o.Server != nil {
o.Server.Init(server.RegisterInterval(t))
}
}
}
// WrapClient is a convenience method for wrapping a Client with

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

@ -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

View File

@ -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) {

View File

@ -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.

View File

@ -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) {

View File

@ -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)

View File

@ -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 {

View File

@ -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) {