logger

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: MIT Imports: 9 Imported by: 0

README

logger

Structured logging with slog and Gin middleware.

Usage

import "github.com/adeptry-app/go-common/logger"

// From LOG_LEVEL / LOG_FORMAT / LOG_SOURCE environment variables
appLogger := logger.NewFromEnv("my-service")

// Or explicit configuration
appLogger = logger.New(logger.Config{
    Level:       "info",
    Format:      "json",
    ServiceName: "my-service",
    AddSource:   false,
})

// Use middleware
router.Use(logger.Recovery(appLogger))
router.Use(logger.RequestLogger(appLogger))

// Log messages
appLogger.Info("Server started", "port", 8080)
appLogger.Error("Database error", "error", err)

Log Levels

  • debug - Detailed debugging info
  • info - General operational info
  • warn - Warning conditions
  • error - Error conditions

Formats

  • json - Structured JSON (production)
  • text - Human-readable (development)

Documentation

Index

Constants

View Source
const (
	HeaderRequestID     = "X-Request-ID"
	HeaderCorrelationID = "X-Correlation-ID"
)

Header names carrying the tracing IDs across services.

Variables

This section is empty.

Functions

func AddCorrelationID

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

AddCorrelationID adds correlation ID to context

func AddRequestID

func AddRequestID(ctx context.Context, requestID string) context.Context

AddRequestID adds request ID to context

func FromContext

func FromContext(ctx context.Context, defaultLogger *slog.Logger) *slog.Logger

FromContext retrieves logger from context or returns the default logger

func GetCorrelationID

func GetCorrelationID(ctx context.Context) string

GetCorrelationID retrieves correlation ID from context

func GetLogger

func GetLogger(c *gin.Context) *slog.Logger

GetLogger retrieves the logger from Gin context

func GetRequestID

func GetRequestID(ctx context.Context) string

GetRequestID retrieves request ID from context

func New

func New(cfg Config) *slog.Logger

New creates a new structured logger with the given configuration

func NewFromEnv

func NewFromEnv(serviceName string) *slog.Logger

NewFromEnv creates a logger from the standard LOG_LEVEL, LOG_FORMAT, and LOG_SOURCE environment variables. A malformed LOG_SOURCE panics at startup.

func Recovery

func Recovery(logger *slog.Logger) gin.HandlerFunc

Recovery returns a Gin middleware that recovers from panics and logs them

func RequestLogger

func RequestLogger(logger *slog.Logger) gin.HandlerFunc

RequestLogger returns a Gin middleware that logs HTTP requests with structured logging

func WithContext

func WithContext(ctx context.Context, logger *slog.Logger) *slog.Logger

WithContext creates a logger with context values

Types

type Config

type Config struct {
	Level       string // debug, info, warn, error
	Format      string // json, text
	ServiceName string
	AddSource   bool // Add source file and line number
}

Config holds logger configuration

type ContextKey

type ContextKey string

ContextKey is the type for context keys

const (
	// RequestIDKey is the context key for request ID
	RequestIDKey ContextKey = "request_id"
	// CorrelationIDKey is the context key for correlation ID
	CorrelationIDKey ContextKey = "correlation_id"
)

Jump to

Keyboard shortcuts

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