crumbs

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2020 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package crumbs provides a flexible but still convenient helper for logging. The backend is based on an interface and so can be exchanged. A Crumbs instance returns a leveled CrumbWriter which can write Info and Error messages. They messages also may be complemented with key/value pairs.

c := crumbs.New()
c.L(1).Info("a message", "id", 1337, "name", aName)
c.L(9).Error(err, "something happened")

The additional function Crumble helps to log errors in defer expressions.

cw := c.L(8)
defer crumbs.Crumble(cw, myFile.Close, "closing file failed", "name", filename)

Configuration options of Crumbs are the backend and the lowest level for reporting.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Crumble

func Crumble(cw CrumbWriter, f func() error, msg string, infos ...interface{})

Crumble is intended to be used with defer. When called the given function f will be called. In case of an error an error crumb will be written with the given message and values.

func NewContext

func NewContext(ctx context.Context, cw CrumbWriter) context.Context

NewContext creates a context containing a CrumbWriter.

Types

type CrumbWriter

type CrumbWriter interface {
	// Info logs messages with key/value pairs of additional
	// information.
	Info(msg string, infos ...interface{}) error

	// Error logs errors with an additional message and key/value
	// pairs of additional information.
	Error(err error, msg string, infos ...interface{}) error
}

CrumbWriter is a leveled and configured writer for infos and errors.

func FromContext

func FromContext(ctx context.Context) (CrumbWriter, bool)

FromContext retrieves a CrumbWriter from a context.

type Crumbs

type Crumbs struct {
	// contains filtered or unexported fields
}

Crumbs is the entry poing for all logging.

func New

func New(options ...Option) *Crumbs

New creates and initializes a new crumbs instances.

func (*Crumbs) L

func (c *Crumbs) L(level byte) CrumbWriter

L returns a leveled crumb writer.

type Grain

type Grain struct {
	Timestamp time.Time   `json:"timestamp"`
	Kind      GrainKind   `json:"kind"`
	Message   string      `json:"message"`
	Infos     []GrainInfo `json:"infos"`
}

Grain contains all data to log.

func (Grain) String

func (g Grain) String() string

String implements fmt.Stringer. This implementation marshals the Grain into JSON.

type GrainInfo

type GrainInfo struct {
	Key   string      `json:"key"`
	Value interface{} `json:"value"`
}

GrainInfo contains a pair of key and value of a Grain.

type GrainKind

type GrainKind int

GrainKind describes if a Grain is an information or an error.

const (
	InfoGrain GrainKind = iota
	ErrorGrain
)

Kind of grains.

func (GrainKind) MarshalJSON

func (gk GrainKind) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (GrainKind) String

func (gk GrainKind) String() string

String implements fmt.Stringer.

type GrainTray

type GrainTray interface {
	// Put adds a Grain to the tray.
	Put(grain *Grain) error
}

GrainTray defines the interface for the backends responsible to collect and store the Grains.

type LoggerGrainTray

type LoggerGrainTray struct {
	// contains filtered or unexported fields
}

LoggerGrainTray writes the grain to the configured Logger.

func NewLoggerGrainTray

func NewLoggerGrainTray(logger *log.Logger) *LoggerGrainTray

NewLoggerGrainTray creates a LoggerGrainTray with the given Logger.

func (*LoggerGrainTray) Put

func (t *LoggerGrainTray) Put(grain *Grain) error

Put implements GrainTray.

type Option

type Option func(c *Crumbs)

Option is a function able to configure the Crumbs.

func Level

func Level(level byte) Option

Level sets the Crumbs level for logging. Default is 0.

func Tray

func Tray(tray GrainTray) Option

Tray sets the GrainTray backend. Default is the WriterGrainTray on stdout.

type WriterGrainTray

type WriterGrainTray struct {
	// contains filtered or unexported fields
}

WriterGrainTray writes the grain to the configured writer.

func NewWriterGrainTray

func NewWriterGrainTray(out io.Writer) *WriterGrainTray

NewWriterGrainTray creates a WriterGrainTray with the given writer.

func (*WriterGrainTray) Put

func (t *WriterGrainTray) Put(grain *Grain) error

Put implements GrainTray.

Jump to

Keyboard shortcuts

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