logctx

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2022 License: MIT Imports: 7 Imported by: 4

README

logctx

Utility library for logging.

Usage

Adding a logger into a context.Context:

// ...
ctx = logctx.WithLogger(ctx, logger.With(zap.String("request_id", ulid.New().String())))
// Now the logger inside of `ctx` has `request_id` as field.
// ...

Extracting a logger from a context.Context:

// ...
logger := logctx.From(ctx)
// Now you can use logger as you wish.
// ...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug added in v0.2.0

func Debug(ctx context.Context, msg string, fields ...zap.Field)

Debug is a helper function to log a debug with the given context.

func Error added in v0.2.0

func Error(ctx context.Context, msg string, fields ...zap.Field)

Error is a helper function to log an error with the given context.

func Fatal added in v0.2.0

func Fatal(ctx context.Context, msg string, fields ...zap.Field)

Fatal is a helper function to log a fatal with the given context.

func From

func From(ctx context.Context) *zap.Logger

From return the zap.Logger from a context. If there is no configuration a default instance is returned.

func HttpResponse

func HttpResponse(key string, response *http.Response) zap.Field

HttpResponse creates a Object fields that will output the response Status, StatusCode, ContentLength and Body.

func HttpResponseWithOpts

func HttpResponseWithOpts(key string, response *http.Response, opts HttpResponseOptions) zap.Field

HttpResponseWithOpts creates a Object fields that will output the response Status, StatusCode, ContentLength. The body will not be part of the output.

func Info added in v0.2.0

func Info(ctx context.Context, msg string, fields ...zap.Field)

Info is a helper function to log an info with the given context.

func Initialize

func Initialize(opts ...Option) error

Initialize intiializes the default configuration for the logger.

func Panic added in v0.2.0

func Panic(ctx context.Context, msg string, fields ...zap.Field)

Panic is a helper function to log a panic with the given context.

func Warn added in v0.2.0

func Warn(ctx context.Context, msg string, fields ...zap.Field)

Warn is a helper function to log a warning with the given context.

func WithFields

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

WithFields returns a new context with the logger that includes the given fields.

func WithLogger

func WithLogger(ctx context.Context, logger *zap.Logger) context.Context

WithLogger add the given zap.Logger and returning the new context.Context.

Types

type HttpResponseOptions

type HttpResponseOptions struct {
	LimitBody int64
	SkipBody  bool
}

type Option

type Option = func(*options)

func WithContextKey

func WithContextKey(key string) Option

WithContextKey is the option that initializes the default context key.

func WithDefaultLogger

func WithDefaultLogger(logger *zap.Logger) Option

WithDefaultLogger is the option that initializes the default logger instance.

Jump to

Keyboard shortcuts

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