logger

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package logger provides a structured logging utility built on top of zerolog, designed specifically for AWS Lambda workloads.

The logger automatically:

  • Configures log level from environment variables
  • Emits JSON structured logs
  • Enriches logs with AWS Lambda context (request ID, ARN, memory, etc.)
  • Supports contextual and formatted logging

Log Level Configuration

The log level is controlled using the environment variable:

POWERTOOLS_LOG_LEVEL

Supported values:

ERROR, WARN, INFO, DEBUG, TRACE

If the variable is not set, the default log level is DEBUG.

AWS Lambda Context Injection

When running inside AWS Lambda, the logger can inject runtime metadata from the Lambda execution context:

  • function_name
  • function_memory_size
  • function_arn
  • function_request_id

This is achieved by calling InjectContext with the Lambda context.

Usage

Basic usage:

log := logger.New(types.LogConfig{})
log.Info("service started")

Injecting Lambda context:

func handler(ctx context.Context) {
    log := logger.New(types.LogConfig{})
    log.InjectContext(ctx)
    log.Info("request received")
}

Structured Properties

Static key-value properties can be attached to every log entry using LogConfig.Properties:

log := logger.New(types.LogConfig{
    Properties: map[string]string{
        "service": "orders",
        "env":     "prod",
    },
})

Output

Logs are emitted in JSON format and written to stdout by default, making them compatible with AWS CloudWatch Logs and log aggregators.

This package is inspired by AWS Powertools logging patterns and follows best practices for observability in serverless environments.

Index

Constants

This section is empty.

Variables

View Source
var LogMapper = map[string]zerolog.Level{
	"ERROR": zerolog.ErrorLevel,
	"WARN":  zerolog.WarnLevel,
	"INFO":  zerolog.InfoLevel,
	"DEBUG": zerolog.DebugLevel,
	"TRACE": zerolog.TraceLevel,
}

Functions

This section is empty.

Types

type Logger

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

func New

func New(logConfig types.LogConfig) *Logger

func (*Logger) Debug

func (log *Logger) Debug(message string, args ...any)

func (*Logger) Error

func (log *Logger) Error(message string, args ...any)

func (*Logger) Info

func (log *Logger) Info(message string, args ...any)

func (*Logger) InjectContext

func (log *Logger) InjectContext(ctx context.Context)

func (*Logger) Trace

func (log *Logger) Trace(message string, args ...any)

func (*Logger) Warn

func (log *Logger) Warn(message string, args ...any)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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