예제 #1
0
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!")
}
예제 #2
0
파일: main.go 프로젝트: omidnikta/logrus
func main() {
	log.SetLevel(log.DebugLevel)
	//	formatter := &log.TextFormatter{DisableColors: true}
	formatter := &log.JSONFormatter{}
	f, err := os.OpenFile("caller.txt", os.O_CREATE|os.O_WRONLY, os.ModePerm)
	if err != nil {
		fmt.Println(err)
	}
	defer f.Close()
	log.SetOutput(f)

	logg := log.StandardLogger()
	logg.Level = log.DebugLevel
	logg.Formatter = formatter
	logg.ShowCaller(true)
	logger := log.New()
	logger.ShowCaller(true)
	logger.Level = log.DebugLevel
	logger.Formatter = formatter
	check(logg)
	check(logger)

	log.StandardLogger().ShowCaller(true)
	log.SetFormatter(formatter)
	log.Debug(Caller())
	log.Debugf(Caller())
	log.Debugln(Caller())
	log.WithError(nil).Debug(Caller())
	log.WithError(nil).Debugf(Caller())
	log.WithError(nil).Debugln(Caller())
	log.WithField("caller", 1).Debug(Caller())
	log.WithField("first", 1).Debugf(Caller())
	log.WithField("first", 1).Debugln(Caller())
	log.WithFields(log.Fields{"first": 1, "second": 2}).Debug(Caller())
	log.WithFields(log.Fields{"first": 1, "second": 2}).Debugf(Caller())
	log.WithFields(log.Fields{"first": 1, "second": 2}).Debugln(Caller())
	log.Info(Caller())
	log.Infof(Caller())
	log.Infoln(Caller())
	log.WithError(nil).Info(Caller())
	log.WithError(nil).Infof(Caller())
	log.WithError(nil).Infoln(Caller())
	log.WithField("first", 1).Info(Caller())
	log.WithField("first", 1).Infof(Caller())
	log.WithField("first", 1).Infoln(Caller())
	log.WithFields(log.Fields{"first": 1, "second": 2}).Info(Caller())
	log.WithFields(log.Fields{"first": 1, "second": 2}).Infof(Caller())
	log.WithFields(log.Fields{"first": 1, "second": 2}).Infoln(Caller())
	log.Warn(Caller())
	log.Warnf(Caller())
	log.Warnln(Caller())
	log.WithError(nil).Warn(Caller())
	log.WithError(nil).Warnf(Caller())
	log.WithError(nil).Warnln(Caller())
	log.WithField("first", 1).Warn(Caller())
	log.WithField("first", 1).Warnf(Caller())
	log.WithField("first", 1).Warnln(Caller())
	log.WithFields(log.Fields{"first": 1, "second": 2}).Warn(Caller())
	log.WithFields(log.Fields{"first": 1, "second": 2}).Warnf(Caller())
	log.WithFields(log.Fields{"first": 1, "second": 2}).Warnln(Caller())
	log.Error(Caller())
	log.Errorf(Caller())
	log.Errorln(Caller())
	log.WithError(nil).Error(Caller())
	log.WithError(nil).Errorf(Caller())
	log.WithError(nil).Errorln(Caller())
	log.WithField("first", 1).Error(Caller())
	log.WithField("first", 1).Errorf(Caller())
	log.WithField("first", 1).Errorln(Caller())
	log.WithFields(log.Fields{"first": 1, "second": 2}).Error(Caller())
	log.WithFields(log.Fields{"first": 1, "second": 2}).Errorf(Caller())
	log.WithFields(log.Fields{"first": 1, "second": 2}).Errorln(Caller())

	log.Print(Caller())
	log.Println(Caller())
	log.Printf(Caller())

	contextLogger := log.WithFields(log.Fields{
		"common": "this is a common field",
		"other":  "I also should be logged always",
	})

	contextLogger.Info(Caller())
	contextLogger.Info(Caller())

}
예제 #3
0
파일: basic.go 프로젝트: omidnikta/logrus
package main

import (
	"github.com/omidnikta/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")