contract

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2021 License: Apache-2.0 Imports: 6 Imported by: 6

Documentation

Overview

Package contract provides interfaces for a common logging backend

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatToError added in v1.1.0

func FormatToError(name string, callerSkip int, msg string, fields ...Field) error

FormatToError is a public helper function that converts a msg and fields pair into an combined error. Intended for Logger.ErrorReturn

Types

type Field

type Field interface {
	// Key returns the fields key part
	Key() string
	// Value returns the fields value part
	Value() interface{}
}

A Field is a marshaling operation used to add a key-value pair to a logger's context

func NewField

func NewField(key string, value interface{}) Field

NewField provides a simple shortcut function to create a struct that satisfies the Field interface

type Level

type Level int8

Level is a logging priority. Higher levels are more important.

const (
	// DebugLevel logs are typically voluminous, and are usually disabled in
	// production.
	DebugLevel Level = iota - 1
	// InfoLevel is the default logging priority.
	InfoLevel
	// WarnLevel logs are more important than Info, but don't need individual
	// human review.
	WarnLevel
	// ErrorLevel logs are high-priority. If an application is running smoothly,
	// it shouldn't generate any error-level logs.
	ErrorLevel
	// DPanicLevel logs are particularly important errors. In development the
	// logger panics after writing the message.
	DPanicLevel
	// PanicLevel logs a message, then panics.
	PanicLevel
	// FatalLevel logs a message, then calls os.Exit(1).
	FatalLevel
)

func (Level) String added in v1.1.0

func (l Level) String() string

String returns a readable representation of Level

type Logger

type Logger interface {
	// Named adds a new path segment to the logger's name. Segments are joined by
	// periods. By default, Loggers are unnamed.
	Named(name string) Logger
	// With creates a child logger and adds structured context to it. Fields added
	// to the child don't affect the parent, and vice versa.
	With(fields ...Field) Logger
	// Sync flushes any buffered log entries and should be called by applications
	// before exiting
	Sync() error
	// Debug logs a message at DebugLevel. The message includes any fields passed
	// at the log site, as well as any fields accumulated on the logger.
	Debug(msg string, fields ...Field)
	// Info logs a message at InfoLevel. The message includes any fields passed
	// at the log site, as well as any fields accumulated on the logger.
	Info(msg string, fields ...Field)
	// Warn logs a message at WarnLevel. The message includes any fields passed
	// at the log site, as well as any fields accumulated on the logger.
	Warn(msg string, fields ...Field)
	// Error logs a message at ErrorLevel. The message includes any fields passed
	// at the log site, as well as any fields accumulated on the logger.
	Error(msg string, fields ...Field)
	// ErrorReturn logs a message at ErrorLevel exactly like the Error function, but
	// additionally returns an error object, containing the provided information.
	ErrorReturn(msg string, fields ...Field) error
	// DPanic logs a message at DPanicLevel. The message includes any fields
	// passed at the log site, as well as any fields accumulated on the logger.
	//
	// If the logger is in development mode, it then panics (DPanic means
	// "development panic"). This is useful for catching errors that are
	// recoverable, but shouldn't ever happen.
	DPanic(msg string, fields ...Field)
	// Panic logs a message at PanicLevel. The message includes any fields passed
	// at the log site, as well as any fields accumulated on the logger.
	//
	// The logger then panics, even if logging at PanicLevel is disabled.
	Panic(msg string, fields ...Field)
	// Fatal logs a message at FatalLevel. The message includes any fields passed
	// at the log site, as well as any fields accumulated on the logger.
	//
	// The logger then calls os.Exit(1), even if logging at FatalLevel is
	// disabled.
	Fatal(msg string, fields ...Field)
}

Logger provides leveled, structured logging. It is an abstract interface for different logging backends.

func MustNewStd added in v1.1.0

func MustNewStd(opts ...StdOption) Logger

MustNewStd is like NewStd, but panics if one of the StdOption cannot be applied.

func NewStd added in v1.1.0

func NewStd(opts ...StdOption) (Logger, error)

NewStd creates a new Logger using only Go's standard library. This can be useful for packages that want to include liblog, and provide logging output by default, but not overstuff their library with third-party dependencies.

type StdOption added in v1.1.0

type StdOption func(*logger) error

StdOption is an option function for NewStd and MustNewStd

func DisableLogWrites

func DisableLogWrites() StdOption

DisableLogWrites fully disables the logger instance. No message is written to the OutWriter and ErrWriter.

func ErrWriter

func ErrWriter(w io.Writer) StdOption

ErrWriter lets you set the destination for errors that happened inside the standard logger itself (which at the moment can only happen if the OutWriter returns an error on writing)

func IsInDevEnvironment

func IsInDevEnvironment(isInDevEnvironment bool) StdOption

IsInDevEnvironment influences whether or not DPanicLevel panics or not

func MinLevel

func MinLevel(minLevel Level) StdOption

MinLevel sets the minimum needed level for messages that get written to OutWriter. Messages below this level will get discarded

func OutWriter

func OutWriter(w io.Writer) StdOption

OutWriter lets you set the destination for the default logging output

Jump to

Keyboard shortcuts

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