From 04471c6918f85a680d3a8b7a875e49d51ff3889e Mon Sep 17 00:00:00 2001 From: Renan DelValle Date: Tue, 10 Sep 2019 15:19:45 -0700 Subject: [PATCH] Adding trace logging. --- logger.go | 32 ++++++++++++++++++++++++++------ realis.go | 33 +++++++++++++++++++++------------ retry.go | 2 +- 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/logger.go b/logger.go index 54a901f..34e62ee 100644 --- a/logger.go +++ b/logger.go @@ -31,29 +31,49 @@ func (NoopLogger) Println(a ...interface{}) {} type LevelLogger struct { Logger debug bool + trace bool } func (l *LevelLogger) EnableDebug(enable bool) { l.debug = enable } +func (l *LevelLogger) EnableTrace(enable bool) { + l.trace = enable +} + func (l LevelLogger) DebugPrintf(format string, a ...interface{}) { if l.debug { - l.Print("[DEBUG] ") - l.Printf(format, a...) + l.Printf("[DEBUG] "+format, a...) } } func (l LevelLogger) DebugPrint(a ...interface{}) { if l.debug { - l.Print("[DEBUG] ") - l.Print(a...) + l.Print(append([]interface{}{"[DEBUG] "}, a...)...) } } func (l LevelLogger) DebugPrintln(a ...interface{}) { if l.debug { - l.Print("[DEBUG] ") - l.Println(a...) + l.Println(append([]interface{}{"[DEBUG] "}, a...)...) + } +} + +func (l LevelLogger) TracePrintf(format string, a ...interface{}) { + if l.trace { + l.Printf("[TRACE] "+format, a...) + } +} + +func (l LevelLogger) TracePrint(a ...interface{}) { + if l.trace { + l.Print(append([]interface{}{"[TRACE] "}, a...)...) + } +} + +func (l LevelLogger) TracePrintln(a ...interface{}) { + if l.trace { + l.Println(append([]interface{}{"[TRACE] "}, a...)...) } } diff --git a/realis.go b/realis.go index 7602fde..d1bd112 100644 --- a/realis.go +++ b/realis.go @@ -65,6 +65,7 @@ type clientConfig struct { clientKey, clientCert string options []ClientOption debug bool + trace bool zkOptions []ZKOpt } @@ -163,7 +164,7 @@ func ZookeeperOptions(opts ...ZKOpt) ClientOption { // Using the word set to avoid name collision with Interface. func SetLogger(l Logger) ClientOption { return func(config *clientConfig) { - config.logger = &LevelLogger{l, false} + config.logger = &LevelLogger{Logger: l} } } @@ -174,6 +175,13 @@ func Debug() ClientOption { } } +// Enable trace statements. +func Trace() ClientOption { + return func(config *clientConfig) { + config.trace = true + } +} + func newTJSONTransport(url string, timeout time.Duration, config *clientConfig) (thrift.TTransport, error) { trans, err := defaultTTransport(url, timeout, config) if err != nil { @@ -209,7 +217,7 @@ func NewClient(options ...ClientOption) (*Client, error) { // Default configs config.timeout = 10 * time.Second config.backoff = defaultBackoff - config.logger = &LevelLogger{log.New(os.Stdout, "realis: ", log.Ltime|log.Ldate|log.LUTC), false} + config.logger = &LevelLogger{Logger: log.New(os.Stdout, "realis: ", log.Ltime|log.Ldate|log.LUTC)} // Save options to recreate client if a connection error happens config.options = options @@ -221,18 +229,18 @@ func NewClient(options ...ClientOption) (*Client, error) { // TODO(rdelvalle): Move this logic to it's own function to make initialization code easier to read. - // Turn off all logging (including debug) + // Set a sane logger based upon configuration passed by the user if config.logger == nil { - config.logger = &LevelLogger{NoopLogger{}, false} - } - - // Set a logger if debug has been set to true but no logger has been set - if config.logger == nil && config.debug { - config.logger = &LevelLogger{log.New(os.Stdout, "realis: ", log.Ltime|log.Ldate|log.LUTC), true} + if config.debug || config.trace { + config.logger = &LevelLogger{Logger: log.New(os.Stdout, "realis: ", log.Ltime|log.Ldate|log.LUTC)} + } else { + config.logger = &LevelLogger{Logger: NoopLogger{}} + } } // Note, by this point, a LevelLogger should have been created. config.logger.EnableDebug(config.debug) + config.logger.EnableTrace(config.trace) config.logger.DebugPrintln("Number of options applied to clientConfig: ", len(options)) @@ -302,9 +310,10 @@ func NewClient(options ...ClientOption) (*Client, error) { client: aurora.NewAuroraSchedulerManagerClientFactory(config.transport, config.protoFactory), readonlyClient: aurora.NewReadOnlySchedulerClientFactory(config.transport, config.protoFactory), adminClient: aurora.NewAuroraAdminClientFactory(config.transport, config.protoFactory), - logger: LevelLogger{config.logger, config.debug}, - lock: &sync.Mutex{}, - transport: config.transport, + // We initialize logger this way to allow any logger which satisfies the Logger interface + logger: LevelLogger{Logger: config.logger, debug: config.debug, trace: config.trace}, + lock: &sync.Mutex{}, + transport: config.transport, }, nil } diff --git a/retry.go b/retry.go index 1b20dae..508df41 100644 --- a/retry.go +++ b/retry.go @@ -151,7 +151,7 @@ func (c *Client) thriftCallWithRetries(thriftCall auroraThriftCall) (*aurora.Res resp, clientErr = thriftCall() - c.logger.DebugPrintf("Aurora Thrift Call ended resp: %v clientErr: %v\n", resp, clientErr) + c.logger.TracePrintf("Aurora Thrift Call ended resp: %v clientErr: %v\n", resp, clientErr) }() // Check if our thrift call is returning an error. This is a retriable event as we don't know