logctx

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2020 License: Unlicense Imports: 2 Imported by: 3

Documentation

Overview

Package logctx adds contextual logging to logrus.

Example
package main

import (
	"context"
	"os"

	log "github.com/sirupsen/logrus"

	"github.com/go-logrusutil/logrusutil/logctx"
)

func main() {
	log.SetOutput(os.Stdout)
	log.SetFormatter(&log.TextFormatter{DisableTimestamp: true})

	// setting contextual log entry
	ctx := logctx.New(context.Background(), log.WithField("foo", "bar"))

	// adding additional log field
	ctx = logctx.AddField(ctx, "bizz", "buzz")

	// retrieving context log entry, adding some data and emitting the log
	logctx.From(ctx).Info("hello world")
}
Output:

level=info msg="hello world" bizz=buzz foo=bar
Example (Default)
package main

import (
	"context"
	"os"

	log "github.com/sirupsen/logrus"

	"github.com/go-logrusutil/logrusutil/logctx"
)

func main() {
	log.SetOutput(os.Stdout)
	log.SetFormatter(&log.TextFormatter{DisableTimestamp: true})

	// set the default log entry
	logctx.Default = log.WithField("foo", "bar")

	// get a log entry from context for which a contextual entry was not set
	logctx.From(context.Background()).Info("hello world")
}
Output:

level=info msg="hello world" foo=bar

Index

Examples

Constants

This section is empty.

Variables

Default is used to create a new log entry if there is none in the context.

Functions

func AddField added in v1.1.0

func AddField(ctx context.Context, key string, value interface{}) context.Context

AddField adds a log field to the contexual log entry.

func From

func From(ctx context.Context) *logrus.Entry

From returns the log entry from the context. Returns log entry from Default if there is no log entry in the context.

func New

func New(ctx context.Context, logEntry *logrus.Entry) context.Context

New creates a new context with log entry.

Types

This section is empty.

Jump to

Keyboard shortcuts

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