Include timestamp offset in test message logger

This commit is contained in:
Brian Picciano 2025-01-01 13:17:51 +01:00
parent c0ddd24dde
commit e3d4fc5a8e
2 changed files with 22 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt"
"os"
"testing"
"time"
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
"github.com/stretchr/testify/mock"
@ -17,6 +18,20 @@ func MarkIntegrationTest(t *testing.T) {
}
}
type mlogMessageHandlerWithOffset struct {
mlog.MessageHandler
start time.Time
}
func (h *mlogMessageHandlerWithOffset) Handle(msg mlog.FullMessage) error {
msg.Description = fmt.Sprintf(
"(+%v) %s",
time.Since(h.start).Truncate(time.Millisecond),
msg.Description,
)
return h.MessageHandler.Handle(msg)
}
// NewTestLogger returns a Logger which should be used for testing purposes. The
// log level of the Logger can be adjusted using the ISLE_LOG_LEVEL envvar.
func NewTestLogger(t *testing.T) *mlog.Logger {
@ -27,8 +42,14 @@ func NewTestLogger(t *testing.T) *mlog.Logger {
}
}
handler := mlog.NewTestMessageHandler(t)
handler = &mlogMessageHandlerWithOffset{
MessageHandler: handler,
start: time.Now(),
}
return mlog.NewLogger(&mlog.LoggerOpts{
MessageHandler: mlog.NewTestMessageHandler(t),
MessageHandler: handler,
MaxLevel: level.Int(),
})
}

View File

@ -1,6 +0,0 @@
---
type: task
---
On each message which is logged the test message logger (toolkit.NewTestLogger)
should include the duration since then the test started.