2019-07-24 07:58:01 +00:00
|
|
|
package logutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2020-01-02 09:10:19 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-07-24 07:58:01 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2019-07-24 07:58:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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`)
|
|
|
|
}
|