2019-04-21 17:58:15 +00:00
|
|
|
// +build !windows
|
|
|
|
|
2018-01-28 14:56:30 +00:00
|
|
|
package logrus_test
|
|
|
|
|
|
|
|
import (
|
2019-04-21 17:58:15 +00:00
|
|
|
"log/syslog"
|
2018-01-28 14:56:30 +00:00
|
|
|
"os"
|
2019-04-21 17:58:15 +00:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
slhooks "github.com/sirupsen/logrus/hooks/syslog"
|
2018-01-28 14:56:30 +00:00
|
|
|
)
|
|
|
|
|
2019-04-21 17:58:15 +00:00
|
|
|
// An example on how to use a hook
|
2018-01-28 14:56:30 +00:00
|
|
|
func Example_hook() {
|
|
|
|
var log = logrus.New()
|
|
|
|
log.Formatter = new(logrus.TextFormatter) // default
|
2019-04-21 17:58:15 +00:00
|
|
|
log.Formatter.(*logrus.TextFormatter).DisableColors = true // remove colors
|
2018-01-28 14:56:30 +00:00
|
|
|
log.Formatter.(*logrus.TextFormatter).DisableTimestamp = true // remove timestamp from test output
|
2019-04-21 17:58:15 +00:00
|
|
|
if sl, err := slhooks.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, ""); err == nil {
|
|
|
|
log.Hooks.Add(sl)
|
|
|
|
}
|
2018-01-28 14:56:30 +00: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
|
|
|
|
}
|