...

Source file src/github.com/sirupsen/logrus/hooks/writer/writer_test.go

Documentation: github.com/sirupsen/logrus/hooks/writer

     1  package writer
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"testing"
     7  
     8  	log "github.com/sirupsen/logrus"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestDifferentLevelsGoToDifferentWriters(t *testing.T) {
    13  	var a, b bytes.Buffer
    14  
    15  	log.SetFormatter(&log.TextFormatter{
    16  		DisableTimestamp: true,
    17  		DisableColors:    true,
    18  	})
    19  	log.SetOutput(ioutil.Discard) // Send all logs to nowhere by default
    20  
    21  	log.AddHook(&Hook{
    22  		Writer: &a,
    23  		LogLevels: []log.Level{
    24  			log.WarnLevel,
    25  		},
    26  	})
    27  	log.AddHook(&Hook{ // Send info and debug logs to stdout
    28  		Writer: &b,
    29  		LogLevels: []log.Level{
    30  			log.InfoLevel,
    31  		},
    32  	})
    33  	log.Warn("send to a")
    34  	log.Info("send to b")
    35  
    36  	assert.Equal(t, a.String(), "level=warning msg=\"send to a\"\n")
    37  	assert.Equal(t, b.String(), "level=info msg=\"send to b\"\n")
    38  }
    39  

View as plain text