...

Source file src/github.com/sirupsen/logrus/example_hook_test.go

Documentation: github.com/sirupsen/logrus

     1  // +build !windows
     2  
     3  package logrus_test
     4  
     5  import (
     6  	"log/syslog"
     7  	"os"
     8  
     9  	"github.com/sirupsen/logrus"
    10  	slhooks "github.com/sirupsen/logrus/hooks/syslog"
    11  )
    12  
    13  // An example on how to use a hook
    14  func Example_hook() {
    15  	var log = logrus.New()
    16  	log.Formatter = new(logrus.TextFormatter)                     // default
    17  	log.Formatter.(*logrus.TextFormatter).DisableColors = true    // remove colors
    18  	log.Formatter.(*logrus.TextFormatter).DisableTimestamp = true // remove timestamp from test output
    19  	if sl, err := slhooks.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, ""); err == nil {
    20  		log.Hooks.Add(sl)
    21  	}
    22  	log.Out = os.Stdout
    23  
    24  	log.WithFields(logrus.Fields{
    25  		"animal": "walrus",
    26  		"size":   10,
    27  	}).Info("A group of walrus emerges from the ocean")
    28  
    29  	log.WithFields(logrus.Fields{
    30  		"omg":    true,
    31  		"number": 122,
    32  	}).Warn("The group's number increased tremendously!")
    33  
    34  	log.WithFields(logrus.Fields{
    35  		"omg":    true,
    36  		"number": 100,
    37  	}).Error("The ice breaks!")
    38  
    39  	// Output:
    40  	// level=info msg="A group of walrus emerges from the ocean" animal=walrus size=10
    41  	// level=warning msg="The group's number increased tremendously!" number=122 omg=true
    42  	// level=error msg="The ice breaks!" number=100 omg=true
    43  }
    44  

View as plain text