git.colasdn.top/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrlogrus/nrlogrus_test.go (about)

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package nrlogrus
     5  
     6  import (
     7  	"bytes"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/sirupsen/logrus"
    12  )
    13  
    14  func bufferToStringAndReset(buf *bytes.Buffer) string {
    15  	s := buf.String()
    16  	buf.Reset()
    17  	return s
    18  }
    19  
    20  func TestLogrus(t *testing.T) {
    21  	buf := &bytes.Buffer{}
    22  	l := logrus.New()
    23  	l.SetOutput(buf)
    24  	l.SetLevel(logrus.DebugLevel)
    25  	lg := Transform(l)
    26  	lg.Debug("elephant", map[string]interface{}{"color": "gray"})
    27  	s := bufferToStringAndReset(buf)
    28  	if !strings.Contains(s, "elephant") || !strings.Contains(s, "gray") {
    29  		t.Error(s)
    30  	}
    31  	if enabled := lg.DebugEnabled(); !enabled {
    32  		t.Error(enabled)
    33  	}
    34  	// Now switch the level and test that debug is no longer enabled.
    35  	l.SetLevel(logrus.InfoLevel)
    36  	lg.Debug("lion", map[string]interface{}{"color": "yellow"})
    37  	s = bufferToStringAndReset(buf)
    38  	if strings.Contains(s, "lion") || strings.Contains(s, "yellow") {
    39  		t.Error(s)
    40  	}
    41  	if enabled := lg.DebugEnabled(); enabled {
    42  		t.Error(enabled)
    43  	}
    44  	lg.Info("tiger", map[string]interface{}{"color": "orange"})
    45  	s = bufferToStringAndReset(buf)
    46  	if !strings.Contains(s, "tiger") || !strings.Contains(s, "orange") {
    47  		t.Error(s)
    48  	}
    49  }