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.
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.
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.
func (GrainKind) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
type GrainTray ¶
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 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.