Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: logging for errors #261

Merged
merged 1 commit into from
Nov 26, 2024
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
2 changes: 2 additions & 0 deletions cmd/exit_handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/rs/zerolog/log"
"os"
)

Expand All @@ -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
}

Expand Down
5 changes: 2 additions & 3 deletions lib/utils/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
3 changes: 3 additions & 0 deletions plugins/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"sync"
"time"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand Down