log

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2023 License: MIT Imports: 11 Imported by: 1

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")

Third party log library

zap
go get -u github.com/go-kratos/kratos/contrib/log/zap/v2
logrus
go get -u github.com/go-kratos/kratos/contrib/log/logrus/v2
fluent
go get -u github.com/go-kratos/kratos/contrib/log/fluent/v2
aliyun
go get -u github.com/go-kratos/kratos/contrib/log/aliyun/v2

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(4)

	// DefaultTimestamp is a Valuer that returns the current wallclock time.
	DefaultTimestamp = Timestamp(time.RFC3339)
)
View Source
var DefaultLogger = NewStdLogger(log.Writer())

DefaultLogger is default logger.

View Source
var DefaultMessageKey = "msg"

DefaultMessageKey default message key.

Functions

func Debug

func Debug(a ...interface{})

Debug logs a message at debug level.

func Debugf

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

Debugf logs a message at debug level.

func Debugw

func Debugw(keyvals ...interface{})

Debugw logs a message at debug level.

func Error

func Error(a ...interface{})

Error logs a message at error level.

func Errorf

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

Errorf logs a message at error level.

func Errorw

func Errorw(keyvals ...interface{})

Errorw logs a message at error level.

func Fatal

func Fatal(a ...interface{})

Fatal logs a message at fatal level.

func Fatalf

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

Fatalf logs a message at fatal level.

func Fatalw

func Fatalw(keyvals ...interface{})

Fatalw logs a message at fatal level.

func Info

func Info(a ...interface{})

Info logs a message at info level.

func Infof

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

Infof logs a message at info level.

func Infow

func Infow(keyvals ...interface{})

Infow logs a message at info level.

func Log

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

Log Print log by level and keyvals.

func NewWriter

func NewWriter(logger Logger, opts ...WriterOptionFn) io.Writer

NewWriter return a writer wrapper.

func SetLogger

func SetLogger(logger Logger)

SetLogger should be called before any other log call. And it is NOT THREAD SAFE.

func Value

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

Value return the function value.

func Warn

func Warn(a ...interface{})

Warn logs a message at warn level.

func Warnf

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

Warnf logs a message at warnf level.

func Warnw

func Warnw(keyvals ...interface{})

Warnw logs a message at warnf level.

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 Context

func Context(ctx context.Context) *Helper

Context with context logger.

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) Key

func (l Level) Key() string

func (Level) String

func (l Level) String() string

type Logger

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

Logger is a logger interface.

func GetLogger

func GetLogger() Logger

GetLogger returns global logger appliance as logger in current process.

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

WithMessageKey with message key.

func WithSprint

func WithSprint(sprint func(...interface{}) string) Option

WithSprint with sprint

func WithSprintf

func WithSprintf(sprintf func(format string, a ...interface{}) string) Option

WithSprintf with sprintf

type Valuer

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

Valuer is returns a log value.

func Caller

func Caller(depth int) Valuer

Caller 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.

type WriterOptionFn

type WriterOptionFn func(w *writerWrapper)

func WithWriteMessageKey

func WithWriteMessageKey(key string) WriterOptionFn

WithWriteMessageKey set writerWrapper helper message key.

func WithWriterLevel

func WithWriterLevel(level Level) WriterOptionFn

WithWriterLevel set writerWrapper level.

Jump to

Keyboard shortcuts

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