1
0
Fork 0

Introduce log levels

This commit is contained in:
Gregory Eremin 2018-07-27 20:24:57 +02:00
parent 41967e03c6
commit 5bf8bb3a36
1 changed files with 55 additions and 14 deletions

View File

@ -6,64 +6,105 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
// Logger is a Logrus logger used by the package.
var Logger *logrus.Logger
// F is a short alias for a string to interface map. // F is a short alias for a string to interface map.
type F map[string]interface{} type F map[string]interface{}
func init() { // Level defines logging level.
type Level byte
const (
// LevelDebug is a logging level of debug messages.
LevelDebug Level = iota
// LevelInfo is a logging level of info messages.
LevelInfo
// LevelWarn is a logging level of warning messages.
LevelWarn
// LevelError is a logging level of error messages.
LevelError
// LevelFatal is a logging level of fatal messages.
LevelFatal
)
var (
// Logger is a Logrus logger used by the package.
Logger = logrus.New() Logger = logrus.New()
level = LevelDebug
)
// SetLevel sets up minimum logging level.
func SetLevel(l Level) {
level = l
} }
// Debug prints a debug message with given fields attached. // Debug prints a debug message with given fields attached.
func Debug(ctx context.Context, msg string, fields ...F) { func Debug(ctx context.Context, msg string, fields ...F) {
withFields(mergeFields(ctx, fields)).Debug(msg) if level >= LevelDebug {
withFields(mergeFields(ctx, fields)).Debug(msg)
}
} }
// Debugf prints a formatted debug message. // Debugf prints a formatted debug message.
func Debugf(ctx context.Context, format string, args ...interface{}) { func Debugf(ctx context.Context, format string, args ...interface{}) {
withFields(contextFields(ctx)).Debugf(format, args...) if level >= LevelDebug {
withFields(contextFields(ctx)).Debugf(format, args...)
}
} }
// Info prints an info message with given fields attached. // Info prints an info message with given fields attached.
func Info(ctx context.Context, msg string, fields ...F) { func Info(ctx context.Context, msg string, fields ...F) {
withFields(mergeFields(ctx, fields)).Info(msg) if level >= LevelInfo {
withFields(mergeFields(ctx, fields)).Info(msg)
}
} }
// Infof prints a formatted info message. // Infof prints a formatted info message.
func Infof(ctx context.Context, format string, args ...interface{}) { func Infof(ctx context.Context, format string, args ...interface{}) {
withFields(contextFields(ctx)).Infof(format, args...) if level >= LevelInfo {
withFields(contextFields(ctx)).Infof(format, args...)
}
} }
// Warn prints an warning message with given fields attached. // Warn prints an warning message with given fields attached.
func Warn(ctx context.Context, msg string, fields ...F) { func Warn(ctx context.Context, msg string, fields ...F) {
withFields(mergeFields(ctx, fields)).Warn(msg) if level >= LevelWarn {
withFields(mergeFields(ctx, fields)).Warn(msg)
}
} }
// Warnf prints a formatted warning message. // Warnf prints a formatted warning message.
func Warnf(ctx context.Context, format string, args ...interface{}) { func Warnf(ctx context.Context, format string, args ...interface{}) {
withFields(contextFields(ctx)).Warnf(format, args...) if level >= LevelWarn {
withFields(contextFields(ctx)).Warnf(format, args...)
}
} }
// Error prints an error message with given fields attached. // Error prints an error message with given fields attached.
func Error(ctx context.Context, msg string, fields ...F) { func Error(ctx context.Context, msg string, fields ...F) {
withFields(mergeFields(ctx, fields)).Error(msg) if level >= LevelError {
withFields(mergeFields(ctx, fields)).Error(msg)
}
} }
// Errorf prints a formatted error message. // Errorf prints a formatted error message.
func Errorf(ctx context.Context, format string, args ...interface{}) { func Errorf(ctx context.Context, format string, args ...interface{}) {
withFields(contextFields(ctx)).Errorf(format, args...) if level >= LevelError {
withFields(contextFields(ctx)).Errorf(format, args...)
}
} }
// Fatal prints an error message with given fields attached and then exits. // Fatal prints an error message with given fields attached and then exits.
func Fatal(ctx context.Context, msg string, fields ...F) { func Fatal(ctx context.Context, msg string, fields ...F) {
withFields(mergeFields(ctx, fields)).Fatal(msg) if level >= LevelFatal {
withFields(mergeFields(ctx, fields)).Fatal(msg)
}
} }
// Fatalf prints a formatted error message and then exits. // Fatalf prints a formatted error message and then exits.
func Fatalf(ctx context.Context, format string, args ...interface{}) { func Fatalf(ctx context.Context, format string, args ...interface{}) {
withFields(contextFields(ctx)).Fatalf(format, args...) if level >= LevelFatal {
withFields(contextFields(ctx)).Fatalf(format, args...)
}
} }
func mergeFields(ctx context.Context, fields []F) F { func mergeFields(ctx context.Context, fields []F) F {