Skip to content

Instantly share code, notes, and snippets.

@greut
Created November 28, 2020 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greut/f86c31c0ce44d8d9d7ebc84fc44b9300 to your computer and use it in GitHub Desktop.
Save greut/f86c31c0ce44d8d9d7ebc84fc44b9300 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/hashicorp/go-retryablehttp"
)
type LeveledLogrus struct {
*logrus.Logger
}
func (l *LeveledLogrus) fields(keysAndValues ...interface{}) map[string]interface{} {
fields := make(map[string]interface{})
for i := 0; i < len(keysAndValues); i += 2 {
fields[keysAndValues[i].(string)] = keysAndValues[i+1]
}
return fields
}
func (l *LeveledLogrus) Error(msg string, keysAndValues ...interface{}) {
l.Logger.WithFields(l.fields(keysAndValues...)).Error(msg)
}
func (l *LeveledLogrus) Info(msg string, keysAndValues ...interface{}) {
l.Logger.WithFields(l.fields(keysAndValues...)).Info(msg)
}
func (l *LeveledLogrus) Debug(msg string, keysAndValues ...interface{}) {
l.Logger.WithFields(l.fields(keysAndValues...)).Debug(msg)
}
func (l *LeveledLogrus) Warn(msg string, keysAndValues ...interface{}) {
l.Logger.WithFields(l.fields(keysAndValues...)).Warn(msg)
}
func main() {
log := logrus.New()
logger := retryablehttp.LeveledLogger(&LeveledLogrus{log})
logger.Info("hello", "name", "world", "age", 2020)
fmt.Printf("%T\n", logger)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment