github.com/amazechain/amc@v0.1.3/log/logrus-prefixed-formatter/formatter_test.go (about)

     1  package prefixed_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/amazechain/amc/log/logrus-prefixed-formatter"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  	"github.com/pkg/errors"
    10  	"github.com/sirupsen/logrus"
    11  )
    12  
    13  var _ = Describe("Formatter", func() {
    14  	var formatter *TextFormatter
    15  	var log *logrus.Logger
    16  	var output *LogOutput
    17  
    18  	BeforeEach(func() {
    19  		output = new(LogOutput)
    20  		formatter = new(TextFormatter)
    21  		log = logrus.New()
    22  		log.Out = output
    23  		log.Formatter = formatter
    24  		log.Level = logrus.DebugLevel
    25  	})
    26  
    27  	Describe("logfmt output", func() {
    28  		It("should output simple message", func() {
    29  			formatter.DisableTimestamp = true
    30  			log.Debug("test")
    31  			Ω(output.GetValue()).Should(Equal("level=debug msg=test\n"))
    32  		})
    33  
    34  		It("should output message with additional field", func() {
    35  			formatter.DisableTimestamp = true
    36  			log.WithFields(logrus.Fields{"animal": "walrus"}).Debug("test")
    37  			Ω(output.GetValue()).Should(Equal("level=debug msg=test animal=walrus\n"))
    38  		})
    39  	})
    40  
    41  	Describe("Formatted output", func() {
    42  		It("should output formatted message", func() {
    43  			formatter.DisableTimestamp = true
    44  			formatter.ForceFormatting = true
    45  			log.Debug("test")
    46  			Ω(output.GetValue()).Should(Equal("DEBUG test\n"))
    47  		})
    48  	})
    49  
    50  	Describe("Theming support", func() {
    51  
    52  	})
    53  })
    54  
    55  func TestFormatter_SuppressErrorStackTraces(t *testing.T) {
    56  	formatter := new(TextFormatter)
    57  	formatter.ForceFormatting = true
    58  	log := logrus.New()
    59  	log.Formatter = formatter
    60  	output := new(LogOutput)
    61  	log.Out = output
    62  
    63  	errFn := func() error {
    64  		return errors.New("inner")
    65  	}
    66  
    67  	log.WithError(errors.Wrap(errFn(), "outer")).Error("test")
    68  }
    69  
    70  func TestFormatter_EscapesControlCharacters(t *testing.T) {
    71  	formatter := new(TextFormatter)
    72  	formatter.ForceFormatting = true
    73  	log := logrus.New()
    74  	log.Formatter = formatter
    75  	output := new(LogOutput)
    76  	log.Out = output
    77  
    78  	log.WithField("test", "foo\nbar").Error("testing things")
    79  }