log

package
v1.0.29 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 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 ...any)

Debug logs a message at debug level.

func Debugf

func Debugf(format string, a ...any)

Debugf logs a message at debug level.

func Debugw

func Debugw(keyvals ...any)

Debugw logs a message at debug level.

func Error

func Error(a ...any)

Error logs a message at error level.

func Errorf

func Errorf(format string, a ...any)

Errorf logs a message at error level.

func Errorw

func Errorw(keyvals ...any)

Errorw logs a message at error level.

func Fatal

func Fatal(a ...any)

Fatal logs a message at fatal level.

func Fatalf

func Fatalf(format string, a ...any)

Fatalf logs a message at fatal level.

func Fatalw

func Fatalw(keyvals ...any)

Fatalw logs a message at fatal level.

func Info

func Info(a ...any)

Info logs a message at info level.

func Infof

func Infof(format string, a ...any)

Infof logs a message at info level.

func Infow

func Infow(keyvals ...any)

Infow logs a message at info level.

func Log

func Log(level Level, keyvals ...any)

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

Value return the function value.

func Warn

func Warn(a ...any)

Warn logs a message at warn level.

func Warnf

func Warnf(format string, a ...any)

Warnf logs a message at warnf level.

func Warnw

func Warnw(keyvals ...any)

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 ...any) 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 ...any) 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 ...any)

Debug logs a message at debug level.

func (*Helper) Debugf

func (h *Helper) Debugf(format string, a ...any)

Debugf logs a message at debug level.

func (*Helper) Debugw

func (h *Helper) Debugw(keyvals ...any)

Debugw logs a message at debug level.

func (*Helper) Error

func (h *Helper) Error(a ...any)

Error logs a message at error level.

func (*Helper) Errorf

func (h *Helper) Errorf(format string, a ...any)

Errorf logs a message at error level.

func (*Helper) Errorw

func (h *Helper) Errorw(keyvals ...any)

Errorw logs a message at error level.

func (*Helper) Fatal

func (h *Helper) Fatal(a ...any)

Fatal logs a message at fatal level.

func (*Helper) Fatalf

func (h *Helper) Fatalf(format string, a ...any)

Fatalf logs a message at fatal level.

func (*Helper) Fatalw

func (h *Helper) Fatalw(keyvals ...any)

Fatalw logs a message at fatal level.

func (*Helper) Info

func (h *Helper) Info(a ...any)

Info logs a message at info level.

func (*Helper) Infof

func (h *Helper) Infof(format string, a ...any)

Infof logs a message at info level.

func (*Helper) Infow

func (h *Helper) Infow(keyvals ...any)

Infow logs a message at info level.

func (*Helper) Log

func (h *Helper) Log(level Level, keyvals ...any)

Log Print log by level and keyvals.

func (*Helper) Warn

func (h *Helper) Warn(a ...any)

Warn logs a message at warn level.

func (*Helper) Warnf

func (h *Helper) Warnf(format string, a ...any)

Warnf logs a message at warnf level.

func (*Helper) Warnw

func (h *Helper) Warnw(keyvals ...any)

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 ...any) 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 ...any) 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(...any) string) Option

WithSprint with sprint

func WithSprintf

func WithSprintf(sprintf func(format string, a ...any) string) Option

WithSprintf with sprintf

type Valuer

type Valuer func(ctx context.Context) any

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