Skip to content

Commit 707ca94

Browse files
committed
log: add log.Entry type
Don't return logrus types from exported functions. Signed-off-by: Sebastiaan van Stijn <[email protected]> (cherry picked from commit 634a4a1) Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 0a79e67 commit 707ca94

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

log/context.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ type loggerKey struct{}
3131
// Fields type to pass to "WithFields".
3232
type Fields = logrus.Fields
3333

34+
// Entry is a logging entry. It contains all the fields passed with
35+
// [Entry.WithFields]. It's finally logged when Trace, Debug, Info, Warn,
36+
// Error, Fatal or Panic is called on it. These objects can be reused and
37+
// passed around as much as you wish to avoid field duplication.
38+
//
39+
// Entry is a transitional type, and currently an alias for [logrus.Entry].
40+
type Entry = logrus.Entry
41+
3442
// RFC3339NanoFixed is [time.RFC3339Nano] with nanoseconds padded using
3543
// zeros to ensure the formatted time is always the same number of
3644
// characters.
@@ -129,20 +137,20 @@ func SetFormat(format OutputFormat) error {
129137

130138
// WithLogger returns a new context with the provided logger. Use in
131139
// combination with logger.WithField(s) for great effect.
132-
func WithLogger(ctx context.Context, logger *logrus.Entry) context.Context {
140+
func WithLogger(ctx context.Context, logger *Entry) context.Context {
133141
return context.WithValue(ctx, loggerKey{}, logger.WithContext(ctx))
134142
}
135143

136144
// GetLogger retrieves the current logger from the context. If no logger is
137145
// available, the default logger is returned.
138-
func GetLogger(ctx context.Context) *logrus.Entry {
146+
func GetLogger(ctx context.Context) *Entry {
139147
return G(ctx)
140148
}
141149

142150
// G is a shorthand for [GetLogger].
143-
func G(ctx context.Context) *logrus.Entry {
151+
func G(ctx context.Context) *Entry {
144152
if logger := ctx.Value(loggerKey{}); logger != nil {
145-
return logger.(*logrus.Entry)
153+
return logger.(*Entry)
146154
}
147155
return L.WithContext(ctx)
148156
}

0 commit comments

Comments
 (0)