log

package
v0.0.0-...-d8e06a9 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2021 License: MIT Imports: 11 Imported by: 4

README

Logger

Usage

Structured logging
logger := log.NewStdLogger(os.Stdout)
// fields & valuer
logger = log.With(logger,
    "service.name", "hellworld",
    "service.version", "v1.0.0",
    "ts", log.DefaultTimestamp,
    "caller", log.DefaultCaller,
)
logger.Log(log.LevelInfo, "key", "value")

// helper
helper := log.NewHelper(logger)
helper.Log(log.LevelInfo, "key", "value")
helper.Info("info message")
helper.Infof("info %s", "message")
helper.Infow("key", "value")

// filter
log := log.NewHelper(log.NewFilter(logger,
	log.FilterLevel(log.LevelInfo),
	log.FilterKey("foo"),
	log.FilterValue("bar"),
	log.FilterFunc(customFilter),
))
log.Debug("debug log")
log.Info("info log")
log.Warn("warn log")
log.Error("warn log")

Documentation

Index

Constants

View Source
const LevelKey = "level"

LevelKey is logger level key.

Variables

View Source
var (

	// DefaultCaller is a Valuer that returns the file and line.
	DefaultCaller = Caller(defaultDepth)

	// DefaultTimestamp is a Valuer that returns the current wallclock time.
	DefaultTimestamp = Timestamp(time.RFC3339)
)
View Source
var DefaultMessageKey = "msg"

Functions

func Value

func Value(ctx context.Context, v interface{}) interface{}

Value return the function value.

Types

type Filter

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

Filter is a logger filter.

func NewFilter

func NewFilter(logger Logger, opts ...FilterOption) *Filter

NewFilter new a logger filter.

func (*Filter) Log

func (f *Filter) Log(level Level, keyvals ...interface{}) error

Log Print log by level and keyvals.

type FilterOption

type FilterOption func(*Filter)

FilterOption is filter option.

func FilterFunc

func FilterFunc(f func(level Level, keyvals ...interface{}) bool) FilterOption

FilterFunc with filter func.

func FilterKey

func FilterKey(key ...string) FilterOption

FilterKey with filter key.

func FilterLevel

func FilterLevel(level Level) FilterOption

FilterLevel with filter level.

func FilterValue

func FilterValue(value ...string) FilterOption

FilterValue with filter value.

type Helper

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

Helper is a logger helper.

func NewHelper

func NewHelper(logger Logger, opts ...Option) *Helper

NewHelper new a logger helper.

func (*Helper) Debug

func (h *Helper) Debug(a ...interface{})

Debug logs a message at debug level.

func (*Helper) Debugf

func (h *Helper) Debugf(format string, a ...interface{})

Debugf logs a message at debug level.

func (*Helper) Debugw

func (h *Helper) Debugw(keyvals ...interface{})

Debugw logs a message at debug level.

func (*Helper) Error

func (h *Helper) Error(a ...interface{})

Error logs a message at error level.

func (*Helper) Errorf

func (h *Helper) Errorf(format string, a ...interface{})

Errorf logs a message at error level.

func (*Helper) Errorw

func (h *Helper) Errorw(keyvals ...interface{})

Errorw logs a message at error level.

func (*Helper) Fatal

func (h *Helper) Fatal(a ...interface{})

Fatal logs a message at fatal level.

func (*Helper) Fatalf

func (h *Helper) Fatalf(format string, a ...interface{})

Fatalf logs a message at fatal level.

func (*Helper) Fatalw

func (h *Helper) Fatalw(keyvals ...interface{})

Fatalw logs a message at fatal level.

func (*Helper) Info

func (h *Helper) Info(a ...interface{})

Info logs a message at info level.

func (*Helper) Infof

func (h *Helper) Infof(format string, a ...interface{})

Infof logs a message at info level.

func (*Helper) Infow

func (h *Helper) Infow(keyvals ...interface{})

Infow logs a message at info level.

func (*Helper) Log

func (h *Helper) Log(level Level, keyvals ...interface{})

Log Print log by level and keyvals.

func (*Helper) Warn

func (h *Helper) Warn(a ...interface{})

Warn logs a message at warn level.

func (*Helper) Warnf

func (h *Helper) Warnf(format string, a ...interface{})

Warnf logs a message at warnf level.

func (*Helper) Warnw

func (h *Helper) Warnw(keyvals ...interface{})

Warnw logs a message at warnf level.

func (*Helper) WithContext

func (h *Helper) WithContext(ctx context.Context) *Helper

WithContext returns a shallow copy of h with its context changed to ctx. The provided ctx must be non-nil.

type Level

type Level int8

Level is a logger level.

const (
	// LevelDebug is logger debug level.
	LevelDebug Level = iota - 1
	// LevelInfo is logger info level.
	LevelInfo
	// LevelWarn is logger warn level.
	LevelWarn
	// LevelError is logger error level.
	LevelError
	// LevelFatal is logger fatal level
	LevelFatal
)

func ParseLevel

func ParseLevel(s string) Level

ParseLevel parses a level string into a logger Level value.

func (Level) String

func (l Level) String() string

type Logger

type Logger interface {
	Log(level Level, keyvals ...interface{}) error
}

Logger is a logger interface.

var DefaultLogger Logger = NewStdLogger(log.Writer())

DefaultLogger is default logger.

func MultiLogger

func MultiLogger(logs ...Logger) Logger

MultiLogger wraps multi logger.

func NewStdLogger

func NewStdLogger(w io.Writer) Logger

NewStdLogger new a logger with writer.

func With

func With(l Logger, kv ...interface{}) Logger

With with logger fields.

func WithContext

func WithContext(ctx context.Context, l Logger) Logger

WithContext returns a shallow copy of l with its context changed to ctx. The provided ctx must be non-nil.

type Option

type Option func(*Helper)

Option is Helper option.

func WithMessageKey

func WithMessageKey(k string) Option

type Valuer

type Valuer func(ctx context.Context) interface{}

Valuer is returns a log value.

func Caller

func Caller(depth int) Valuer

Caller returns returns a Valuer that returns a pkg/file:line description of the caller.

func Timestamp

func Timestamp(layout string) Valuer

Timestamp returns a timestamp Valuer with a custom time format.

Jump to

Keyboard shortcuts

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