Created
November 28, 2020 09:38
-
-
Save greut/f86c31c0ce44d8d9d7ebc84fc44b9300 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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