Adding trace logging.

This commit is contained in:
Renan DelValle 2019-09-10 15:19:45 -07:00 committed by Renan DelValle
parent dbad078d95
commit 04471c6918
3 changed files with 48 additions and 19 deletions

View file

@ -31,29 +31,49 @@ func (NoopLogger) Println(a ...interface{}) {}
type LevelLogger struct { type LevelLogger struct {
Logger Logger
debug bool debug bool
trace bool
} }
func (l *LevelLogger) EnableDebug(enable bool) { func (l *LevelLogger) EnableDebug(enable bool) {
l.debug = enable l.debug = enable
} }
func (l *LevelLogger) EnableTrace(enable bool) {
l.trace = enable
}
func (l LevelLogger) DebugPrintf(format string, a ...interface{}) { func (l LevelLogger) DebugPrintf(format string, a ...interface{}) {
if l.debug { if l.debug {
l.Print("[DEBUG] ") l.Printf("[DEBUG] "+format, a...)
l.Printf(format, a...)
} }
} }
func (l LevelLogger) DebugPrint(a ...interface{}) { func (l LevelLogger) DebugPrint(a ...interface{}) {
if l.debug { if l.debug {
l.Print("[DEBUG] ") l.Print(append([]interface{}{"[DEBUG] "}, a...)...)
l.Print(a...)
} }
} }
func (l LevelLogger) DebugPrintln(a ...interface{}) { func (l LevelLogger) DebugPrintln(a ...interface{}) {
if l.debug { if l.debug {
l.Print("[DEBUG] ") l.Println(append([]interface{}{"[DEBUG] "}, a...)...)
l.Println(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

@ -65,6 +65,7 @@ type clientConfig struct {
clientKey, clientCert string clientKey, clientCert string
options []ClientOption options []ClientOption
debug bool debug bool
trace bool
zkOptions []ZKOpt zkOptions []ZKOpt
} }
@ -163,7 +164,7 @@ func ZookeeperOptions(opts ...ZKOpt) ClientOption {
// Using the word set to avoid name collision with Interface. // Using the word set to avoid name collision with Interface.
func SetLogger(l Logger) ClientOption { func SetLogger(l Logger) ClientOption {
return func(config *clientConfig) { 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) { func newTJSONTransport(url string, timeout time.Duration, config *clientConfig) (thrift.TTransport, error) {
trans, err := defaultTTransport(url, timeout, config) trans, err := defaultTTransport(url, timeout, config)
if err != nil { if err != nil {
@ -209,7 +217,7 @@ func NewClient(options ...ClientOption) (*Client, error) {
// Default configs // Default configs
config.timeout = 10 * time.Second config.timeout = 10 * time.Second
config.backoff = defaultBackoff 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 // Save options to recreate client if a connection error happens
config.options = options 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. // 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 { if config.logger == nil {
config.logger = &LevelLogger{NoopLogger{}, false} 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{}}
} }
// 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}
} }
// Note, by this point, a LevelLogger should have been created. // Note, by this point, a LevelLogger should have been created.
config.logger.EnableDebug(config.debug) config.logger.EnableDebug(config.debug)
config.logger.EnableTrace(config.trace)
config.logger.DebugPrintln("Number of options applied to clientConfig: ", len(options)) config.logger.DebugPrintln("Number of options applied to clientConfig: ", len(options))
@ -302,7 +310,8 @@ func NewClient(options ...ClientOption) (*Client, error) {
client: aurora.NewAuroraSchedulerManagerClientFactory(config.transport, config.protoFactory), client: aurora.NewAuroraSchedulerManagerClientFactory(config.transport, config.protoFactory),
readonlyClient: aurora.NewReadOnlySchedulerClientFactory(config.transport, config.protoFactory), readonlyClient: aurora.NewReadOnlySchedulerClientFactory(config.transport, config.protoFactory),
adminClient: aurora.NewAuroraAdminClientFactory(config.transport, config.protoFactory), adminClient: aurora.NewAuroraAdminClientFactory(config.transport, config.protoFactory),
logger: LevelLogger{config.logger, config.debug}, // 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{}, lock: &sync.Mutex{},
transport: config.transport, transport: config.transport,
}, nil }, nil

View file

@ -151,7 +151,7 @@ func (c *Client) thriftCallWithRetries(thriftCall auroraThriftCall) (*aurora.Res
resp, clientErr = thriftCall() 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 // Check if our thrift call is returning an error. This is a retriable event as we don't know