percipia/eslgo

Allow setting a custom logger

winsock opened this issue · 6 comments

Context

There are some debug log messages printed out by the library. As brought up in a side discussion of #5 we should allow setting a customer logger to allow users of the library to decide how to save/print these messages rather than printing to stdout using GoLang's default logging interface.

Ideas

  • Add in a new options struct for containing these kind of optional settings
  • Keep the original outbound and inbound connection functions signatures the same but add new ones that will take the struct and make the old ones call the new one internally.
  • All internal library logging can be disabled by passing nil for a logger
  • Until Golang 2 add a new interface Logger with the following definition since it seems to be what most third party libraries use
type Logger interface {
	Debug(format string, args ...interface{})
	Info(format string, args ...interface{})
	Warn(format string, args ...interface{})
	Error(format string, args ...interface{})
}

@egorsmkv I have a first draft implementation that would allow you to set your own logger. What do you think of it?
https://github.com/percipia/eslgo/tree/options

Hello, Andrew!

It seems it works correctly, thanks for this feature!

Now I create the connect like the following:

type EslLogger struct {
	logger *log.Logger
}

func (l EslLogger) Debug(format string, args ...interface{}) {
	l.logger.Debugf(format, args)
}
func (l EslLogger) Info(format string, args ...interface{}) {
	l.logger.Infof(format, args)
}
func (l EslLogger) Warn(format string, args ...interface{}) {
	l.logger.Warnf(format, args)
}
func (l EslLogger) Error(format string, args ...interface{}) {
	l.logger.Errorf(format, args)
}

func doPoolCall(log *log.Logger, eslData EslData, data OriginatePoolData) error {
	addr := fmt.Sprintf("%s:%d", eslData.Host, eslData.Port)
	conn, err := eslgo.InboundOptions{
		Options:  eslgo.Options{Logger: EslLogger{logger: log}},
		Address:  addr,
		Network:  "tcp",
		Password: eslData.Password,
		OnDisconnect: func() {
			log.Println("Inbound Connection Disconnected")
			return
		},
	}.Dial()
// ...

I'm glad to hear that! Some more feature and cleanup work will happen before this makes it into the production branch but when it does it will be v1.4.0 and I'll be sure to update this issue when it's released too!

@egorsmkv The final revision is now opened as PR #8 It should be reviewed and merged into production soon. Let me know if you have any issues with the changes. But the overall main change was to provide default options that can be modified/used and remove Address from the options and back into the method signature.

It seems that all is okay, I have no issues right now. Thanks for your work!

No problem!