Skip to content

Commit

Permalink
feat(logutil): introduce LogOnError
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal-Leszczynski committed Mar 29, 2024
1 parent 6b38cea commit 2905429
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pkg/util/logutil/logutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (C) 2024 ScyllaDB

package logutil

import (
"context"

"github.com/scylladb/go-log"
)

// LogOnError logs provided msg and keyvals only when f results encounters error.
// It's useful for logging resource cleanup errors that don't directly influence function execution.
func LogOnError(ctx context.Context, log log.Logger, f func() error, msg string, keyvals ...interface{}) {
if err := f(); err != nil {
log.Error(ctx, msg, append(append([]any{}, keyvals...), "error", err)...)
}
}

0 comments on commit 2905429

Please sign in to comment.