Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pkg/log/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func (d *DummyLogger) InfoFWithoutLn(format string, a ...interface{}) {
fmt.Printf(format, a...)
}

// InfoLn
// Deprecated:
// Use InfoF(string) it add \n to end
func (d *DummyLogger) InfoLn(a ...interface{}) {
fmt.Println(a...)
}
Expand All @@ -78,6 +81,9 @@ func (d *DummyLogger) ErrorFWithoutLn(format string, a ...interface{}) {
fmt.Printf(format, a...)
}

// ErrorLn
// Deprecated:
// Use ErrorF(string) it add \n to end
func (d *DummyLogger) ErrorLn(a ...interface{}) {
fmt.Println(a...)
}
Expand All @@ -88,6 +94,9 @@ func (d *DummyLogger) DebugFWithoutLn(format string, a ...interface{}) {
}
}

// DebugLn
// Deprecated:
// Use DebugF(string) it add \n to end
func (d *DummyLogger) DebugLn(a ...interface{}) {
if d.isDebug {
fmt.Println(a...)
Expand All @@ -106,6 +115,9 @@ func (d *DummyLogger) FailRetry(l string) {
d.Fail(l)
}

// WarnLn
// Deprecated:
// Use WarnF(string) it add \n to end
func (d *DummyLogger) WarnLn(a ...interface{}) {
fmt.Println(a...)
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/log/in_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ func (l *InMemoryLogger) InfoFWithoutLn(format string, a ...interface{}) {
l.parent.InfoFWithoutLn(format, a...)
}

// InfoLn
// Deprecated:
// Use InfoF(string) it add \n to end
func (l *InMemoryLogger) InfoLn(a ...interface{}) {
l.writeEntityFormatted(listToString(a))
l.parent.InfoLn(a...)
Expand All @@ -182,6 +185,9 @@ func (l *InMemoryLogger) ErrorFWithoutLn(format string, a ...interface{}) {
l.parent.ErrorFWithoutLn(format, a...)
}

// ErrorLn
// Deprecated:
// Use ErrorF(string) it add \n to end
func (l *InMemoryLogger) ErrorLn(a ...interface{}) {
l.writeEntityWithPrefix(l.errorPrefix, listToString(a))
l.parent.ErrorLn(a...)
Expand All @@ -196,6 +202,9 @@ func (l *InMemoryLogger) DebugFWithoutLn(format string, a ...interface{}) {
l.parent.DebugFWithoutLn(format, a...)
}

// DebugLn
// Deprecated:
// Use DebugF(string) it add \n to end
func (l *InMemoryLogger) DebugLn(a ...interface{}) {
if l.notDebug {
return
Expand All @@ -210,6 +219,9 @@ func (l *InMemoryLogger) WarnFWithoutLn(format string, a ...interface{}) {
l.parent.WarnFWithoutLn(format, a...)
}

// WarnLn
// Deprecated:
// Use WarnF(string) it add \n to end
func (l *InMemoryLogger) WarnLn(a ...interface{}) {
l.writeEntityFormatted(listToString(a))
l.parent.WarnLn(a...)
Expand Down
5 changes: 4 additions & 1 deletion pkg/log/ln_logger_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package log

import "strings"

// formatWithNewLineLogger
// we often use *F function, but for pretty log we use "\n" in end of string
// this interface and wrapper help us for get rit of this
Expand Down Expand Up @@ -42,5 +44,6 @@ func (w *formatWithNewLineLoggerWrapper) WarnF(format string, a ...any) {
}

func addLnToFormat(format string) string {
return format + "\n"
// remove last new line to avoid add double new lines
return strings.TrimSuffix(format, "\n") + "\n"
}
94 changes: 92 additions & 2 deletions pkg/log/ln_logger_wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,80 @@ package log
import (
"errors"
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/require"
)

func TestLnLoggerWrapper(t *testing.T) {
logger := NewInMemoryLoggerWithParent(NewSimpleLogger(LoggerOptions{IsDebug: true}))
emptyStringTests := []struct {
name string
do func(w formatWithNewLineLogger)
}{
{
name: "ErrorF",
do: func(w formatWithNewLineLogger) {
w.ErrorF("")
},
},

assertAddNewLine := func(t *testing.T, msg string) {
{
name: "WarnF",
do: func(w formatWithNewLineLogger) {
w.WarnF("")
},
},

{
name: "InfoF",
do: func(w formatWithNewLineLogger) {
w.InfoF("")
},
},

{
name: "DebugF",
do: func(w formatWithNewLineLogger) {
w.DebugF("")
},
},
}

for _, test := range emptyStringTests {
t.Run(fmt.Sprintf("Log empty line for %s", test.name), func(t *testing.T) {
logger := NewInMemoryLoggerWithParent(NewPrettyLogger(LoggerOptions{IsDebug: true}))
wrapper := newFormatWithNewLineLoggerWrapper(logger)

test.do(wrapper)

matches, err := logger.AllMatches(&Match{
Prefix: []string{"\n"},
})

require.NoError(t, err)
require.Len(t, matches, 1, "should one match")
require.Equal(t, "\n", matches[0], "should produce new line")
})
}

logger := NewInMemoryLoggerWithParent(NewPrettyLogger(LoggerOptions{IsDebug: true}))

assertAddNewLine := func(t *testing.T, msg string) string {
matches, err := logger.AllMatches(&Match{
Prefix: []string{fmt.Sprintf("%s\n", msg)},
})

require.NoError(t, err)
require.Len(t, matches, 1, msg)

return matches[0]
}

assertCountNewLines := func(t *testing.T, msg string, expected int) {
match := assertAddNewLine(t, msg)
count := strings.Count(match, "\n")
require.Equal(t, expected, count, "should contain %d trailing new lines", expected)
}

wrapper := newFormatWithNewLineLoggerWrapper(logger)
Expand All @@ -42,18 +101,49 @@ func TestLnLoggerWrapper(t *testing.T) {
wrapper.ErrorF("VariablesError %s %v", "msg", true)
assertAddNewLine(t, "VariablesError msg true")

// trim one new line
wrapper.ErrorF("ErrorOneLn\n")
assertCountNewLines(t, "ErrorOneLn", 1)
// save multiple new lines expected one
wrapper.ErrorF("ErrorMultiLn\n\n\n")
assertCountNewLines(t, "ErrorMultiLn\n\n", 3)

wrapper.WarnF("Warn")
assertAddNewLine(t, "Warn")

wrapper.WarnF("VariablesWarn %s %v", "msg", true)
assertAddNewLine(t, "VariablesWarn msg true")

// trim one new line
wrapper.WarnF("WarnOneLn\n")
assertCountNewLines(t, "WarnOneLn", 1)
// save multiple new lines expected one
wrapper.WarnF("WarnMultiLn\n\n")
assertCountNewLines(t, "WarnMultiLn\n", 2)

wrapper.InfoF("Info")
assertAddNewLine(t, "Info")

wrapper.InfoF("VariablesInfo %s %v", "msg", errors.New("error"))
assertAddNewLine(t, "VariablesInfo msg error")

// trim one new line
wrapper.InfoF("InfoOneLn\n")
assertCountNewLines(t, "InfoOneLn", 1)
// save multiple new lines expected one
wrapper.InfoF("InfoMultiLn\n\n\n\n")
assertCountNewLines(t, "InfoMultiLn\n\n\n", 4)

wrapper.DebugF("Debug")
assertAddNewLine(t, "Debug")

wrapper.DebugF("VariablesDebug %v %s", 42, "msg")
assertAddNewLine(t, "VariablesDebug 42 msg")

// trim one new line
wrapper.DebugF("DebugOneLn\n")
assertCountNewLines(t, "DebugOneLn", 1)
// save multiple new lines expected one
wrapper.DebugF("DebugMultiLn\n\n\n\n\n")
assertCountNewLines(t, "DebugMultiLn\n\n\n\n", 5)
}
8 changes: 8 additions & 0 deletions pkg/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,28 @@ type baseLogger interface {
Process(Process, string, func() error) error

InfoFWithoutLn(format string, a ...interface{})

// InfoLn
// Deprecated:
// Use InfoF(string) it add \n to end
InfoLn(a ...interface{})

ErrorFWithoutLn(format string, a ...interface{})

// ErrorLn
// Deprecated:
// Use ErrorF(string) it add \n to end
ErrorLn(a ...interface{})

DebugFWithoutLn(format string, a ...interface{})

// DebugLn
// Deprecated:
// Use DebugF(string) it add \n to end
DebugLn(a ...interface{})

WarnFWithoutLn(format string, a ...interface{})

// WarnLn
// Deprecated:
// Use WarnF(string) it add \n to end
Expand All @@ -149,18 +153,22 @@ type formatWithNewLineLogger interface {
// InfoF
// Warning! InfoF add \n to end of message.
// If you do not have \n to end of message please use InfoFWithoutLn
// Also trim last new line from format
InfoF(format string, a ...any)
// ErrorF
// Warning! ErrorF add \n to end of message.
// If you do not have \n to end of message please use ErrorFWithoutLn
// Also trim last new line from format
ErrorF(format string, a ...any)
// DebugF
// Warning! DebugF add \n to end of message.
// If you do not have \n to end of message please use DebugFWithoutLn
// Also trim last new line from format
DebugF(format string, a ...any)
// WarnF
// Warning! WarnF add \n to end of message.
// If you do not have \n to end of message please use WarnFWithoutLn
// Also trim last new line from format
WarnF(format string, a ...any)
}

Expand Down
14 changes: 13 additions & 1 deletion pkg/log/pretty.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ func (d *PrettyLogger) InfoFWithoutLn(format string, a ...interface{}) {
d.logboekLogger.Info().LogF(format, a...)
}

// InfoLn
// Deprecated:
// Use InfoF(string) it add \n to end
func (d *PrettyLogger) InfoLn(a ...interface{}) {
d.logboekLogger.Info().LogLn(a...)
}
Expand All @@ -129,6 +132,9 @@ func (d *PrettyLogger) ErrorFWithoutLn(format string, a ...interface{}) {
d.logboekLogger.Error().LogF(format, a...)
}

// ErrorLn
// Deprecated:
// Use ErrorF(string) it add \n to end
func (d *PrettyLogger) ErrorLn(a ...interface{}) {
d.logboekLogger.Error().LogLn(a...)
}
Expand All @@ -147,6 +153,9 @@ func (d *PrettyLogger) DebugFWithoutLn(format string, a ...interface{}) {
}
}

// DebugLn
// Deprecated:
// Use DebugF(string) it add \n to end
func (d *PrettyLogger) DebugLn(a ...interface{}) {
if d.debugLogWriter != nil {
o := fmt.Sprintln(a...)
Expand All @@ -173,6 +182,9 @@ func (d *PrettyLogger) FailRetry(l string) {
d.Fail(l)
}

// WarnLn
// Deprecated:
// Use WarnF(string) it add \n to end
func (d *PrettyLogger) WarnLn(a ...interface{}) {
a = append([]interface{}{"❗ ~ "}, a...)
d.InfoLn(color.New(color.Bold).Sprint(a...))
Expand All @@ -184,7 +196,7 @@ func (d *PrettyLogger) WarnFWithoutLn(format string, a ...interface{}) {
}

func (d *PrettyLogger) JSON(content []byte) {
d.InfoLn(prettyJSON(content))
d.InfoF(prettyJSON(content))
}

func (d *PrettyLogger) Write(content []byte) (int, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/log/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (l *wrappedProcessLogger) ProcessStart(msg string) {

l.processes.push(p)

l.logger.InfoLn(msg)
l.logger.InfoF(msg)
}

func (l *wrappedProcessLogger) ProcessEnd() {
Expand All @@ -116,7 +116,7 @@ func (l *wrappedProcessLogger) ProcessEnd() {
msg = fmt.Sprintf("%s (%s)", p.Msg, p.formatTime())
}

l.logger.InfoLn(msg)
l.logger.InfoF(msg)
}

func (l *wrappedProcessLogger) ProcessFail() {
Expand All @@ -127,5 +127,5 @@ func (l *wrappedProcessLogger) ProcessFail() {
msg = fmt.Sprintf("%s FAILED (%s)", p.Msg, p.formatTime())
}

l.logger.ErrorLn(msg)
l.logger.ErrorF(msg)
}
33 changes: 33 additions & 0 deletions pkg/log/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,37 @@ func TestProcessLoggers(t *testing.T) {
})
})
}

t.Run("Add new line to process messages", func(t *testing.T) {
assertNewLine := func(t *testing.T, logger *InMemoryLogger, count int) {
startMatches, err := logger.AllMatches(&Match{
Suffix: []string{"\n"},
})

require.NoError(t, err)
require.Len(t, startMatches, count)

for i := 0; i < count; i++ {
require.NotEmpty(t, startMatches[i], "should contains new line")
}
}

expectedLoggerSuccess := NewInMemoryLoggerWithParent(NewPrettyLogger(LoggerOptions{}))
successLogger := newWrappedProcessLogger(expectedLoggerSuccess)

successLogger.ProcessStart("Process start")
assertNewLine(t, expectedLoggerSuccess, 1)

successLogger.ProcessEnd()
assertNewLine(t, expectedLoggerSuccess, 2)

expectedLoggerFail := NewInMemoryLoggerWithParent(NewPrettyLogger(LoggerOptions{}))
failLogger := newWrappedProcessLogger(expectedLoggerFail)

failLogger.ProcessStart("Process fail")
assertNewLine(t, expectedLoggerFail, 1)

failLogger.ProcessFail()
assertNewLine(t, expectedLoggerFail, 2)
})
}
Loading