Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
context loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe committed Apr 10, 2024
1 parent ca9ab5a commit c03bb12
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions util/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

import (
"context"
"fmt"
"strings"

Expand All @@ -23,18 +24,36 @@ func LoggingError(err error) error {
return err
}

// LoggingCtxError is a utility to combine logging an error, and returning and error
func LoggingCtxError(ctx context.Context, err error) error {
logrus.WithContext(ctx).WithError(err).Error()
return err
}

// LoggingNewError is a utility to create an error from a message, log it, and return it as an error
func LoggingNewError(msg string) error {
err := errors.New(msg)
logrus.WithError(err).Error()
return err
}

// LoggingCtxNewError is a utility to create an error from a message, log it, and return it as an error
func LoggingCtxNewError(ctx context.Context, msg string) error {
err := errors.New(msg)
logrus.WithContext(ctx).WithError(err).Error()
return err
}

// LoggingNewErrorf is a utility to create an error from a formatted message, log it, and return it as an error
func LoggingNewErrorf(msg string, args ...any) error {
return LoggingNewError(fmt.Sprintf(msg, args...))
}

// LoggingCtxNewErrorf is a utility to create an error from a formatted message, log it, and return it as an error
func LoggingCtxNewErrorf(ctx context.Context, msg string, args ...any) error {
return LoggingCtxNewError(ctx, fmt.Sprintf(msg, args...))
}

// LoggingErrorMsg is a utility to combine logging an error, and returning and error with a message
func LoggingErrorMsg(err error, msg string) error {
logrus.WithError(err).Error(SanitizeLog(msg))
Expand All @@ -44,11 +63,25 @@ func LoggingErrorMsg(err error, msg string) error {
return errors.Wrap(err, msg)
}

// LoggingCtxErrorMsg is a utility to combine logging an error, and returning and error with a message
func LoggingCtxErrorMsg(ctx context.Context, err error, msg string) error {
logrus.WithContext(ctx).WithError(err).Error(SanitizeLog(msg))
if err == nil {
return errors.New(msg)
}
return errors.Wrap(err, msg)
}

// LoggingErrorMsgf is a utility to combine logging an error, and returning and error with a formatted message
func LoggingErrorMsgf(err error, msg string, args ...any) error {
return LoggingErrorMsg(err, fmt.Sprintf(msg, args...))
}

// LoggingCtxErrorMsgf is a utility to combine logging an error, and returning and error with a formatted message
func LoggingCtxErrorMsgf(ctx context.Context, err error, msg string, args ...any) error {
return LoggingCtxErrorMsg(ctx, err, fmt.Sprintf(msg, args...))
}

// SanitizeLog prevents certain classes of injection attacks before logging
// https://codeql.github.com/codeql-query-help/go/go-log-injection/
func SanitizeLog(log string) string {
Expand Down

0 comments on commit c03bb12

Please sign in to comment.