logger

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2024 License: MIT Imports: 7 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddKV

func AddKV(ctx context.Context, kvs ...any) context.Context

AddKV Adds KV pairs to

Example
ctx := AddKV(
	context.Background(),
	zap.String("a", "b"),
	zap.Int("c", 1),
	zap.Error(assert.AnError),
)

// В сообщении появятся поля, переданные в AddKV.
Info(ctx, "some message")
Output:

func Debug

func Debug(ctx context.Context, args ...interface{})

func DebugKV

func DebugKV(ctx context.Context, message string, kvs ...interface{})

func Debugf

func Debugf(ctx context.Context, format string, args ...interface{})

func Error

func Error(ctx context.Context, args ...interface{})

func ErrorKV

func ErrorKV(ctx context.Context, message string, kvs ...interface{})

func Errorf

func Errorf(ctx context.Context, format string, args ...interface{})

func Fatal

func Fatal(ctx context.Context, args ...interface{})

func FatalKV

func FatalKV(ctx context.Context, message string, kvs ...interface{})

func Fatalf

func Fatalf(ctx context.Context, format string, args ...interface{})

func FromContext

func FromContext(ctx context.Context) *zap.SugaredLogger

FromContext Gets the logger from contet

func Info

func Info(ctx context.Context, args ...interface{})

func InfoKV

func InfoKV(ctx context.Context, message string, kvs ...interface{})

func Infof

func Infof(ctx context.Context, format string, args ...interface{})

func Level

func Level() zapcore.Level

Level Get current log_level

func LevelFromContext

func LevelFromContext(ctx context.Context) zapcore.Level

LevelFromContext Gets the log_level from the context logger

func Logger

func Logger() *zap.SugaredLogger

Logger Get global logger

func New

func New(level zapcore.Level, options ...zap.Option) *zap.SugaredLogger

New create a new logger with specific log_leve & options

func NewWithSink

func NewWithSink(level zapcore.LevelEnabler, sink io.Writer, options ...zap.Option) *zap.SugaredLogger

func Panic

func Panic(ctx context.Context, args ...interface{})

func PanicKV

func PanicKV(ctx context.Context, message string, kvs ...interface{})

func Panicf

func Panicf(ctx context.Context, format string, args ...interface{})

func SetLevel

func SetLevel(l zapcore.Level)

SetLevel Set current log_level

func SetLogger

func SetLogger(l *zap.SugaredLogger)

SetLogger Set global logger (not thread safe)

func ToContext

func ToContext(ctx context.Context, l *zap.SugaredLogger) context.Context

ToContext Attaches a logger to context

Example
logger := Logger()

customLogger := logger.With(
	zap.String("component", "database"),
)

ctx := ToContext(context.Background(), customLogger)

loggerFromContext := FromContext(ctx)

// Будет содержать дополнительное поле component.
loggerFromContext.Info("some message")
Output:

func Warn

func Warn(ctx context.Context, args ...interface{})

func WarnKV

func WarnKV(ctx context.Context, message string, kvs ...interface{})

func Warnf

func Warnf(ctx context.Context, format string, args ...interface{})

func WithFields

func WithFields(ctx context.Context, fields ...zap.Field) context.Context

WithFields Adds fields to logger from context

Example
// context, containing logger
ctx := context.Background()
// new context with logger fields
ctx = WithFields(ctx,
	zap.String("kafka-topic", "my topic"),
	zap.Int32("kafka-partition", 1),
)

_ = ctx
Output:

func WithKV

func WithKV(ctx context.Context, key string, value any) context.Context

WithKV Adds KV pair to logger from context

Example
// context, containing logger
ctx := context.Background()
// new context with logger fields
ctx = WithKV(ctx, "my key", "my value")

_ = ctx
Output:

func WithLevel

func WithLevel(lvl zapcore.Level) zap.Option

WithLevel option that allows you to create a logger with a specified level from an existing one

Example
Logger().Desugar().WithOptions(WithLevel(zapcore.DebugLevel)).Sugar()
Output:

func WithName

func WithName(ctx context.Context, name string) context.Context

WithName Set a name for the logger

Example
// context, containing logger
ctx := context.Background()
ctx = WithName(ctx, "GetApples")    // -> "GetApples"
ctx = WithName(ctx, "AppleManager") // - > "GetApples.AppleManager"
ctx = WithName(ctx, "DB")           // -> "GetApples.AppleManager.DB"

_ = ctx
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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