status-go/logutils/core_test.go

62 lines
1.7 KiB
Go
Raw Normal View History

package logutils
import (
"bytes"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func TestCore(t *testing.T) {
level := zap.NewAtomicLevelAt(zap.DebugLevel)
buffer1 := bytes.NewBuffer(nil)
buffer2 := bytes.NewBuffer(nil)
core := NewCore(
zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()),
zapcore.AddSync(buffer1),
level,
)
parent := zap.New(core)
child := parent.Named("child")
childWithContext := child.With(zap.String("key1", "value1"))
childWithMoreContext := childWithContext.With(zap.String("key2", "value2"))
grandChild := childWithMoreContext.Named("grandChild")
parent.Debug("Status")
child.Debug("Super")
childWithContext.Debug("App")
childWithMoreContext.Debug("The")
grandChild.Debug("Best")
core.UpdateSyncer(zapcore.AddSync(buffer2))
core.UpdateEncoder(zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()))
parent.Debug("Status")
child.Debug("Super")
childWithContext.Debug("App")
childWithMoreContext.Debug("The")
grandChild.Debug("Best")
fmt.Println(buffer1.String())
fmt.Println(buffer2.String())
// Ensure that the first buffer has the console encoder output
buffer1Lines := strings.Split(buffer1.String(), "\n")
require.Len(t, buffer1Lines, 5+1)
require.Regexp(t, `\s+child\s+`, buffer1Lines[1])
require.Regexp(t, `\s+child\.grandChild\s+`, buffer1Lines[4])
// Ensure that the second buffer has the JSON encoder output
buffer2Lines := strings.Split(buffer2.String(), "\n")
require.Len(t, buffer2Lines, 5+1)
require.Regexp(t, `"logger"\s*:\s*"child"`, buffer2Lines[1])
require.Regexp(t, `"logger"\s*:\s*"child\.grandChild"`, buffer2Lines[4])
}