Skip to content

Instantly share code, notes, and snippets.

@nbutton23
Created August 7, 2020 18:04
Show Gist options
  • Save nbutton23/ae687726f9b66199ba78e4b939c5bd58 to your computer and use it in GitHub Desktop.
Save nbutton23/ae687726f9b66199ba78e4b939c5bd58 to your computer and use it in GitHub Desktop.
logging.go
// Current using the released interface
// LeveledLogger interface implements the basic methods that a logger library needs
type LeveledLogger interface {
Error(string, ...interface{})
Info(string, ...interface{})
Debug(string, ...interface{})
Warn(string, ...interface{})
}
// Info leveled log message
func (l *LeveledZerologger) Info(f string, args ...interface{}) {
l.Logger.Info().Msgf(f, args...)
}
// Using the unreleased interface that the code supports
// LeveledLogger is an interface that can be implemented by any logger or a
// logger wrapper to provide leveled logging. The methods accept a message
// string and a variadic number of key-value pairs. For log.Printf style
// formatting where message string contains a format specifier, use Logger
// interface.
type LeveledLogger interface {
Error(msg string, keysAndValues ...interface{})
Info(msg string, keysAndValues ...interface{})
Debug(msg string, keysAndValues ...interface{})
Warn(msg string, keysAndValues ...interface{})
}
// Info leveled log message
func (l *LeveledZerologger) Info(msg string, keysAndValues ...interface{}) {
logger := l.Info()
var key string
for i, value := range keysAndValues {
if i % 2 == 1 {
l = logger.Interface(key, value)
} else {
key = value.(string)
}
}
logger.Msg(msg)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment