Skip to content

Latest commit

 

History

History
136 lines (88 loc) · 3.54 KB

File metadata and controls

136 lines (88 loc) · 3.54 KB

logs

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.

Index

Constants

const (
    AllowedLogLevels string = "disabled|trace|debug|info|warn|error|fatal|panic"
)

func Ctx

func Ctx(ctx context.Context) *zerolog.Logger

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

func SetupLogger(logLevel string) error

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

func WithCorrelationID(ctx context.Context, correlationID string) context.Context

WithCorrelationID adds a correlation ID to the logger in the context and returns the updated context.

func WithCtxField

func WithCtxField(ctx context.Context, key, value string) context.Context

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")

func WithJob

func WithJob(ctx context.Context, jobName string) context.Context

WithJob adds a job name field to the logger in the context and returns the updated context.

func WithNewCorrelationID

func WithNewCorrelationID(ctx context.Context) context.Context

WithNewCorrelationID generates a new correlation ID, adds it to the logger in the context, and returns the updated context.

Generated by gomarkdoc