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 ¶
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") )
var (
ContextKeyLoggerFields = contextKey("loggerFields")
)
Predefined context keys for storing logger and fields in the context.
Functions ¶
func AddFieldsToContext ¶
AddFieldsToContext associates an array of fields with a context.
func AddLoggerToContex ¶
AddLoggerToContex associates a logger with a context.
func GetFieldsFromContext ¶
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.
type MutableFields ¶
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.