mirror of https://github.com/sirupsen/logrus.git
feat: new methods to check enabled log level
Adding 6 methods on 'exported', 'logger' and 'entry': - IsDebugEnabled() bool - IsInfoEnabled() bool - IsWarnEnabled() bool - IsErrorEnabled() bool - IsFatalEnabled() bool - IsPanicEnabled() bool Replace duplicated 'if logger.level() >= XxxxLevel' by a call to the new methods in 'logger' and 'entry' Closes #761
This commit is contained in:
parent
ea8897e799
commit
4225d694ba
60
entry.go
60
entry.go
|
@ -139,7 +139,7 @@ func (entry *Entry) write() {
|
|||
}
|
||||
|
||||
func (entry *Entry) Debug(args ...interface{}) {
|
||||
if entry.Logger.level() >= DebugLevel {
|
||||
if entry.IsDebugEnabled() {
|
||||
entry.log(DebugLevel, fmt.Sprint(args...))
|
||||
}
|
||||
}
|
||||
|
@ -149,13 +149,13 @@ func (entry *Entry) Print(args ...interface{}) {
|
|||
}
|
||||
|
||||
func (entry *Entry) Info(args ...interface{}) {
|
||||
if entry.Logger.level() >= InfoLevel {
|
||||
if entry.IsInfoEnabled() {
|
||||
entry.log(InfoLevel, fmt.Sprint(args...))
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Warn(args ...interface{}) {
|
||||
if entry.Logger.level() >= WarnLevel {
|
||||
if entry.IsWarnEnabled() {
|
||||
entry.log(WarnLevel, fmt.Sprint(args...))
|
||||
}
|
||||
}
|
||||
|
@ -165,20 +165,20 @@ func (entry *Entry) Warning(args ...interface{}) {
|
|||
}
|
||||
|
||||
func (entry *Entry) Error(args ...interface{}) {
|
||||
if entry.Logger.level() >= ErrorLevel {
|
||||
if entry.IsErrorEnabled() {
|
||||
entry.log(ErrorLevel, fmt.Sprint(args...))
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Fatal(args ...interface{}) {
|
||||
if entry.Logger.level() >= FatalLevel {
|
||||
if entry.IsFatalEnabled() {
|
||||
entry.log(FatalLevel, fmt.Sprint(args...))
|
||||
}
|
||||
Exit(1)
|
||||
}
|
||||
|
||||
func (entry *Entry) Panic(args ...interface{}) {
|
||||
if entry.Logger.level() >= PanicLevel {
|
||||
if entry.IsPanicEnabled() {
|
||||
entry.log(PanicLevel, fmt.Sprint(args...))
|
||||
}
|
||||
panic(fmt.Sprint(args...))
|
||||
|
@ -187,13 +187,13 @@ func (entry *Entry) Panic(args ...interface{}) {
|
|||
// Entry Printf family functions
|
||||
|
||||
func (entry *Entry) Debugf(format string, args ...interface{}) {
|
||||
if entry.Logger.level() >= DebugLevel {
|
||||
if entry.IsDebugEnabled() {
|
||||
entry.Debug(fmt.Sprintf(format, args...))
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Infof(format string, args ...interface{}) {
|
||||
if entry.Logger.level() >= InfoLevel {
|
||||
if entry.IsInfoEnabled() {
|
||||
entry.Info(fmt.Sprintf(format, args...))
|
||||
}
|
||||
}
|
||||
|
@ -203,7 +203,7 @@ func (entry *Entry) Printf(format string, args ...interface{}) {
|
|||
}
|
||||
|
||||
func (entry *Entry) Warnf(format string, args ...interface{}) {
|
||||
if entry.Logger.level() >= WarnLevel {
|
||||
if entry.IsWarnEnabled() {
|
||||
entry.Warn(fmt.Sprintf(format, args...))
|
||||
}
|
||||
}
|
||||
|
@ -213,20 +213,20 @@ func (entry *Entry) Warningf(format string, args ...interface{}) {
|
|||
}
|
||||
|
||||
func (entry *Entry) Errorf(format string, args ...interface{}) {
|
||||
if entry.Logger.level() >= ErrorLevel {
|
||||
if entry.IsErrorEnabled() {
|
||||
entry.Error(fmt.Sprintf(format, args...))
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Fatalf(format string, args ...interface{}) {
|
||||
if entry.Logger.level() >= FatalLevel {
|
||||
if entry.IsFatalEnabled() {
|
||||
entry.Fatal(fmt.Sprintf(format, args...))
|
||||
}
|
||||
Exit(1)
|
||||
}
|
||||
|
||||
func (entry *Entry) Panicf(format string, args ...interface{}) {
|
||||
if entry.Logger.level() >= PanicLevel {
|
||||
if entry.IsPanicEnabled() {
|
||||
entry.Panic(fmt.Sprintf(format, args...))
|
||||
}
|
||||
}
|
||||
|
@ -234,13 +234,13 @@ func (entry *Entry) Panicf(format string, args ...interface{}) {
|
|||
// Entry Println family functions
|
||||
|
||||
func (entry *Entry) Debugln(args ...interface{}) {
|
||||
if entry.Logger.level() >= DebugLevel {
|
||||
if entry.IsDebugEnabled() {
|
||||
entry.Debug(entry.sprintlnn(args...))
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Infoln(args ...interface{}) {
|
||||
if entry.Logger.level() >= InfoLevel {
|
||||
if entry.IsInfoEnabled() {
|
||||
entry.Info(entry.sprintlnn(args...))
|
||||
}
|
||||
}
|
||||
|
@ -250,7 +250,7 @@ func (entry *Entry) Println(args ...interface{}) {
|
|||
}
|
||||
|
||||
func (entry *Entry) Warnln(args ...interface{}) {
|
||||
if entry.Logger.level() >= WarnLevel {
|
||||
if entry.IsWarnEnabled() {
|
||||
entry.Warn(entry.sprintlnn(args...))
|
||||
}
|
||||
}
|
||||
|
@ -260,24 +260,48 @@ func (entry *Entry) Warningln(args ...interface{}) {
|
|||
}
|
||||
|
||||
func (entry *Entry) Errorln(args ...interface{}) {
|
||||
if entry.Logger.level() >= ErrorLevel {
|
||||
if entry.IsErrorEnabled() {
|
||||
entry.Error(entry.sprintlnn(args...))
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) Fatalln(args ...interface{}) {
|
||||
if entry.Logger.level() >= FatalLevel {
|
||||
if entry.IsFatalEnabled() {
|
||||
entry.Fatal(entry.sprintlnn(args...))
|
||||
}
|
||||
Exit(1)
|
||||
}
|
||||
|
||||
func (entry *Entry) Panicln(args ...interface{}) {
|
||||
if entry.Logger.level() >= PanicLevel {
|
||||
if entry.IsPanicEnabled() {
|
||||
entry.Panic(entry.sprintlnn(args...))
|
||||
}
|
||||
}
|
||||
|
||||
func (entry *Entry) IsDebugEnabled() bool {
|
||||
return entry.Logger.IsDebugEnabled()
|
||||
}
|
||||
|
||||
func (entry *Entry) IsInfoEnabled() bool {
|
||||
return entry.Logger.IsInfoEnabled()
|
||||
}
|
||||
|
||||
func (entry *Entry) IsWarnEnabled() bool {
|
||||
return entry.Logger.IsWarnEnabled()
|
||||
}
|
||||
|
||||
func (entry *Entry) IsErrorEnabled() bool {
|
||||
return entry.Logger.IsErrorEnabled()
|
||||
}
|
||||
|
||||
func (entry *Entry) IsFatalEnabled() bool {
|
||||
return entry.Logger.IsFatalEnabled()
|
||||
}
|
||||
|
||||
func (entry *Entry) IsPanicEnabled() bool {
|
||||
return entry.Logger.IsPanicEnabled()
|
||||
}
|
||||
|
||||
// Sprintlnn => Sprint no newline. This is to get the behavior of how
|
||||
// fmt.Sprintln where spaces are always added between operands, regardless of
|
||||
// their type. Instead of vendoring the Sprintln implementation to spare a
|
||||
|
|
24
exported.go
24
exported.go
|
@ -41,6 +41,30 @@ func GetLevel() Level {
|
|||
return std.level()
|
||||
}
|
||||
|
||||
func IsDebugEnabled() bool {
|
||||
return std.IsDebugEnabled()
|
||||
}
|
||||
|
||||
func IsInfoEnabled() bool {
|
||||
return std.IsInfoEnabled()
|
||||
}
|
||||
|
||||
func IsWarnEnabled() bool {
|
||||
return std.IsWarnEnabled()
|
||||
}
|
||||
|
||||
func IsErrorEnabled() bool {
|
||||
return std.IsErrorEnabled()
|
||||
}
|
||||
|
||||
func IsFatalEnabled() bool {
|
||||
return std.IsFatalEnabled()
|
||||
}
|
||||
|
||||
func IsPanicEnabled() bool {
|
||||
return std.IsPanicEnabled()
|
||||
}
|
||||
|
||||
// AddHook adds a hook to the standard logger hooks.
|
||||
func AddHook(hook Hook) {
|
||||
std.mu.Lock()
|
||||
|
|
66
logger.go
66
logger.go
|
@ -113,7 +113,7 @@ func (logger *Logger) WithError(err error) *Entry {
|
|||
}
|
||||
|
||||
func (logger *Logger) Debugf(format string, args ...interface{}) {
|
||||
if logger.level() >= DebugLevel {
|
||||
if logger.IsDebugEnabled() {
|
||||
entry := logger.newEntry()
|
||||
entry.Debugf(format, args...)
|
||||
logger.releaseEntry(entry)
|
||||
|
@ -121,7 +121,7 @@ func (logger *Logger) Debugf(format string, args ...interface{}) {
|
|||
}
|
||||
|
||||
func (logger *Logger) Infof(format string, args ...interface{}) {
|
||||
if logger.level() >= InfoLevel {
|
||||
if logger.IsInfoEnabled() {
|
||||
entry := logger.newEntry()
|
||||
entry.Infof(format, args...)
|
||||
logger.releaseEntry(entry)
|
||||
|
@ -135,7 +135,7 @@ func (logger *Logger) Printf(format string, args ...interface{}) {
|
|||
}
|
||||
|
||||
func (logger *Logger) Warnf(format string, args ...interface{}) {
|
||||
if logger.level() >= WarnLevel {
|
||||
if logger.IsWarnEnabled() {
|
||||
entry := logger.newEntry()
|
||||
entry.Warnf(format, args...)
|
||||
logger.releaseEntry(entry)
|
||||
|
@ -143,7 +143,7 @@ func (logger *Logger) Warnf(format string, args ...interface{}) {
|
|||
}
|
||||
|
||||
func (logger *Logger) Warningf(format string, args ...interface{}) {
|
||||
if logger.level() >= WarnLevel {
|
||||
if logger.IsWarnEnabled() {
|
||||
entry := logger.newEntry()
|
||||
entry.Warnf(format, args...)
|
||||
logger.releaseEntry(entry)
|
||||
|
@ -151,7 +151,7 @@ func (logger *Logger) Warningf(format string, args ...interface{}) {
|
|||
}
|
||||
|
||||
func (logger *Logger) Errorf(format string, args ...interface{}) {
|
||||
if logger.level() >= ErrorLevel {
|
||||
if logger.IsErrorEnabled() {
|
||||
entry := logger.newEntry()
|
||||
entry.Errorf(format, args...)
|
||||
logger.releaseEntry(entry)
|
||||
|
@ -159,7 +159,7 @@ func (logger *Logger) Errorf(format string, args ...interface{}) {
|
|||
}
|
||||
|
||||
func (logger *Logger) Fatalf(format string, args ...interface{}) {
|
||||
if logger.level() >= FatalLevel {
|
||||
if logger.IsFatalEnabled() {
|
||||
entry := logger.newEntry()
|
||||
entry.Fatalf(format, args...)
|
||||
logger.releaseEntry(entry)
|
||||
|
@ -168,7 +168,7 @@ func (logger *Logger) Fatalf(format string, args ...interface{}) {
|
|||
}
|
||||
|
||||
func (logger *Logger) Panicf(format string, args ...interface{}) {
|
||||
if logger.level() >= PanicLevel {
|
||||
if logger.IsPanicEnabled() {
|
||||
entry := logger.newEntry()
|
||||
entry.Panicf(format, args...)
|
||||
logger.releaseEntry(entry)
|
||||
|
@ -176,7 +176,7 @@ func (logger *Logger) Panicf(format string, args ...interface{}) {
|
|||
}
|
||||
|
||||
func (logger *Logger) Debug(args ...interface{}) {
|
||||
if logger.level() >= DebugLevel {
|
||||
if logger.IsDebugEnabled() {
|
||||
entry := logger.newEntry()
|
||||
entry.Debug(args...)
|
||||
logger.releaseEntry(entry)
|
||||
|
@ -184,7 +184,7 @@ func (logger *Logger) Debug(args ...interface{}) {
|
|||
}
|
||||
|
||||
func (logger *Logger) Info(args ...interface{}) {
|
||||
if logger.level() >= InfoLevel {
|
||||
if logger.IsInfoEnabled() {
|
||||
entry := logger.newEntry()
|
||||
entry.Info(args...)
|
||||
logger.releaseEntry(entry)
|
||||
|
@ -198,7 +198,7 @@ func (logger *Logger) Print(args ...interface{}) {
|
|||
}
|
||||
|
||||
func (logger *Logger) Warn(args ...interface{}) {
|
||||
if logger.level() >= WarnLevel {
|
||||
if logger.IsWarnEnabled() {
|
||||
entry := logger.newEntry()
|
||||
entry.Warn(args...)
|
||||
logger.releaseEntry(entry)
|
||||
|
@ -206,7 +206,7 @@ func (logger *Logger) Warn(args ...interface{}) {
|
|||
}
|
||||
|
||||
func (logger *Logger) Warning(args ...interface{}) {
|
||||
if logger.level() >= WarnLevel {
|
||||
if logger.IsWarnEnabled() {
|
||||
entry := logger.newEntry()
|
||||
entry.Warn(args...)
|
||||
logger.releaseEntry(entry)
|
||||
|
@ -214,7 +214,7 @@ func (logger *Logger) Warning(args ...interface{}) {
|
|||
}
|
||||
|
||||
func (logger *Logger) Error(args ...interface{}) {
|
||||
if logger.level() >= ErrorLevel {
|
||||
if logger.IsErrorEnabled() {
|
||||
entry := logger.newEntry()
|
||||
entry.Error(args...)
|
||||
logger.releaseEntry(entry)
|
||||
|
@ -222,7 +222,7 @@ func (logger *Logger) Error(args ...interface{}) {
|
|||
}
|
||||
|
||||
func (logger *Logger) Fatal(args ...interface{}) {
|
||||
if logger.level() >= FatalLevel {
|
||||
if logger.IsFatalEnabled() {
|
||||
entry := logger.newEntry()
|
||||
entry.Fatal(args...)
|
||||
logger.releaseEntry(entry)
|
||||
|
@ -231,7 +231,7 @@ func (logger *Logger) Fatal(args ...interface{}) {
|
|||
}
|
||||
|
||||
func (logger *Logger) Panic(args ...interface{}) {
|
||||
if logger.level() >= PanicLevel {
|
||||
if logger.IsPanicEnabled() {
|
||||
entry := logger.newEntry()
|
||||
entry.Panic(args...)
|
||||
logger.releaseEntry(entry)
|
||||
|
@ -239,7 +239,7 @@ func (logger *Logger) Panic(args ...interface{}) {
|
|||
}
|
||||
|
||||
func (logger *Logger) Debugln(args ...interface{}) {
|
||||
if logger.level() >= DebugLevel {
|
||||
if logger.IsDebugEnabled() {
|
||||
entry := logger.newEntry()
|
||||
entry.Debugln(args...)
|
||||
logger.releaseEntry(entry)
|
||||
|
@ -247,7 +247,7 @@ func (logger *Logger) Debugln(args ...interface{}) {
|
|||
}
|
||||
|
||||
func (logger *Logger) Infoln(args ...interface{}) {
|
||||
if logger.level() >= InfoLevel {
|
||||
if logger.IsInfoEnabled() {
|
||||
entry := logger.newEntry()
|
||||
entry.Infoln(args...)
|
||||
logger.releaseEntry(entry)
|
||||
|
@ -261,7 +261,7 @@ func (logger *Logger) Println(args ...interface{}) {
|
|||
}
|
||||
|
||||
func (logger *Logger) Warnln(args ...interface{}) {
|
||||
if logger.level() >= WarnLevel {
|
||||
if logger.IsWarnEnabled() {
|
||||
entry := logger.newEntry()
|
||||
entry.Warnln(args...)
|
||||
logger.releaseEntry(entry)
|
||||
|
@ -269,7 +269,7 @@ func (logger *Logger) Warnln(args ...interface{}) {
|
|||
}
|
||||
|
||||
func (logger *Logger) Warningln(args ...interface{}) {
|
||||
if logger.level() >= WarnLevel {
|
||||
if logger.IsWarnEnabled() {
|
||||
entry := logger.newEntry()
|
||||
entry.Warnln(args...)
|
||||
logger.releaseEntry(entry)
|
||||
|
@ -277,7 +277,7 @@ func (logger *Logger) Warningln(args ...interface{}) {
|
|||
}
|
||||
|
||||
func (logger *Logger) Errorln(args ...interface{}) {
|
||||
if logger.level() >= ErrorLevel {
|
||||
if logger.IsErrorEnabled() {
|
||||
entry := logger.newEntry()
|
||||
entry.Errorln(args...)
|
||||
logger.releaseEntry(entry)
|
||||
|
@ -285,7 +285,7 @@ func (logger *Logger) Errorln(args ...interface{}) {
|
|||
}
|
||||
|
||||
func (logger *Logger) Fatalln(args ...interface{}) {
|
||||
if logger.level() >= FatalLevel {
|
||||
if logger.IsFatalEnabled() {
|
||||
entry := logger.newEntry()
|
||||
entry.Fatalln(args...)
|
||||
logger.releaseEntry(entry)
|
||||
|
@ -294,7 +294,7 @@ func (logger *Logger) Fatalln(args ...interface{}) {
|
|||
}
|
||||
|
||||
func (logger *Logger) Panicln(args ...interface{}) {
|
||||
if logger.level() >= PanicLevel {
|
||||
if logger.IsPanicEnabled() {
|
||||
entry := logger.newEntry()
|
||||
entry.Panicln(args...)
|
||||
logger.releaseEntry(entry)
|
||||
|
@ -321,3 +321,27 @@ func (logger *Logger) AddHook(hook Hook) {
|
|||
defer logger.mu.Unlock()
|
||||
logger.Hooks.Add(hook)
|
||||
}
|
||||
|
||||
func (logger *Logger) IsDebugEnabled() bool {
|
||||
return logger.level() >= DebugLevel
|
||||
}
|
||||
|
||||
func (logger *Logger) IsInfoEnabled() bool {
|
||||
return logger.level() >= InfoLevel
|
||||
}
|
||||
|
||||
func (logger *Logger) IsWarnEnabled() bool {
|
||||
return logger.level() >= WarnLevel
|
||||
}
|
||||
|
||||
func (logger *Logger) IsErrorEnabled() bool {
|
||||
return logger.level() >= ErrorLevel
|
||||
}
|
||||
|
||||
func (logger *Logger) IsFatalEnabled() bool {
|
||||
return logger.level() >= FatalLevel
|
||||
}
|
||||
|
||||
func (logger *Logger) IsPanicEnabled() bool {
|
||||
return logger.level() >= PanicLevel
|
||||
}
|
||||
|
|
|
@ -140,4 +140,11 @@ type FieldLogger interface {
|
|||
Errorln(args ...interface{})
|
||||
Fatalln(args ...interface{})
|
||||
Panicln(args ...interface{})
|
||||
|
||||
IsDebugEnabled() bool
|
||||
IsInfoEnabled() bool
|
||||
IsWarnEnabled() bool
|
||||
IsErrorEnabled() bool
|
||||
IsFatalEnabled() bool
|
||||
IsPanicEnabled() bool
|
||||
}
|
||||
|
|
|
@ -384,3 +384,54 @@ func TestEntryWriter(t *testing.T) {
|
|||
assert.Equal(t, fields["foo"], "bar")
|
||||
assert.Equal(t, fields["level"], "warning")
|
||||
}
|
||||
|
||||
func TestLogLevelEnabled(t *testing.T) {
|
||||
log := New()
|
||||
log.SetLevel(PanicLevel)
|
||||
assert.Equal(t, true, log.IsPanicEnabled())
|
||||
assert.Equal(t, false, log.IsFatalEnabled())
|
||||
assert.Equal(t, false, log.IsErrorEnabled())
|
||||
assert.Equal(t, false, log.IsWarnEnabled())
|
||||
assert.Equal(t, false, log.IsInfoEnabled())
|
||||
assert.Equal(t, false, log.IsDebugEnabled())
|
||||
|
||||
log.SetLevel(FatalLevel)
|
||||
assert.Equal(t, true, log.IsPanicEnabled())
|
||||
assert.Equal(t, true, log.IsFatalEnabled())
|
||||
assert.Equal(t, false, log.IsErrorEnabled())
|
||||
assert.Equal(t, false, log.IsWarnEnabled())
|
||||
assert.Equal(t, false, log.IsInfoEnabled())
|
||||
assert.Equal(t, false, log.IsDebugEnabled())
|
||||
|
||||
log.SetLevel(ErrorLevel)
|
||||
assert.Equal(t, true, log.IsPanicEnabled())
|
||||
assert.Equal(t, true, log.IsFatalEnabled())
|
||||
assert.Equal(t, true, log.IsErrorEnabled())
|
||||
assert.Equal(t, false, log.IsWarnEnabled())
|
||||
assert.Equal(t, false, log.IsInfoEnabled())
|
||||
assert.Equal(t, false, log.IsDebugEnabled())
|
||||
|
||||
log.SetLevel(WarnLevel)
|
||||
assert.Equal(t, true, log.IsPanicEnabled())
|
||||
assert.Equal(t, true, log.IsFatalEnabled())
|
||||
assert.Equal(t, true, log.IsErrorEnabled())
|
||||
assert.Equal(t, true, log.IsWarnEnabled())
|
||||
assert.Equal(t, false, log.IsInfoEnabled())
|
||||
assert.Equal(t, false, log.IsDebugEnabled())
|
||||
|
||||
log.SetLevel(InfoLevel)
|
||||
assert.Equal(t, true, log.IsPanicEnabled())
|
||||
assert.Equal(t, true, log.IsFatalEnabled())
|
||||
assert.Equal(t, true, log.IsErrorEnabled())
|
||||
assert.Equal(t, true, log.IsWarnEnabled())
|
||||
assert.Equal(t, true, log.IsInfoEnabled())
|
||||
assert.Equal(t, false, log.IsDebugEnabled())
|
||||
|
||||
log.SetLevel(DebugLevel)
|
||||
assert.Equal(t, true, log.IsPanicEnabled())
|
||||
assert.Equal(t, true, log.IsFatalEnabled())
|
||||
assert.Equal(t, true, log.IsErrorEnabled())
|
||||
assert.Equal(t, true, log.IsWarnEnabled())
|
||||
assert.Equal(t, true, log.IsInfoEnabled())
|
||||
assert.Equal(t, true, log.IsDebugEnabled())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue