From 9750b37a64bf67d99dbd18b49f5cac4015994d90 Mon Sep 17 00:00:00 2001 From: Renan DelValle Date: Thu, 10 Jan 2019 12:45:50 -0800 Subject: [PATCH] Adding TracePrinf, TracePrintln. Inlined library level prefixes. --- logger.go | 36 ++++++++++++++++++++++++------------ realis.go | 12 ++++-------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/logger.go b/logger.go index 29829b3..73fc8a9 100644 --- a/logger.go +++ b/logger.go @@ -38,30 +38,42 @@ func (l *LevelLogger) EnableDebug(enable bool) { l.debug = enable } -func (l LevelLogger) DebugPrintf(format string, a ...interface{}) { - if l.debug { - l.Print("[DEBUG] ") - l.Printf(format, a...) - } +func (l *LevelLogger) EnableTrace(enable bool) { + l.trace = enable } -func (l LevelLogger) TracePrintf(format string, a ...interface{}) { +func (l LevelLogger) DebugPrintf(format string, a ...interface{}) { if l.debug { - l.Print("[TRACE] ") - 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 b5bd391..34d3c1a 100644 --- a/realis.go +++ b/realis.go @@ -217,7 +217,7 @@ func ZookeeperOptions(opts ...ZKOpt) ClientOption { // Using the word set to avoid name collision with Interface. func SetLogger(l Logger) ClientOption { return func(config *RealisConfig) { - config.logger = &LevelLogger{l, false, false} + config.logger = &LevelLogger{Logger: l} } } @@ -270,11 +270,7 @@ func NewRealisClient(options ...ClientOption) (Realis, error) { // Default configs config.timeoutms = 10000 config.backoff = defaultBackoff - config.logger = &LevelLogger{ - Logger: log.New(os.Stdout, "realis: ", log.Ltime|log.Ldate|log.LUTC), - debug: false, - trace: 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 @@ -288,7 +284,7 @@ func NewRealisClient(options ...ClientOption) (Realis, error) { // Turn off all logging (including debug) if config.logger == nil { - config.logger = &LevelLogger{Logger: NoopLogger{}, debug: false, trace: false} + config.logger = &LevelLogger{Logger: NoopLogger{}} } // Set a logger if debug has been set to true but no logger has been set @@ -296,7 +292,6 @@ func NewRealisClient(options ...ClientOption) (Realis, error) { config.logger = &LevelLogger{ Logger: log.New(os.Stdout, "realis: ", log.Ltime|log.Ldate|log.LUTC), debug: true, - trace: false, } } @@ -305,6 +300,7 @@ func NewRealisClient(options ...ClientOption) (Realis, error) { // 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 config: ", len(options))