Introduce log levels
This commit is contained in:
parent
41967e03c6
commit
5bf8bb3a36
49
log/log.go
49
log/log.go
|
@ -6,65 +6,106 @@ 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) {
|
||||||
|
if level >= LevelDebug {
|
||||||
withFields(mergeFields(ctx, fields)).Debug(msg)
|
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{}) {
|
||||||
|
if level >= LevelDebug {
|
||||||
withFields(contextFields(ctx)).Debugf(format, args...)
|
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) {
|
||||||
|
if level >= LevelInfo {
|
||||||
withFields(mergeFields(ctx, fields)).Info(msg)
|
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{}) {
|
||||||
|
if level >= LevelInfo {
|
||||||
withFields(contextFields(ctx)).Infof(format, args...)
|
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) {
|
||||||
|
if level >= LevelWarn {
|
||||||
withFields(mergeFields(ctx, fields)).Warn(msg)
|
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{}) {
|
||||||
|
if level >= LevelWarn {
|
||||||
withFields(contextFields(ctx)).Warnf(format, args...)
|
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) {
|
||||||
|
if level >= LevelError {
|
||||||
withFields(mergeFields(ctx, fields)).Error(msg)
|
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{}) {
|
||||||
|
if level >= LevelError {
|
||||||
withFields(contextFields(ctx)).Errorf(format, args...)
|
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) {
|
||||||
|
if level >= LevelFatal {
|
||||||
withFields(mergeFields(ctx, fields)).Fatal(msg)
|
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{}) {
|
||||||
|
if level >= LevelFatal {
|
||||||
withFields(contextFields(ctx)).Fatalf(format, args...)
|
withFields(contextFields(ctx)).Fatalf(format, args...)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func mergeFields(ctx context.Context, fields []F) F {
|
func mergeFields(ctx context.Context, fields []F) F {
|
||||||
ctxf := contextFields(ctx)
|
ctxf := contextFields(ctx)
|
||||||
|
|
Loading…
Reference in New Issue