Skip to content

Commit

Permalink
log: add log.Entry type
Browse files Browse the repository at this point in the history
Don't return logrus types from exported functions.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 634a4a1)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Sep 8, 2023
1 parent dd12788 commit bace17e
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions log/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ type loggerKey struct{}
// Fields type to pass to "WithFields".
type Fields = logrus.Fields

// Entry is a logging entry. It contains all the fields passed with
// [Entry.WithFields]. It's finally logged when Trace, Debug, Info, Warn,
// Error, Fatal or Panic is called on it. These objects can be reused and
// passed around as much as you wish to avoid field duplication.
//
// Entry is a transitional type, and currently an alias for [logrus.Entry].
type Entry = logrus.Entry

// RFC3339NanoFixed is [time.RFC3339Nano] with nanoseconds padded using
// zeros to ensure the formatted time is always the same number of
// characters.
Expand Down Expand Up @@ -129,20 +137,20 @@ func SetFormat(format OutputFormat) error {

// WithLogger returns a new context with the provided logger. Use in
// combination with logger.WithField(s) for great effect.
func WithLogger(ctx context.Context, logger *logrus.Entry) context.Context {
func WithLogger(ctx context.Context, logger *Entry) context.Context {
return context.WithValue(ctx, loggerKey{}, logger.WithContext(ctx))
}

// GetLogger retrieves the current logger from the context. If no logger is
// available, the default logger is returned.
func GetLogger(ctx context.Context) *logrus.Entry {
func GetLogger(ctx context.Context) *Entry {
return G(ctx)
}

// G is a shorthand for [GetLogger].
func G(ctx context.Context) *logrus.Entry {
func G(ctx context.Context) *Entry {
if logger := ctx.Value(loggerKey{}); logger != nil {
return logger.(*logrus.Entry)
return logger.(*Entry)
}
return L.WithContext(ctx)
}

0 comments on commit bace17e

Please sign in to comment.