2022-12-21 15:59:06 +03:00
|
|
|
//go:build !windows
|
2018-09-03 22:58:50 +03:00
|
|
|
// +build !windows
|
|
|
|
|
2017-08-10 17:46:39 +03:00
|
|
|
package logrus_test
|
|
|
|
|
|
|
|
import (
|
2018-09-02 21:22:06 +03:00
|
|
|
"log/syslog"
|
2017-08-10 17:46:39 +03:00
|
|
|
"os"
|
2018-09-18 20:54:23 +03:00
|
|
|
|
2022-12-21 15:59:06 +03:00
|
|
|
"git.internal/re/logrus"
|
|
|
|
slhooks "git.internal/re/logrus/hooks/syslog"
|
2017-08-10 17:46:39 +03:00
|
|
|
)
|
|
|
|
|
2018-09-04 23:15:13 +03:00
|
|
|
// An example on how to use a hook
|
2017-08-10 17:46:39 +03:00
|
|
|
func Example_hook() {
|
2022-12-21 15:59:06 +03:00
|
|
|
log := logrus.New()
|
2017-08-10 17:46:39 +03:00
|
|
|
log.Formatter = new(logrus.TextFormatter) // default
|
2018-09-18 20:54:23 +03:00
|
|
|
log.Formatter.(*logrus.TextFormatter).DisableColors = true // remove colors
|
2017-08-10 17:46:39 +03:00
|
|
|
log.Formatter.(*logrus.TextFormatter).DisableTimestamp = true // remove timestamp from test output
|
2018-11-09 15:34:45 +03:00
|
|
|
if sl, err := slhooks.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, ""); err == nil {
|
2018-09-02 21:22:06 +03:00
|
|
|
log.Hooks.Add(sl)
|
|
|
|
}
|
2017-08-10 17:46:39 +03:00
|
|
|
log.Out = os.Stdout
|
|
|
|
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"animal": "walrus",
|
|
|
|
"size": 10,
|
|
|
|
}).Info("A group of walrus emerges from the ocean")
|
|
|
|
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"omg": true,
|
|
|
|
"number": 122,
|
|
|
|
}).Warn("The group's number increased tremendously!")
|
|
|
|
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"omg": true,
|
|
|
|
"number": 100,
|
|
|
|
}).Error("The ice breaks!")
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// level=info msg="A group of walrus emerges from the ocean" animal=walrus size=10
|
|
|
|
// level=warning msg="The group's number increased tremendously!" number=122 omg=true
|
|
|
|
// level=error msg="The ice breaks!" number=100 omg=true
|
|
|
|
}
|