status-go/logutils/zap_adapter_test.go

73 lines
2.0 KiB
Go
Raw Permalink Normal View History

package logutils
import (
"bytes"
"errors"
2020-01-02 09:10:19 +00:00
"testing"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
2020-01-02 09:10:19 +00:00
"github.com/ethereum/go-ethereum/log"
)
func TestNewZapAdapter(t *testing.T) {
buf := bytes.NewBuffer(nil)
logger := log.New()
handler := log.StreamHandler(buf, log.LogfmtFormat())
logger.SetHandler(handler)
cfg := zap.NewDevelopmentConfig()
adapter := NewZapAdapter(logger, cfg.Level)
zapLogger := zap.New(adapter)
buf.Reset()
zapLogger.
With(zap.Error(errors.New("some error"))).
Error("some message with error level")
require.Contains(t, buf.String(), `lvl=eror msg="some message with error level" error="some error`)
buf.Reset()
zapLogger.
With(zap.Int("counter", 100)).
Info("some message with param", zap.String("another-field", "another-value"))
require.Contains(t, buf.String(), `lvl=info msg="some message with param" counter=100 another-field=another-value`)
buf.Reset()
zapLogger.
With(zap.Namespace("some-namespace")).
With(zap.String("site", "SomeSite")).
Info("some message with param")
require.Contains(t, buf.String(), `lvl=info msg="some message with param" namespace=some-namespace site=SomeSite`)
}
2019-07-17 22:25:42 +00:00
func TestNewZapLoggerWithAdapter(t *testing.T) {
buf := bytes.NewBuffer(nil)
logger := log.New()
handler := log.StreamHandler(buf, log.LogfmtFormat())
logger.SetHandler(handler)
zapLogger, err := NewZapLoggerWithAdapter(logger)
require.NoError(t, err)
buf.Reset()
zapLogger.
With(zap.Error(errors.New("some error"))).
Error("some message with error level")
require.Contains(t, buf.String(), `lvl=eror msg="some message with error level" error="some error`)
}
2023-11-09 06:35:59 +00:00
func TestZapLoggerTerminalFormat(t *testing.T) {
buf := bytes.NewBuffer(nil)
logger := log.New()
handler := log.StreamHandler(buf, log.TerminalFormat(false))
logger.SetHandler(handler)
zapLogger, err := NewZapLoggerWithAdapter(logger)
require.NoError(t, err)
zapLogger.Info("some message with error level")
require.Contains(t, buf.String(), `logutils/zap_adapter_test.go:70`)
}