diff --git a/pkg/log/in_memory.go b/pkg/log/in_memory.go index 48cbbd7..e6fb439 100644 --- a/pkg/log/in_memory.go +++ b/pkg/log/in_memory.go @@ -248,7 +248,7 @@ func (l *InMemoryLogger) JSON(s []byte) { } func (l *InMemoryLogger) ProcessLogger() ProcessLogger { - return l + return newInMemoryProcessLogger(l, l.parent.ProcessLogger()) } func (l *InMemoryLogger) SilentLogger() *SilentLogger { @@ -264,18 +264,6 @@ func (l *InMemoryLogger) Write(s []byte) (int, error) { return l.parent.Write(s) } -func (l *InMemoryLogger) ProcessStart(name string) { - l.writeEntityFormatted("Start process: %s", name) -} - -func (l *InMemoryLogger) ProcessFail() { - l.writeEntityWithPrefix(l.errorPrefix, "Fail process") -} - -func (l *InMemoryLogger) ProcessEnd() { - l.writeEntity("End process") -} - func (l *InMemoryLogger) match(m *Match, entity string) bool { if len(m.Regex) > 0 { for _, regex := range m.Regex { @@ -336,3 +324,30 @@ func (l *InMemoryLogger) writeEntityWithPrefix(prefix, f string, a ...any) { l.writeEntity(msg) } + +type inMemoryProcessLogger struct { + parent ProcessLogger + inMemory *InMemoryLogger +} + +func newInMemoryProcessLogger(inMemory *InMemoryLogger, parent ProcessLogger) *inMemoryProcessLogger { + return &inMemoryProcessLogger{ + parent: parent, + inMemory: inMemory, + } +} + +func (l *inMemoryProcessLogger) ProcessStart(name string) { + l.parent.ProcessStart(name) + l.inMemory.writeEntityFormatted("Start process: %s", name) +} + +func (l *inMemoryProcessLogger) ProcessFail() { + l.parent.ProcessFail() + l.inMemory.writeEntityWithPrefix(l.inMemory.errorPrefix, "Fail process") +} + +func (l *inMemoryProcessLogger) ProcessEnd() { + l.parent.ProcessEnd() + l.inMemory.writeEntity("End process") +} diff --git a/pkg/log/in_memory_test.go b/pkg/log/in_memory_test.go index 9786125..7be2f39 100644 --- a/pkg/log/in_memory_test.go +++ b/pkg/log/in_memory_test.go @@ -14,7 +14,11 @@ package log -import "testing" +import ( + "testing" + + "github.com/stretchr/testify/require" +) func TestInMemoryLoggerFollowInterfaces(t *testing.T) { t.Run("Default constructor", func(t *testing.T) { @@ -25,3 +29,48 @@ func TestInMemoryLoggerFollowInterfaces(t *testing.T) { assertFollowAllInterfaces(t, NewInMemoryLoggerWithParent(NewDummyLogger(true))) }) } + +func TestInMemoryLoggerProcessLogger(t *testing.T) { + assertAll := func(t *testing.T, logger *InMemoryLogger, expected []string) { + for _, prefix := range expected { + matches, err := logger.AllMatches(&Match{ + Prefix: []string{prefix}, + }) + + require.NoError(t, err) + require.Len(t, matches, 1, "should only have one match for %s", prefix) + require.Equal(t, prefix, matches[0]) + } + } + + t.Run("Success", func(t *testing.T) { + logger := NewInMemoryLoggerWithParent(NewPrettyLogger(LoggerOptions{IsDebug: true})) + processLogger := logger.ProcessLogger() + + processLogger.ProcessStart("My process") + logger.InfoF("Do something") + processLogger.ProcessEnd() + + assertAll(t, logger, []string{ + "Start process: My process", + "Do something\n", + "End process", + }) + }) + + t.Run("Failed", func(t *testing.T) { + logger := NewInMemoryLoggerWithParent(NewPrettyLogger(LoggerOptions{IsDebug: true})). + WithErrorPrefix("Error") + processLogger := logger.ProcessLogger() + + processLogger.ProcessStart("My failed process") + logger.WarnF("Error!") + processLogger.ProcessFail() + + assertAll(t, logger, []string{ + "Start process: My failed process", + "Error!\n", + "Error: Fail process", + }) + }) +}