From ed1c13f2aba4280df3480349bbc4f1c97f1a4cb1 Mon Sep 17 00:00:00 2001 From: LeonardoLordelloFontes Date: Tue, 26 Nov 2024 13:44:09 +0000 Subject: [PATCH] Add Logging for errors --- cmd/exit_handler.go | 2 ++ lib/utils/logger.go | 5 ++--- plugins/filesystem.go | 3 +++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/cmd/exit_handler.go b/cmd/exit_handler.go index aeed0ca2..0e116562 100644 --- a/cmd/exit_handler.go +++ b/cmd/exit_handler.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/rs/zerolog/log" "os" ) @@ -27,6 +28,7 @@ func isNeedReturnErrorCodeFor(kind ignoreOnExit) bool { func exitCodeIfError(err error) int { if err != nil && isNeedReturnErrorCodeFor("errors") { + log.Error().Err(err).Msg("Failed to run 2ms") return errorCode } diff --git a/lib/utils/logger.go b/lib/utils/logger.go index 6c209c2f..6699864b 100644 --- a/lib/utils/logger.go +++ b/lib/utils/logger.go @@ -24,18 +24,17 @@ func (w SpecificLevelWriter) WriteLevel(level zerolog.Level, p []byte) (int, err func CreateLogger(minimumLevel zerolog.Level) zerolog.Logger { writer := zerolog.MultiLevelWriter( SpecificLevelWriter{ - Writer: zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: "15:04:05"}, + Writer: zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: "15:04:05", NoColor: true}, Levels: []zerolog.Level{ zerolog.DebugLevel, zerolog.InfoLevel, zerolog.WarnLevel, }, }, SpecificLevelWriter{ - Writer: zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "15:04:05"}, + Writer: zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "15:04:05", NoColor: true}, Levels: []zerolog.Level{ zerolog.ErrorLevel, zerolog.FatalLevel, zerolog.PanicLevel, }, }, ) - return zerolog.New(writer).Level(minimumLevel).With().Timestamp().Logger() } diff --git a/plugins/filesystem.go b/plugins/filesystem.go index 02949aa6..fc4138ec 100644 --- a/plugins/filesystem.go +++ b/plugins/filesystem.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" "sync" + "time" "github.com/rs/zerolog/log" "github.com/spf13/cobra" @@ -93,6 +94,7 @@ func (p *FileSystemPlugin) getFiles(items chan ISourceItem, errs chan error, wg if err != nil { errs <- fmt.Errorf("error while walking through the directory: %w", err) + time.Sleep(time.Second) // Temporary fix for incorrect non-error exits; needs a better solution. return } @@ -107,6 +109,7 @@ func (p *FileSystemPlugin) getItems(items chan ISourceItem, errs chan error, wg actualFile, err := p.getItem(filePath) if err != nil { errs <- err + time.Sleep(time.Second) // Temporary fix for incorrect non-error exits; needs a better solution. return } items <- *actualFile