logger

package
v0.0.0-...-5720ada Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2024 License: MIT Imports: 5 Imported by: 18

Documentation

Index

Examples

Constants

View Source
const UUIDKey = "uuid"

Variables

View Source
var GlobalFields = logrus.Fields{}

Functions

func ContextLog

func ContextLog(ctx Valuer, err []error, entry *logrus.Entry) *logrus.Entry

func GetLoggableValue

func GetLoggableValue(ctx Valuer, key string) interface{}

GetLoggableValue returns the value of the metadata currently attached to the Context.

func GetLoggableValues

func GetLoggableValues(ctx Valuer) logrus.Fields

func LogIfError

func LogIfError(ctx context.Context, fn func() error, logger Logger, msg string)

LogIfError makes it less verbose to defer a Close() call while handling an unlikely-but-possible error return by logging it. Example:

defer LogIfError(ctx, f.Close, log, "failed to close file")

func WatchingLoggable

func WatchingLoggable(ctx context.Context, l Loggable) context.Context

WatchingLoggable attaches a Loggable to the Context. Upon logging, LogFields will be called and the fields will be attached as metadata.

Example
loggable := &exampleLoggable{"foo": "bar"}

ctx := context.Background()
ctx = WatchingLoggable(ctx, loggable)

log := New("testing")

// Typically called as log(ctx, nil).Debug("the message")
entry := log(ctx, nil)
fmt.Printf("%+v", entry.Data["foo"])
Output:

bar

func WithField

func WithField(ctx context.Context, k string, v interface{}) context.Context

WithField attaches a key-value pair to a Context. Upon logging, the pair will be attached as metadata.

Example
ctx := context.Background()
ctx = WithField(ctx, "foo", "bar")

log := New("testing")

// Typically called as log(ctx, nil).Debug("the message")
entry := log(ctx, nil)
fmt.Printf("%+v", entry.Data["foo"])
Output:

bar

func WithFields

func WithFields(ctx context.Context, m logrus.Fields) context.Context

WithFields attaches fields to a Context. Upon logging, those fields will be attached as metadata.

Example
ctx := context.Background()
ctx = WithFields(ctx, logrus.Fields{
	"foo": "bar",
})

log := New("testing")

// Typically called as log(ctx, nil).Debug("the message")
entry := log(ctx, nil)
fmt.Printf("%+v", entry.Data["foo"])
Output:

bar

func WithLoggable

func WithLoggable(ctx context.Context, l Loggable) context.Context

WithLoggable attaches a Loggable's fields to the Context. Upon logging, those fields will be attached as metadata.

func WithUUID

func WithUUID(ctx context.Context) (context.Context, string)

Types

type Loggable

type Loggable interface {
	LogFields() logrus.Fields
}

type Logger

type Logger func(Valuer, ...error) *logrus.Entry

func New

func New(name string) Logger

type Valuer

type Valuer interface {
	Value(key interface{}) interface{}
}

Valuer is essentially a Context, but down-scoped to what Loggable expects

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL