From 8f0de5e144397fd045a1b14c5be4ef02f1a88f35 Mon Sep 17 00:00:00 2001 From: Evgeny Abramovich Date: Thu, 1 Jun 2023 15:22:31 -0300 Subject: [PATCH] Refactor debug output for logger --- internal/log/log.go | 9 +-------- internal/log/logger.go | 13 +++++++++++-- internal/log/options.go | 1 + 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/internal/log/log.go b/internal/log/log.go index 53d09dec..121c3075 100644 --- a/internal/log/log.go +++ b/internal/log/log.go @@ -1,18 +1,11 @@ package log import ( - "io" - "os" - "github.com/evg4b/uncors/internal/sfmt" "github.com/pterm/pterm" + "io" ) -func Fatal(a ...any) { - Error(a...) - os.Exit(0) -} - func Error(a ...any) { errorPrinter.Println(a...) } diff --git a/internal/log/logger.go b/internal/log/logger.go index b012fc63..09370b17 100644 --- a/internal/log/logger.go +++ b/internal/log/logger.go @@ -8,6 +8,7 @@ import ( type PrefixedLogger struct { writer *pterm.PrefixPrinter + debug *pterm.PrefixPrinter } func NewLogger(name string, options ...LoggerOption) *PrefixedLogger { @@ -19,6 +20,14 @@ func NewLogger(name string, options ...LoggerOption) *PrefixedLogger { Text: name, }, }, + debug: &pterm.PrefixPrinter{ + MessageStyle: &pterm.ThemeDefault.DebugMessageStyle, + Prefix: pterm.Prefix{ + Text: name, + Style: &pterm.ThemeDefault.DebugPrefixStyle, + }, + Debugger: true, + }, } for _, option := range options { @@ -54,13 +63,13 @@ func (logger *PrefixedLogger) Infof(template string, v ...any) { func (logger *PrefixedLogger) Debug(v ...any) { if pterm.PrintDebugMessages { - logger.writer.Println(debugPrinter.Sprint(v...)) + logger.debug.Println(debugPrinter.Sprint(v...)) } } func (logger *PrefixedLogger) Debugf(template string, v ...any) { if pterm.PrintDebugMessages { - logger.writer.Println(debugPrinter.Sprintf(template, v...)) + logger.debug.Println(debugPrinter.Sprintf(template, v...)) } } diff --git a/internal/log/options.go b/internal/log/options.go index cf8071bf..a3475ed2 100644 --- a/internal/log/options.go +++ b/internal/log/options.go @@ -11,6 +11,7 @@ type LoggerOption = func(logger *PrefixedLogger) func WithOutput(writer io.Writer) LoggerOption { return func(logger *PrefixedLogger) { logger.writer.Writer = writer + logger.debug.Writer = writer } }