Add instructions to use different log levels for local and syslog

This commit adds instructions to the syslog readme about how to
send different log levels to local logging (`log.SetLevel`) and
syslog hook.

fixes #1369
This commit is contained in:
Tommaso Visconti 2023-02-17 10:03:12 +01:00
parent a448f8228b
commit 70234da9c3
2 changed files with 46 additions and 2 deletions

View File

@ -317,6 +317,8 @@ log.SetLevel(log.InfoLevel)
It may be useful to set `log.Level = logrus.DebugLevel` in a debug or verbose It may be useful to set `log.Level = logrus.DebugLevel` in a debug or verbose
environment if your application has that. environment if your application has that.
Note: If you want different log levels for global (`log.SetLevel(...)`) and syslog logging, please check the [syslog hook README](hooks/syslog/README.md#different-log-levels-for-local-and-remote-logging).
#### Entries #### Entries
Besides the fields added with `WithField` or `WithFields` some fields are Besides the fields added with `WithField` or `WithFields` some fields are

View File

@ -37,3 +37,45 @@ func main() {
} }
} }
``` ```
### Different log levels for local and remote logging
By default `NewSyslogHook()` sends logs through the hook for all log levels. If you want to have
different log levels between local logging and syslog logging (i.e. respect the `priority` argument
passed to `NewSyslogHook()`), you need to implement the `logrus_syslog.SyslogHook` interface
overriding `Levels()` to return only the log levels you're interested on.
The following example shows how to log at **DEBUG** level for local logging and **WARN** level for
syslog logging:
```go
package main
import (
"log/syslog"
log "github.com/sirupsen/logrus"
logrus_syslog "github.com/sirupsen/logrus/hooks/syslog"
)
type customHook struct {
*logrus_syslog.SyslogHook
}
func (h *customHook) Levels() []log.Level {
return []log.Level{log.WarnLevel}
}
func main() {
log.SetLevel(log.DebugLevel)
hook, err := logrus_syslog.NewSyslogHook("tcp", "localhost:5140", syslog.LOG_WARNING, "myTag")
if err != nil {
panic(err)
}
log.AddHook(&customHook{hook})
//...
}
```