// Creates a discarding logger and installs the test hook. func NewNullLogger() (*logrus.Logger, *Hook) { logger := logrus.New() logger.Out = ioutil.Discard return logger, NewLocal(logger) }
func TestLocalhostAddAndPrint(t *testing.T) { log := logrus.New() hook, err := NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "") if err != nil { t.Errorf("Unable to connect to local syslog.") } log.Hooks.Add(hook) for _, level := range hook.Levels() { if len(log.Hooks[level]) != 1 { t.Errorf("SyslogHook was not added. The length of log.Hooks[%v]: %v", level, len(log.Hooks[level])) } } log.Info("Congratulations!") }
package main import ( "github.com/devopstaku/logrus" ) var log = logrus.New() func init() { log.Formatter = new(logrus.JSONFormatter) log.Formatter = new(logrus.TextFormatter) // default log.Level = logrus.DebugLevel } func main() { defer func() { err := recover() if err != nil { log.WithFields(logrus.Fields{ "omg": true, "err": err, "number": 100, }).Fatal("The ice breaks!") } }() log.WithFields(logrus.Fields{ "animal": "walrus", "number": 8, }).Debug("Started observing beach")