contextlogger

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2025 License: MIT Imports: 3 Imported by: 0

README

Go's Context Logger

Releases GitHub commit activity GitHub License Go Reference

ContextLogger implements contextual logging using slog by embedding itself into context.Context's. Making it effortless to maintain consistent log context throughout your application's call stack.

It has the following features:

  • Context-Embedded Logging - Logger travels with your context.Context, eliminating manual logger passing
  • slog Familiarity - Built on top of Go's standard log/slog package with a familiar API
  • Structured Logging - Support for key-value pairs and nested groups for organized log output
  • Highly Configurable - Customize log levels, handlers, formatters, and output destinations
  • Middleware Ready - Built-in HTTP middleware for automatic request logging, and more coming soon!
  • Multiple Handlers - Support for JSON, text, and custom log formats. Basically, everything that slog supports
  • Performance Focused - Minimal overhead with efficient context propagation

Demo

Why use Context Logger?

Traditional logging often requires passing a logger instance through your entire call stack or using a global logger that lacks request-specific context. Context Logger solves this by:

  1. Embedding the logger in context - Access your logger anywhere you have a context.Context
  2. Automatic context accumulation - Build up contextual information as requests flow through layers
  3. Cleaner function signatures - No need to pass logger as a parameter to every function

Installation

Use go get:

go get github.com/pablovarg/contextlogger

Then import the contextlogger package into your code:

import "github.com/pablovarg/contextlogger"

Usage

ctx := context.Background()

// Embed a logger into the context
ctx = contextlogger.EmbedLogger(ctx)

// Update the stored context
contextlogger.UpdateContext(ctx, "id", user.ID, "email", user.Email)

// Create groups for different parts of your code,
// and update the context as above inside this group
processUser(contextlogger.WithGroup(ctx, "userProcess"))

// To print the whole accumulated context
contextlogger.LogWithContext(ctx, slog.LevelInfo, "user information")
Examples

Contributing

This project is currently open to contributions from the community, some things you can work on:

  • Support for different logging libraries
  • Include a middleware for your favorite framework
  • Anything else you can imagine

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EmbedLogger

func EmbedLogger(
	ctx context.Context,
	opts ...buckets.BucketConfigurator,
) context.Context

EmbedLogger embeds a contextual logger into the passed context.Context. Returns a new context.Context with the embeded logger.

func LogWithContext

func LogWithContext(ctx context.Context, level slog.Level, msg string)

LogWithContext logs everything stored into the context by calls to UpdateContext

func UpdateContext

func UpdateContext(ctx context.Context, attrs ...any)

UpdateContext updates a series of [key, values] from the current logging context the input is the same as slog args, i.e.: pairs of [string, any] or slog.Attr's. Malformed inputs that don't conform to this, will be assigned to a BADKEY key.

func WithGroup

func WithGroup(
	ctx context.Context,
	name string,
	opts ...buckets.BucketConfigurator,
) context.Context

WithGroup creates a group with the given name on the current group give by the context.Context passed into the function. Returns inner context.Context created for the new group.

Types

This section is empty.

Directories

Path Synopsis
basic command
custom_logger command
groups command
http_middleware command
middlewares

Jump to

Keyboard shortcuts

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