From 36b11f9643965731ae29871f459a580d924ce8c2 Mon Sep 17 00:00:00 2001 From: Flavio Fernandes Date: Mon, 7 Aug 2023 14:37:05 +0000 Subject: [PATCH] client.go: allow transaction logs to use a specific logger Provide an option to logger to explictly direct transactions logs. When not specified, client.logger remains as the used logger. Reported-at: https://issues.redhat.com/browse/SDN-3507 Signed-off-by: Flavio Fernandes --- client/client.go | 28 ++++++++++++++++++++++++---- client/options.go | 10 ++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/client/client.go b/client/client.go index b71da2a8..fbf4b74a 100644 --- a/client/client.go +++ b/client/client.go @@ -107,7 +107,8 @@ type ovsdbClient struct { trafficSeen chan struct{} - logger *logr.Logger + logger *logr.Logger + txnLogger *logr.Logger } // database is everything needed to map between go types and an ovsdb Database @@ -181,6 +182,25 @@ func newOVSDBClient(clientDBModel model.ClientDBModel, opts ...Option) (*ovsdbCl ) ovs.logger = &l } + + // Specific logger for transactions. Set verbosity explicitly. + if ovs.options.txnLogger == nil { + if ovs.options.logger == nil { + // create a new logger to log to stdout + l := stdr.NewWithOptions(log.New(os.Stderr, "", log.LstdFlags), stdr.Options{LogCaller: stdr.All}).WithName("libovsdb").WithValues( + "database", ovs.primaryDBName, + ).V(4) + ovs.txnLogger = &l + } else { + l := ovs.options.logger.WithValues( + "database", ovs.primaryDBName, + ).V(4) + ovs.txnLogger = &l + } + } else { + l := ovs.options.txnLogger.V(4) + ovs.txnLogger = &l + } ovs.metrics.init(clientDBModel.Name(), ovs.options.metricNamespace, ovs.options.metricSubsystem) ovs.registerMetrics() @@ -822,9 +842,9 @@ func (o *ovsdbClient) transact(ctx context.Context, dbName string, skipChWrite b if o.rpcClient == nil { return nil, ErrNotConnected } - dbgLogger := o.logger.WithValues("database", dbName).V(4) - if dbgLogger.Enabled() { - dbgLogger.Info("transacting operations", "operations", fmt.Sprintf("%+v", operation)) + // Use dedicated logger for providing transactions. + if o.txnLogger.Enabled() { + o.txnLogger.Info(fmt.Sprintf("%+v", operation)) } err := o.rpcClient.CallWithContext(ctx, "transact", args, &reply) if err != nil { diff --git a/client/options.go b/client/options.go index 81ccffe2..a0ea2a18 100644 --- a/client/options.go +++ b/client/options.go @@ -24,6 +24,7 @@ type options struct { timeout time.Duration backoff backoff.BackOff logger *logr.Logger + txnLogger *logr.Logger registry prometheus.Registerer shouldRegisterMetrics bool // in case metrics are changed after-the-fact metricNamespace string // prometheus metric namespace @@ -137,6 +138,15 @@ func WithLogger(l *logr.Logger) Option { } } +// WithTransactionLogger allows setting a specific log sink for transactions. +// Otherwise, the default go log package is used. +func WithTransactionLogger(l *logr.Logger) Option { + return func(o *options) error { + o.txnLogger = l + return nil + } +} + // WithMetricsRegistry allows the user to specify a Prometheus metrics registry. // If supplied, the metrics as defined in metrics.go will be registered. func WithMetricsRegistry(r prometheus.Registerer) Option {