gocql icon indicating copy to clipboard operation
gocql copied to clipboard

A more generic logging interface

Open Xe opened this issue 8 years ago • 0 comments

Hey all,

In Go logging is not simple. Every team and project ends up having their own idea and theory on what logs are and the purpose they serve.

I propose that instead of the current logging interface (which is really low level and wraps a subset of stdlib log.Logger) gocql should use a logging interface like this:

type BetterLogger interface {
  // some logging libraries want the underlying error instance
  Error(ctx context.Context, err error, args ...interface{})
  Errotf(ctx context.Context, err error, format string, args ...interface{})
  Info(ctx context.Context, args ...interface{})
  Infof(ctx context.Context, format string, args ...interface{})
}

This better encapsulates the idea of what log information would actually be presented. It also leaves implementation for something other than the standard library as a trivial effort.

Ex impl for stdlib log.Logger:

type stdLogger struct{}

func (s stdLogger) Error(ctx context.Context, err error, args ...interface{}) {
  result := fmt.Sprintf("error: %v", err)
  args = append([]interface{}{result}, args)
  log.Println(args)
}

func (s stdLogger) Errorf(ctx context.Context, err error, format string, args ...interface{}) {
  args = append([]interface{}{error}, args)
  log.Printf(format, args)
}

func (s stdLogger) Info(ctx context.Context, args ...interface{}) {
  log.Println(args)
}

func (s stdLogger) Infof(ctx context.Context, format string, args ...interface{}) {
  log.Printf(format, args)
}

Xe avatar May 02 '17 18:05 Xe