github.com/anycable/anycable-go@v1.5.1/logger/values_test.go (about)

     1  package logger
     2  
     3  import (
     4  	"bytes"
     5  	"log/slog"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestCompactValue_string(t *testing.T) {
    13  	shortvalue := "log-no-long"
    14  	assert.Equal(t, shortvalue, CompactValue(shortvalue).LogValue().String())
    15  
    16  	longvalue := strings.Repeat("log-long", 50)
    17  
    18  	truncated := CompactValue(longvalue).LogValue().String()
    19  	assert.Equal(t, "log-longlog-l", truncated[:13])
    20  	assert.Len(t, truncated, maxValueLength+8)
    21  }
    22  
    23  func TestCompactValue_bytes(t *testing.T) {
    24  	shortvalue := []byte("log-no-long")
    25  	assert.Equal(t, "log-no-long", CompactValue(shortvalue).LogValue().String())
    26  
    27  	longvalue := []byte(strings.Repeat("log-long", 50))
    28  
    29  	truncated := CompactValue(longvalue).LogValue().String()
    30  	assert.Equal(t, "log-longlog-l", truncated[:13])
    31  	assert.Len(t, truncated, maxValueLength+8)
    32  }
    33  
    34  func TestCompactValues_string(t *testing.T) {
    35  	values := []string{
    36  		"log-no-long",
    37  		strings.Repeat("log-long", 50),
    38  	}
    39  
    40  	compacts := CompactValues(values)
    41  
    42  	assert.Equal(t, "log-no-long", compacts[0].LogValue().String())
    43  
    44  	truncated := compacts[1].LogValue().String()
    45  	assert.Equal(t, "log-longlog-l", truncated[:13])
    46  	assert.Len(t, truncated, maxValueLength+8)
    47  }
    48  
    49  func TestCompactValues_bytes(t *testing.T) {
    50  	values := [][]byte{
    51  		[]byte("log-no-long"),
    52  		[]byte(strings.Repeat("log-long", 50)),
    53  	}
    54  
    55  	any := slog.Any("t", CompactValues(values)).String()
    56  	assert.Contains(t, any, "log-no-long")
    57  
    58  	compacts := CompactValues(values)
    59  
    60  	assert.Equal(t, "log-no-long", compacts[0].LogValue().String())
    61  
    62  	truncated := compacts[1].LogValue().String()
    63  	assert.Equal(t, "log-longlog-l", truncated[:13])
    64  	assert.Len(t, truncated, maxValueLength+8)
    65  }
    66  
    67  func TestCompactAny(t *testing.T) {
    68  	value := struct{ val string }{strings.Repeat("log-long", 50)}
    69  	compact := CompactAny(value)
    70  
    71  	logValue := compact.LogValue()
    72  	str := logValue.String()
    73  
    74  	assert.Equal(t, "{val:log-long", str[:13])
    75  	assert.Len(t, str, maxValueLength+8)
    76  }
    77  
    78  func TestWithJSONHandler(t *testing.T) {
    79  	buf := bytes.Buffer{}
    80  	logger := slog.New(slog.NewJSONHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
    81  
    82  	str := "short"
    83  	longstr := strings.Repeat("long", 500)
    84  
    85  	logger.Debug("test", "b", CompactValue([]byte(str)), "l", CompactValues([]string{str, longstr}))
    86  	logged := buf.String()
    87  
    88  	assert.Contains(t, logged, str)
    89  	assert.Less(t, len(logged), 300)
    90  }