Adding TracePrinf, TracePrintln. Inlined library level prefixes.

This commit is contained in:
Renan DelValle 2019-01-10 12:45:50 -08:00
parent af488ad7c8
commit 9750b37a64
No known key found for this signature in database
GPG key ID: C240AD6D6F443EC9
2 changed files with 28 additions and 20 deletions

View file

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

View file

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