gemnasium / logrus-graylog-hook

Graylog hook for logrus
MIT License
87 stars 63 forks source link

Level logic #31

Closed rafaeljusto closed 6 years ago

rafaeljusto commented 6 years ago

Hey guys! When defining a level in GraylogHook.Level attribute it will be included also all lower levels. So if I define DebugLevel, it will also include InfoLevel, WarnLevel, ErrorLevel, FatalLevel and PanicLevel. But what if I just want to fire the hook when it's DebugLevel, InfoLevel or WarnLevel?

I think we would need more flexibility from the logic defined here.

gravis commented 6 years ago

Hi, you can just set a new Level for the hook:

hook := NewGraylogHook("<graylog_ip>:<graylog_port>", map[string]interface{}{})
hook.Level = logrus.ErrorLevel

and the hook won't log anything below Error. That's why this field is public actually. Does that make sense to you?

rafaeljusto commented 6 years ago

Hi @gravis , the problem is that I would like to trigger the hook for everything that is bellow a specific log level (higher number in the logrus.Level enum). So if I set WarnLevel, I would like to trigger the graylog hook when the levels are WarnLevel, InfoLevel and DebugLevel.

gravis commented 6 years ago

Still not sure I understand :) When you set hook.Level to WarnLevel, it will already log everything for WarnLevel and bellow, that's exactly how loggers work in logrus. So it will catch everything below that level, with https://github.com/gemnasium/logrus-graylog-hook/blob/master/graylog_hook.go#L231

If you want a specific behaviour, like logging for just WarnLevel and ErrorLevel for example, you can create your own hook by wrapping the existing one and replace the Levels func. What do you think?

rafaeljusto commented 6 years ago

When you set hook.Level to WarnLevel, it will already log everything for WarnLevel and bellow, that's exactly how loggers work in logrus. So it will catch everything below that level, with https://github.com/gemnasium/logrus-graylog-hook/blob/master/graylog_hook.go#L231

When you say WarnLevel and bellow, you mean:

// A constant exposing all logging levels
var AllLevels = []Level{
    PanicLevel,  <-- this one
    FatalLevel,  <-- this one
    ErrorLevel,  <-- this one
    WarnLevel,   <-- this one
    InfoLevel,
    DebugLevel,
}

when actually I would like:

// A constant exposing all logging levels
var AllLevels = []Level{
    PanicLevel,
    FatalLevel,
    ErrorLevel,
    WarnLevel,  <-- this one
    InfoLevel,  <-- this one
    DebugLevel, <-- this one
}

And for this behaviour, the condition:

if level <= hook.Level {
    levels = append(levels, level)
}

needs to be:

if level >= hook.Level {
    levels = append(levels, level)
}

If you want a specific behaviour, like logging for just WarnLevel and ErrorLevel for example, you can create your own hook by wrapping the existing one and replace the Levels func. What do you think?

Yeah, I think wrapping will solve my problem! Thanks! 😄

gravis commented 6 years ago

That makes sense, sorry I wasn't getting your point :) Glad you can use wrapping!