From e3d4fc5a8eb43a9d18ab7c31cd53157ec19d9bb3 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Wed, 1 Jan 2025 13:17:51 +0100 Subject: [PATCH] Include timestamp offset in test message logger --- go/toolkit/testutils.go | 23 ++++++++++++++++++++++- tasks/misc/test-message-logger-ts.md | 6 ------ 2 files changed, 22 insertions(+), 7 deletions(-) delete mode 100644 tasks/misc/test-message-logger-ts.md diff --git a/go/toolkit/testutils.go b/go/toolkit/testutils.go index 8efac13..bb70980 100644 --- a/go/toolkit/testutils.go +++ b/go/toolkit/testutils.go @@ -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(), }) } diff --git a/tasks/misc/test-message-logger-ts.md b/tasks/misc/test-message-logger-ts.md deleted file mode 100644 index 369e5d3..0000000 --- a/tasks/misc/test-message-logger-ts.md +++ /dev/null @@ -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.