logger

package
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 11 Imported by: 0

README

Logger

The Logger package provides a custom JSON logger with built-in observability features. It extends log/slog, a high-performance structured, that is part of the Go standard library.

The Logger follows the Open Telemetry Semantic Conventions, to ensure compatibility with observability standards.

  1. SpanLogger
  2. Environment Variables
  3. Examples

SpanLogger

You can use the logger idependently, however, combining with the monitoring/ package, the logger provides out-of-the-box the below extra features.

To enable the below features, you must first follow the steps to setup observability: monitoring/README.md.

Correlate the IDs with Spans

When logging within a traced request, the logger automatically extracts and attaches trace and span IDs to each log entry. This ensures that logs can be correlated with their respective traces, making it easier to analyze distributed requests and debug issues across services. By linking logs and traces, you gain deeper visibility into request flows and system behavior.

Inject log attributes to Spans

Developers often rely on logs as the primary source of truth when debugging. To enhance this, the logger automatically injects extra log attributes into the corresponding spans. This ensures that spans contain valuable contextual information, making it easier to analyze and debug issues by providing a more comprehensive view of the request flow.

Environment Variables

The logger accepts a config that reads values from Environment Variables. The below table contains all the supported Environment Variables for the logger:

Variable Name Description Default
LOG_LEVEL The log level. The accepted values can be one of (debug, info, warn, error) info

Examples

You can find examples for the logger:

Documentation

Overview

Package logger provides a simple logger interface for logging messages. It also provides an out of the box support with the Otel span. The logger is passing metadata to the span, so the logs can be easily correlated with the traces. Furthermore, the logger is recording the span as errored for error logs with a given error.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(ctx context.Context, message string, args ...interface{})

Debug logs a message at the debug level.

Any attributes passed as arguments are added to the log message in the group "metadata", and in the span that is retrieved from the given context.

func Error

func Error(ctx context.Context, message string, err error, args ...interface{})

Error logs a message at the error level.

Any attributes passed as arguments are added to the log message in the group "metadata", and in the span that is retrieved from the given context. Furthermore, if an error is passed as an argument, it is added to the log message in the attribute "error", and also sets the span as errored (if the a span cna be retrieved from the given context).

func Info

func Info(ctx context.Context, message string, args ...interface{})

Info logs a message at the info level.

Any attributes passed as arguments are added to the log message in the group "metadata", and in the span that is retrieved from the given context.

func InitLogger

func InitLogger()

InitLogger initializes the logger with the given configuration.

The logger is then selected as the default logger for the application.

func Warn

func Warn(ctx context.Context, message string, args ...interface{})

Warn logs a message at the warn level.

Any attributes passed as arguments are added to the log message in the group "metadata", and in the span that is retrieved from the given context.

Types

type Attribute

type Attribute struct {
	// contains filtered or unexported fields
}

Attribute is a wrapper around the log attributes

func NewAttribute

func NewAttribute() *Attribute

NewAttribute creates a new attribute

func (*Attribute) Get

func (a *Attribute) Get(ctx context.Context) []slog.Attr

Get returns the log attributes

func (*Attribute) WithError

func (a *Attribute) WithError(err error) *Attribute

WithError sets the error in the attribute

func (*Attribute) WithMetadata

func (a *Attribute) WithMetadata(args ...interface{}) *Attribute

WithMetadata sets the metadata in the attribute

func (*Attribute) WithOutInjectingAttrsToSpan

func (a *Attribute) WithOutInjectingAttrsToSpan() *Attribute

WithOutInjectingAttrsToSpan disables injecting attributes to the span

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL