Files
prysm/runtime/logging/logrus-prefixed-formatter/formatter_test.go
Nishant Das 5d6a406829 Update to Go 1.23 (#14818)
* Update to Go 1.23

* Update bazel version

* Update rules_go

* Use toolchains_protoc

* Update go_honnef_go_tools

* Update golang.org/x/tools

* Fix violations of SA3000

* Update errcheck by re-exporting the upstream repo

* Remove problematic ginkgo and gomega test helpers. Rewrote tests without these test libraries.

* Update go to 1.23.5

* gofmt with go1.23.5

* Revert Patch

* Unclog

* Update for go 1.23 support

* Fix Lint Issues

* Gazelle

* Fix Build

* Fix Lint

* no lint

* Fix lint

* Fix lint

* Disable intrange

* Preston's review

---------

Co-authored-by: Preston Van Loon <preston@pvl.dev>
2025-01-24 04:53:23 +00:00

106 lines
2.5 KiB
Go

package prefixed_test
import (
"fmt"
"regexp"
"testing"
"github.com/pkg/errors"
prefixed "github.com/prysmaticlabs/prysm/v5/runtime/logging/logrus-prefixed-formatter"
"github.com/prysmaticlabs/prysm/v5/testing/require"
"github.com/sirupsen/logrus"
)
type LogOutput struct {
buffer string
}
func (o *LogOutput) Write(p []byte) (int, error) {
o.buffer += string(p)
return len(p), nil
}
func (o *LogOutput) GetValue() string {
return o.buffer
}
func TestFormatter_logfmt_output(t *testing.T) {
tests := []struct {
name string
callback func(l *logrus.Logger)
expected string
}{
{
name: "should output simple message",
callback: func(l *logrus.Logger) {
l.Debug("test")
},
expected: "level=debug msg=test\n",
},
{
name: "should output message with additional field",
callback: func(l *logrus.Logger) {
l.WithFields(logrus.Fields{"animal": "walrus"}).Debug("test")
},
expected: "level=debug msg=test animal=walrus\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
output := new(LogOutput)
formatter := new(prefixed.TextFormatter)
formatter.DisableTimestamp = true
log := logrus.New()
log.Out = output
log.Formatter = formatter
log.Level = logrus.DebugLevel
tt.callback(log)
require.Equal(t, output.GetValue(), tt.expected)
})
}
}
func TestFormatter_formatted_output(t *testing.T) {
output := new(LogOutput)
formatter := new(prefixed.TextFormatter)
formatter.DisableTimestamp = true
formatter.ForceFormatting = true
log := logrus.New()
log.Out = output
log.Formatter = formatter
log.Level = logrus.DebugLevel
log.Debug("test")
require.Equal(t, output.GetValue(), "DEBUG test\n")
}
func TestFormatter_SuppressErrorStackTraces(t *testing.T) {
formatter := new(prefixed.TextFormatter)
formatter.ForceFormatting = true
log := logrus.New()
log.Formatter = formatter
output := new(LogOutput)
log.Out = output
errFn := func() error {
return errors.New("inner")
}
log.WithError(errors.Wrap(errFn(), "outer")).Error("test")
require.Equal(t, true, regexp.MustCompile(`test error=outer: inner\n\s*$`).MatchString(output.GetValue()), fmt.Sprintf("wrong log output: %s", output.GetValue()))
}
func TestFormatter_EscapesControlCharacters(t *testing.T) {
formatter := new(prefixed.TextFormatter)
formatter.ForceFormatting = true
log := logrus.New()
log.Formatter = formatter
output := new(LogOutput)
log.Out = output
log.WithField("test", "foo\nbar").Error("testing things")
require.Equal(t, "[0000] ERROR testing things test=foobar"+"\n", output.GetValue())
}