import "github.com/greenbone/opensight-golang-libraries/pkg/logs"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.
- 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
const (
AllowedLogLevels string = "disabled|trace|debug|info|warn|error|fatal|panic"
)func Ctx(ctx context.Context) *zerolog.LoggerCtx 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(logLevel string) errorSetupLogger 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(ctx context.Context, correlationID string) context.ContextWithCorrelationID adds a correlation ID to the logger in the context and returns the updated context.
func WithCtxField(ctx context.Context, key, value string) context.ContextWithCtxField 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")
func WithJob(ctx context.Context, jobName string) context.ContextWithJob adds a job name field to the logger in the context and returns the updated context.
func WithNewCorrelationID(ctx context.Context) context.ContextWithNewCorrelationID generates a new correlation ID, adds it to the logger in the context, and returns the updated context.
Generated by gomarkdoc