Documentation
¶
Index ¶
- func Injection(ctx context.Context, logger Logger) (context.Context, string)
- func TraceId(ctx context.Context) string
- func WithCustomField(ctx context.Context, key string, value any) context.Context
- func WithLogger(ctx context.Context, logger Logger) context.Context
- func WithTraceId(ctx context.Context) (context.Context, string)
- type Logger
- type LogrusLogger
- type ZapLogger
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Injection ¶ added in v0.2.5
Injection injects logger and trace_id into a context. Use it inside any web framework middleware.
func TraceId ¶
TraceId returns the current trace Id from the context.
It checks the following, in order:
- If an OpenTelemetry span exists in the context and has a valid TraceId, it is returned.
- If a local trace Id was previously stored in context via WithTraceId(), it is returned.
- Otherwise, a new UUId is generated and returned.
func WithCustomField ¶
WithCustomField adds a custom key-value pair to the context. These fields will be automatically injected into log output.
func WithLogger ¶
WithLogger stores the given logger inside the context. It can be retrieved later using FromContext.
func WithTraceId ¶
WithTraceId returns a new context with a generated or existing trace Id, and the trace Id itself as a string.
It first checks if a trace Id exists in the context, otherwise it generates a new UUId. This function is compatible with both OpenTelemetry-based contexts and local fallback mode.
Types ¶
type Logger ¶
type Logger interface {
Info(msg string, fields ...any)
Error(msg string, fields ...any)
With(fields ...any) Logger
}
Logger is the main interface used for logging. It mimics a simplified version of structured loggers like Zap or Logrus.
func DefaultLogger ¶ added in v0.2.1
func DefaultLogger() Logger
DefaultLogger returns the internal fallback logger used when no logger is found. This logger does nothing and is safe to call anywhere.
func FromContext ¶
FromContext retrieves the logger from context. If no logger is found, it returns a default no-op logger.
type LogrusLogger ¶ added in v0.2.3
func (*LogrusLogger) Error ¶ added in v0.2.3
func (l *LogrusLogger) Error(msg string, fields ...any)
func (*LogrusLogger) Info ¶ added in v0.2.3
func (l *LogrusLogger) Info(msg string, fields ...any)
func (*LogrusLogger) With ¶ added in v0.2.3
func (l *LogrusLogger) With(fields ...any) Logger