示例#1
0
// NewCustomMiddleware builds a *Middleware with the given level and formatter
func NewCustomMiddleware(level logrus.Level, formatter logrus.Formatter, name string) *Middleware {
	log := logrus.New()
	log.Level = level
	log.Formatter = formatter

	return &Middleware{Logger: log, Name: name, logStarting: true, clock: &realClock{}}
}
示例#2
0
func main() {
	flag.BoolVar(&quiet, "quiet", false, "if set, hide messages from the logger")
	flag.Parse()
	r := http.NewServeMux()
	r.HandleFunc(`/`, func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
		fmt.Fprintf(w, "success!\n")
	})

	log := logrus.New()
	log.Level = logrus.InfoLevel

	n := negroni.New()
	n.Use(negronilogrus.NewMiddlewareFromLogger(log, "web"))
	n.UseHandler(r)

	n.Run(":9999")
}
示例#3
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!")
}
示例#4
0
func TestNewMiddlewareFromLogger_logStarting(t *testing.T) {
	mw := NewMiddlewareFromLogger(logrus.New(), "test")
	assert.True(t, mw.logStarting)
}
示例#5
0
func TestNewMiddlewareFromLogger_Name(t *testing.T) {
	mw := NewMiddlewareFromLogger(logrus.New(), "test")
	assert.Equal(t, "test", mw.Name)
}
示例#6
0
func TestNewMiddlewareFromLogger_Logger(t *testing.T) {
	l := logrus.New()
	mw := NewMiddlewareFromLogger(l, "test")
	assert.Exactly(t, l, mw.Logger)
}
示例#7
0
func TestNewMiddleware_Logger(t *testing.T) {
	l := logrus.New()
	mw := NewMiddleware()
	assert.NotEqual(t, fmt.Sprintf("%p", mw.Logger), fmt.Sprintf("%p", l))
}
示例#8
0
package main

import (
	"github.com/convox/rack/Godeps/_workspace/src/github.com/Sirupsen/logrus"
	"gopkg.in/gemnasium/logrus-airbrake-hook.v2"
)

var log = logrus.New()

func init() {
	log.Formatter = new(logrus.TextFormatter) // default
	log.Hooks.Add(airbrake.NewHook(123, "xyz", "development"))
}

func main() {
	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,
	}).Fatal("The ice breaks!")
}