github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/logging/logconfig/sinks_test.go (about)

     1  package logconfig
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestBuildLoggerFromSinkConfig(t *testing.T) {
    12  	sinkConfig := Sink().
    13  		AddSinks(
    14  			Sink().
    15  				AddSinks(
    16  					Sink().
    17  						AddSinks(
    18  							Sink().
    19  								SetTransform(CaptureTransform("cap", 100, true)).
    20  								SetOutput(StderrOutput()).
    21  								AddSinks(
    22  									Sink().
    23  										SetTransform(LabelTransform(true, "Label", "A Label!")).
    24  										SetOutput(StdoutOutput())))))
    25  
    26  	logger, captures, err := sinkConfig.BuildLogger()
    27  	logger.Log("Foo", "Bar")
    28  	assert.NoError(t, err)
    29  	assert.Equal(t, logLines("Foo", "Bar"),
    30  		captures["cap"].BufferLogger().FlushLogLines())
    31  }
    32  
    33  func TestFileLoggerSink(t *testing.T) {
    34  	sinkConfig := Sink().
    35  		SetOutput(FileOutput("/tmp/logmclogface")).AddSinks(
    36  		Sink().SetOutput(FileOutput("/tmp/doubleloglog")))
    37  
    38  	bs, err := json.Marshal(sinkConfig)
    39  	assert.NoError(t, err)
    40  
    41  	fmt.Println(string(bs))
    42  	_, _, err = sinkConfig.BuildLogger()
    43  	assert.NoError(t, err)
    44  }
    45  
    46  func TestFilterSinks(t *testing.T) {
    47  	sinkConfig := Sink().
    48  		SetOutput(StderrOutput()).
    49  		AddSinks(
    50  			Sink().
    51  				SetTransform(FilterTransform(IncludeWhenAnyMatches,
    52  					"Foo", "Bar",
    53  					"Rough", "Trade",
    54  				)).
    55  				AddSinks(
    56  					Sink().
    57  						SetTransform(CaptureTransform("Included", 100, true)).
    58  						AddSinks(
    59  							Sink().
    60  								SetTransform(FilterTransform(ExcludeWhenAllMatch,
    61  									"Foo", "Baz",
    62  									"Index", "00$")).
    63  								AddSinks(
    64  									Sink().
    65  										SetTransform(CaptureTransform("Excluded", 100, false)),
    66  								),
    67  						),
    68  				),
    69  		)
    70  
    71  	logger, captures, err := sinkConfig.BuildLogger()
    72  	assert.NoError(t, err, "Should be able to build filter logger")
    73  	included := captures["Included"]
    74  	excluded := captures["Excluded"]
    75  
    76  	// Included by both filters
    77  	ll := logLines("Foo", "Bar")
    78  	logger.Log(ll[0]...)
    79  	assert.Equal(t, logLines("Foo", "Bar"),
    80  		included.BufferLogger().FlushLogLines())
    81  	assert.Equal(t, logLines("Foo", "Bar"),
    82  		excluded.BufferLogger().FlushLogLines())
    83  
    84  	// Included by first filter and excluded by second
    85  	ll = logLines("Foo", "Bar", "Foo", "Baz", "Index", "1000")
    86  	logger.Log(ll[0]...)
    87  	assert.Equal(t, ll, included.BufferLogger().FlushLogLines())
    88  	assert.Equal(t, logLines(), excluded.BufferLogger().FlushLogLines())
    89  
    90  	// Included by first filter and not excluded by second despite matching one
    91  	// predicate
    92  	ll = logLines("Rough", "Trade", "Index", "1000")
    93  	logger.Log(ll[0]...)
    94  	assert.Equal(t, ll, included.BufferLogger().FlushLogLines())
    95  	assert.Equal(t, ll, excluded.BufferLogger().FlushLogLines())
    96  }
    97  
    98  func TestPruneTransform(t *testing.T) {
    99  	sinkConfig := Sink().
   100  		SetTransform(PruneTransform("Trace")).
   101  		AddSinks(Sink().
   102  			SetTransform(CaptureTransform("cap", 100, false)))
   103  
   104  	logger, captures, err := sinkConfig.BuildLogger()
   105  	assert.NoError(t, err)
   106  	logger.Log("msg", "Hello with a trace",
   107  		"Trace", []string{"logger:32, state:23"})
   108  	logger.Log("msg", "Goodbye with a trace",
   109  		"Trace", []string{"logger:32, state:14"})
   110  	assert.Equal(t, logLines("msg", "Hello with a trace", "",
   111  		"msg", "Goodbye with a trace"),
   112  		captures["cap"].FlushLogLines())
   113  }
   114  
   115  // Takes a variadic argument of log lines as a list of key value pairs delimited
   116  // by the empty string
   117  func logLines(keyvals ...string) [][]interface{} {
   118  	llines := make([][]interface{}, 0)
   119  	line := make([]interface{}, 0)
   120  	for _, kv := range keyvals {
   121  		if kv == "" {
   122  			llines = append(llines, line)
   123  			line = make([]interface{}, 0)
   124  		} else {
   125  			line = append(line, kv)
   126  		}
   127  	}
   128  	if len(line) > 0 {
   129  		llines = append(llines, line)
   130  	}
   131  	return llines
   132  }