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
10 changes: 5 additions & 5 deletions pkg/log/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func assertFollowAllInterfaces(t *testing.T, logger Logger) {
assertSilentLoggerProviderFollowFormatLnInterface(t, logger)
})

t.Run("Format Ln logger", func(t *testing.T) {
t.Run("Format with Ln logger", func(t *testing.T) {
assertFollowFormatLnInterface(t, logger)
})

Expand All @@ -49,18 +49,18 @@ func assertFollowAllInterfaces(t *testing.T, logger Logger) {
func assertFollowFormatLnInterface(t *testing.T, logger Logger) {
runs := []func(){
func() {
logger.InfoFLn("INFO %s", "test_info")
logger.InfoF("INFO %s", "test_info")
},
func() {
logger.WarnFLn("WARN %s", "test_warn")
logger.WarnF("WARN %s", "test_warn")
},

func() {
logger.DebugFLn("DEBUG %s", "test_debug")
logger.DebugF("DEBUG %s", "test_debug")
},

func() {
logger.ErrorFLn("ERROR %v", fmt.Errorf("test_error"))
logger.ErrorF("ERROR %v", fmt.Errorf("test_error"))
},
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/log/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,23 @@ func (d *DummyLogger) Process(_ Process, t string, run func() error) error {
return err
}

func (d *DummyLogger) InfoF(format string, a ...interface{}) {
func (d *DummyLogger) InfoFWithoutLn(format string, a ...interface{}) {
fmt.Printf(format, a...)
}

func (d *DummyLogger) InfoLn(a ...interface{}) {
fmt.Println(a...)
}

func (d *DummyLogger) ErrorF(format string, a ...interface{}) {
func (d *DummyLogger) ErrorFWithoutLn(format string, a ...interface{}) {
fmt.Printf(format, a...)
}

func (d *DummyLogger) ErrorLn(a ...interface{}) {
fmt.Println(a...)
}

func (d *DummyLogger) DebugF(format string, a ...interface{}) {
func (d *DummyLogger) DebugFWithoutLn(format string, a ...interface{}) {
if d.isDebug {
fmt.Printf(format, a...)
}
Expand Down Expand Up @@ -110,7 +110,7 @@ func (d *DummyLogger) WarnLn(a ...interface{}) {
fmt.Println(a...)
}

func (d *DummyLogger) WarnF(format string, a ...interface{}) {
func (d *DummyLogger) WarnFWithoutLn(format string, a ...interface{}) {
fmt.Printf(format, a...)
}

Expand Down
16 changes: 8 additions & 8 deletions pkg/log/in_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,33 +167,33 @@ func (l *InMemoryLogger) Process(p Process, t string, action func() error) error
return err
}

func (l *InMemoryLogger) InfoF(format string, a ...interface{}) {
func (l *InMemoryLogger) InfoFWithoutLn(format string, a ...interface{}) {
l.writeEntityFormatted(format, a...)
l.parent.InfoF(format, a...)
l.parent.InfoFWithoutLn(format, a...)
}

func (l *InMemoryLogger) InfoLn(a ...interface{}) {
l.writeEntityFormatted(listToString(a))
l.parent.InfoLn(a...)
}

func (l *InMemoryLogger) ErrorF(format string, a ...interface{}) {
func (l *InMemoryLogger) ErrorFWithoutLn(format string, a ...interface{}) {
l.writeEntityWithPrefix(l.errorPrefix, format, a...)
l.parent.ErrorF(format, a...)
l.parent.ErrorFWithoutLn(format, a...)
}

func (l *InMemoryLogger) ErrorLn(a ...interface{}) {
l.writeEntityWithPrefix(l.errorPrefix, listToString(a))
l.parent.ErrorLn(a...)
}

func (l *InMemoryLogger) DebugF(format string, a ...interface{}) {
func (l *InMemoryLogger) DebugFWithoutLn(format string, a ...interface{}) {
if l.notDebug {
return
}

l.writeEntityWithPrefix(l.debugPrefix, format, a...)
l.parent.DebugF(format, a...)
l.parent.DebugFWithoutLn(format, a...)
}

func (l *InMemoryLogger) DebugLn(a ...interface{}) {
Expand All @@ -205,9 +205,9 @@ func (l *InMemoryLogger) DebugLn(a ...interface{}) {
l.parent.DebugLn(a...)
}

func (l *InMemoryLogger) WarnF(format string, a ...interface{}) {
func (l *InMemoryLogger) WarnFWithoutLn(format string, a ...interface{}) {
l.writeEntityFormatted(format, a...)
l.parent.WarnF(format, a...)
l.parent.WarnFWithoutLn(format, a...)
}

func (l *InMemoryLogger) WarnLn(a ...interface{}) {
Expand Down
16 changes: 8 additions & 8 deletions pkg/log/ln_logger_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ func newFormatWithNewLineLoggerWrapper(parent baseLogger) *formatWithNewLineLogg
return &formatWithNewLineLoggerWrapper{parent: parent}
}

func (w *formatWithNewLineLoggerWrapper) InfoFLn(format string, a ...any) {
w.parent.InfoF(addLnToMessage(format, a...))
func (w *formatWithNewLineLoggerWrapper) InfoF(format string, a ...any) {
w.parent.InfoFWithoutLn(addLnToMessage(format, a...))
}

func (w *formatWithNewLineLoggerWrapper) ErrorFLn(format string, a ...any) {
w.parent.ErrorF(addLnToMessage(format, a...))
func (w *formatWithNewLineLoggerWrapper) ErrorF(format string, a ...any) {
w.parent.ErrorFWithoutLn(addLnToMessage(format, a...))
}

func (w *formatWithNewLineLoggerWrapper) DebugFLn(format string, a ...any) {
w.parent.DebugF(addLnToMessage(format, a...))
func (w *formatWithNewLineLoggerWrapper) DebugF(format string, a ...any) {
w.parent.DebugFWithoutLn(addLnToMessage(format, a...))
}

func (w *formatWithNewLineLoggerWrapper) WarnFLn(format string, a ...any) {
w.parent.WarnF(addLnToMessage(format, a...))
func (w *formatWithNewLineLoggerWrapper) WarnF(format string, a ...any) {
w.parent.WarnFWithoutLn(addLnToMessage(format, a...))
}

func addLnToMessage(format string, a ...any) string {
Expand Down
49 changes: 49 additions & 0 deletions pkg/log/ln_logger_wrapper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2026 Flant JSC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package log

import (
"fmt"
"testing"

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

func TestLnLoggerWrapper(t *testing.T) {
logger := NewInMemoryLoggerWithParent(NewSimpleLogger(LoggerOptions{IsDebug: true}))

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

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

wrapper := newFormatWithNewLineLoggerWrapper(logger)

wrapper.ErrorF("Error")
assertAddNewLine(t, "Error")

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

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

wrapper.DebugF("Debug")
assertAddNewLine(t, "Debug")
}
32 changes: 24 additions & 8 deletions pkg/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,16 @@ type baseLogger interface {

Process(Process, string, func() error) error

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

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

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

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

Success(string)
Expand All @@ -129,11 +129,27 @@ type baseLogger interface {
ProcessLogger() ProcessLogger
}

// formatWithNewLineLogger
// Because we are using pretty printing in dhctl
// we huge usage InfoF("msg\n") with \n in end of line
// and often forget to add \n in message
type formatWithNewLineLogger interface {
InfoFLn(format string, a ...any)
ErrorFLn(format string, a ...any)
DebugFLn(format string, a ...any)
WarnFLn(format string, a ...any)
// InfoF
// Warning! InfoF add \n to end of message.
// If you do not have \n to end of message please use InfoFWithoutLn
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
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
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
WarnF(format string, a ...any)
}

type Logger interface {
Expand Down
16 changes: 8 additions & 8 deletions pkg/log/pretty.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,23 @@ func (d *PrettyLogger) Process(p Process, t string, run func() error) error {
return d.logboekLogger.LogProcess(format.Title, t).Options(format.OptionsSetter).DoError(run)
}

func (d *PrettyLogger) InfoF(format string, a ...interface{}) {
func (d *PrettyLogger) InfoFWithoutLn(format string, a ...interface{}) {
d.logboekLogger.Info().LogF(format, a...)
}

func (d *PrettyLogger) InfoLn(a ...interface{}) {
d.logboekLogger.Info().LogLn(a...)
}

func (d *PrettyLogger) ErrorF(format string, a ...interface{}) {
func (d *PrettyLogger) ErrorFWithoutLn(format string, a ...interface{}) {
d.logboekLogger.Error().LogF(format, a...)
}

func (d *PrettyLogger) ErrorLn(a ...interface{}) {
d.logboekLogger.Error().LogLn(a...)
}

func (d *PrettyLogger) DebugF(format string, a ...interface{}) {
func (d *PrettyLogger) DebugFWithoutLn(format string, a ...interface{}) {
if d.debugLogWriter != nil {
o := fmt.Sprintf(format, a...)
_, err := d.debugLogWriter.DebugStream.Write([]byte(o))
Expand Down Expand Up @@ -162,11 +162,11 @@ func (d *PrettyLogger) DebugLn(a ...interface{}) {
}

func (d *PrettyLogger) Success(l string) {
d.InfoF("🎉 %s", l)
d.InfoFWithoutLn("🎉 %s", l)
}

func (d *PrettyLogger) Fail(l string) {
d.InfoF("️⛱️️ %s", l)
d.InfoFWithoutLn("️⛱️️ %s", l)
}

func (d *PrettyLogger) FailRetry(l string) {
Expand All @@ -178,17 +178,17 @@ func (d *PrettyLogger) WarnLn(a ...interface{}) {
d.InfoLn(color.New(color.Bold).Sprint(a...))
}

func (d *PrettyLogger) WarnF(format string, a ...interface{}) {
func (d *PrettyLogger) WarnFWithoutLn(format string, a ...interface{}) {
line := color.New(color.Bold).Sprintf("❗ ~ "+format, a...)
d.InfoF(line)
d.InfoFWithoutLn(line)
}

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

func (d *PrettyLogger) Write(content []byte) (int, error) {
d.InfoF(string(content))
d.InfoFWithoutLn(string(content))
return len(content), nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/log/pretty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func testPrettyLoggerProcess(t *testing.T, tst *testPrettyLogger) {
inRunMsg := fmt.Sprintf("run in process: %s", string(tst.process))

err := tst.logger.Process(tst.process, processName, func() error {
tst.logger.InfoFLn(inRunMsg)
tst.logger.InfoF(inRunMsg)
return nil
})

Expand Down
8 changes: 4 additions & 4 deletions pkg/log/silent.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (d *SilentLogger) FlushAndClose() error {
return nil
}

func (d *SilentLogger) InfoF(format string, a ...interface{}) {
func (d *SilentLogger) InfoFWithoutLn(format string, a ...interface{}) {
if d.t != nil {
d.t.writeToFile(fmt.Sprintf(format, a...))
}
Expand All @@ -80,7 +80,7 @@ func (d *SilentLogger) InfoLn(a ...interface{}) {
}
}

func (d *SilentLogger) ErrorF(format string, a ...interface{}) {
func (d *SilentLogger) ErrorFWithoutLn(format string, a ...interface{}) {
if d.t != nil {
d.t.writeToFile(fmt.Sprintf(format, a...))
}
Expand All @@ -92,7 +92,7 @@ func (d *SilentLogger) ErrorLn(a ...interface{}) {
}
}

func (d *SilentLogger) DebugF(format string, a ...interface{}) {
func (d *SilentLogger) DebugFWithoutLn(format string, a ...interface{}) {
if d.t != nil {
d.t.writeToFile(fmt.Sprintf(format, a...))
}
Expand Down Expand Up @@ -128,7 +128,7 @@ func (d *SilentLogger) WarnLn(a ...interface{}) {
}
}

func (d *SilentLogger) WarnF(format string, a ...interface{}) {
func (d *SilentLogger) WarnFWithoutLn(format string, a ...interface{}) {
if d.t != nil {
d.t.writeToFile(fmt.Sprintf(format, a...))
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/log/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,23 @@ func (d *SimpleLogger) Process(p Process, t string, run func() error) error {
return err
}

func (d *SimpleLogger) InfoF(format string, a ...interface{}) {
func (d *SimpleLogger) InfoFWithoutLn(format string, a ...interface{}) {
d.logger.Info(format, a...)
}

func (d *SimpleLogger) InfoLn(a ...interface{}) {
d.logger.Info(listToString(a))
}

func (d *SimpleLogger) ErrorF(format string, a ...interface{}) {
func (d *SimpleLogger) ErrorFWithoutLn(format string, a ...interface{}) {
d.logger.Error(format, a...)
}

func (d *SimpleLogger) ErrorLn(a ...interface{}) {
d.logger.Error(listToString(a))
}

func (d *SimpleLogger) DebugF(format string, a ...interface{}) {
func (d *SimpleLogger) DebugFWithoutLn(format string, a ...interface{}) {
if d.isDebug {
d.logger.Debug(format, a...)
}
Expand All @@ -121,7 +121,7 @@ func (d *SimpleLogger) FailRetry(l string) {
d.logger.With("status", "FAIL").Warn(l)
}

func (d *SimpleLogger) WarnF(format string, a ...interface{}) {
func (d *SimpleLogger) WarnFWithoutLn(format string, a ...interface{}) {
d.logger.Warn(format, a...)
}

Expand Down
Loading