Documentation
¶
Overview ¶
Package logs provides a thin wrapper around the zerolog logging library, enabling structured, context logging across an application.
It includes:
- Setting the global logging level.
- Extracting loggers from context, with fallback to the global logger.
- Attaching metadata (e.g. job name, correlation IDs) to loggers via context.
This package ensures consistent logging behavior and makes it easy to trace log output using structured fields like correlation IDs, even across goroutines or services.
Index ¶
- Constants
- func Ctx(ctx context.Context) *zerolog.Logger
- func SetupLogger(logLevel string) error
- func WithCorrelationID(ctx context.Context, correlationID string) context.Context
- func WithCtxField(ctx context.Context, key, value string) context.Context
- func WithJob(ctx context.Context, jobName string) context.Context
- func WithNewCorrelationID(ctx context.Context) context.Context
Constants ¶
const (
AllowedLogLevels string = "disabled|trace|debug|info|warn|error|fatal|panic"
)
Variables ¶
This section is empty.
Functions ¶
func Ctx ¶
Ctx retrieves the zerolog logger from the given context. If the context does not have a logger attached, the global logger is returned instead.
func SetupLogger ¶
SetupLogger configures the global log level for the zerolog logger.
It takes a string `logLevel` representing the desired logging level (e.g. "debug", "info", "warn").
Parameters:
- logLevel: a string indicating the desired log verbosity level.
Returns:
- error: if the log level cannot be parsed, an error is returned; otherwise, nil.
Example:
err := SetupLogger("info")
if err != nil {
log.Fatal(err)
}
func WithCorrelationID ¶
WithCorrelationID adds a correlation ID to the logger in the context and returns the updated context.
func WithCtxField ¶
WithCtxField adds a key-value pair to the logger in the given context and returns the updated context.
If the context does not contain a logger, a new logger with the key-value pair is created and attached to the context.
Parameters:
- ctx: the context to enrich with the key-value field.
- key: the field key to add to the logger.
- value: the field value to add to the logger.
Returns:
- context.Context: the updated context containing the enriched logger.
Example:
ctx = WithCtxField(ctx, "request_id", "abc123")
Types ¶
This section is empty.