DEV Community

GitHubOpenSource
GitHubOpenSource

Posted on

Supercharge Your Go Logging with slog-context: Contextual Logging Made Easy

Quick Summary: πŸ“

The slog-context Go library provides utilities for integrating structured logging (slog) with Go's context package. It allows adding and retrieving loggers from contexts, adding attributes to contexts for automatic inclusion in log lines, and extracting custom context values like OpenTelemetry TraceIDs for logging.

Key Takeaways: πŸ’‘

  • βœ… Effortlessly integrate structured logging with context.

  • βœ… Store loggers directly in the context for easy access.

  • βœ… Automatically add contextual attributes to log lines without modifying every logging call.

  • βœ… Seamless integration with OpenTelemetry for improved observability.

  • βœ… Supports both slog and logr logging interfaces for maximum flexibility.

Project Statistics: πŸ“Š

  • ⭐ Stars: 138
  • 🍴 Forks: 6
  • ❗ Open Issues: 1

Tech Stack: πŸ’»

  • βœ… Go

Tired of wrestling with inconsistent logging in your Go applications? Wish you could effortlessly add contextual information to your logs, regardless of where they're generated? Then you absolutely need to check out slog-context! This fantastic Go library solves a common pain point for developers: seamlessly integrating structured logging with the power of context. Imagine a world where tracing requests across multiple services is as simple as adding a few lines of code. That's the promise of slog-context.

At its core, slog-context provides two main workflows to enhance your logging experience. First, it allows you to store your logger directly within the context. This means you can easily access and use the logger throughout your application, without passing it explicitly between functions. No more tedious parameter passing or global logger variables! The library provides helpful wrapper functions like With, Info, Error, and more, simplifying the logging process.

The second, equally powerful workflow involves adding attributes to your context. These attributes are then automatically picked up by slog-context's handler and added to your log lines. This is incredibly useful for adding contextual data like request IDs, user IDs, or even OpenTelemetry tracing information. Best of all, you don't even need to modify every logging call in your code – the attributes are automatically appended or prepended to your log entries.

But the magic doesn't stop there. slog-context offers seamless integration with OpenTelemetry (OTEL). Through the slogotel package, you can automatically extract trace and span IDs from your context and add them to your logs. This immediately improves your observability and makes debugging distributed systems a breeze. Moreover, it integrates with sloghttp to automatically add attributes from HTTP middleware, giving you a complete picture of your requests.

slog-context supports both the standard library's slog and the popular logr logging interfaces. This means you can easily integrate it into existing projects without major refactoring. Whether you're a die-hard slog fan or a logr enthusiast, this library has you covered. The automatic conversion between slog.Logger and logr.Logger ensures smooth interoperability across your entire stack.

The benefits are clear: improved code readability, simplified logging, enhanced debugging, and seamless integration with popular observability tools. slog-context streamlines your workflow, saving you time and effort. It makes your logging more consistent, accurate, and informative. Say goodbye to messy, hard-to-debug logs and hello to clean, contextualized logging that makes sense!

Learn More: πŸ”—

View the Project on GitHub


🌟 Stay Connected with GitHub Open Source!

πŸ“± Join us on Telegram

Get daily updates on the best open-source projects

GitHub Open Source

πŸ‘₯ Follow us on Facebook

Connect with our community and never miss a discovery

GitHub Open Source

Top comments (0)