context

package
v0.0.0-...-5dd924d Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package context manages the service context. It provides facilities to add objects to the context and retrieve them, ensuring thread safety. This context is particularly useful for adding logging capabilities and custom fields relevant to service operation.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrLoggerNotFound is the error returned when the logger is not found in the context
	ErrLoggerNotFound = errors.New("logger not found in context")

	// ErrLoggerFieldsNotFound is the error returned when the logger fields are not found in the context
	ErrLoggerFieldsNotFound = errors.New("logger fields not found in context")
)
View Source
var (
	ContextKeyLoggerFields = contextKey("loggerFields")
)

Predefined context keys for storing logger and fields in the context.

Functions

func AddFieldsToContext

func AddFieldsToContext(ctx context.Context, fields []map[string]interface{}) context.Context

AddFieldsToContext associates an array of fields with a context.

func AddLoggerToContex

func AddLoggerToContex(ctx context.Context, logger Logger) context.Context

AddLoggerToContex associates a logger with a context.

func GetFieldsFromContext

func GetFieldsFromContext(ctx context.Context) []map[string]interface{}

GetFieldsFromContext retrieves the array of fields associated with a context. If the fields do not exist, it returns an empty slice.

Types

type Logger

type Logger interface {
	Info(ctx context.Context, msg string, fields ...map[string]interface{})
	Error(ctx context.Context, msg string, fields ...map[string]interface{})
}

Logger provides an interface for logging functionalities.

func GetLoggerFromContext

func GetLoggerFromContext(ctx context.Context) (Logger, error)

GetLoggerFromContext retrieves the logger associated with a context. If the logger does not exist, it returns an ErrLoggerNotFound error.

type MutableFields

type MutableFields struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

MutableFields represents a collection of fields that can be safely mutated across multiple goroutines.

func NewMutableFields

func NewMutableFields() *MutableFields

NewMutableFields initializes a new instance of MutableFields.

func (*MutableFields) AddField

func (mf *MutableFields) AddField(field map[string]interface{})

AddField safely adds a field to the MutableFields.

func (*MutableFields) GetFields

func (mf *MutableFields) GetFields() []map[string]interface{}

GetFields safely retrieves all fields from the MutableFields.

Jump to

Keyboard shortcuts

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