loggie

package module
v0.2.0 Latest Latest
Warning

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

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

README

loggie 🧠⚡️ — Context-Aware Logger for Go

Go Reference Go Report Card MIT License

Simple, structured, and traceable logging via context.Context — for modern Go backend services.


✨ Features

  • ✅ Context-aware logging (logger.FromContext(ctx))
  • 🧵 Auto generate trace_id per request
  • 🏷 Attach dynamic custom fields (user_id, order_id, etc.)
  • 🔌 Plug-and-play middleware for Gin, Fiber, and Echo
  • 🔧 Compatible with context.WithTimeout, WithCancel
  • 📦 Designed for use with Zap (Logrus / slog coming soon)

📦 Installation

go get github.com/ditthkr/loggie

🚀 Quick Start

🔗 Gin
r := gin.Default()
r.Use(middleware.GinZapMiddleware(zapLogger))

r.GET("/ping", func(c *gin.Context) {
	ctx := loggie.WithCustomField(c.Request.Context(), "user_id", 123)
	log := loggie.FromContext(ctx)
	log.Info("hello gin")
	c.JSON(200, gin.H{"msg": "pong"})
})

⚡ Fiber
app := fiber.New()
app.Use(middleware.FiberZapMiddleware(zapLogger))

app.Get("/ping", func(c fiber.Ctx) error {
	ctx := loggie.WithCustomField(c.Context(), "user_id", 123)
	log := loggie.FromContext(ctx)
	log.Info("hello fiber")
	return c.JSON(fiber.Map{"msg": "pong"})
})

🎯 Echo
e := echo.New()
e.Use(middleware.EchoZapMiddleware(zapLogger))

e.GET("/ping", func(c echo.Context) error {
	ctx := loggie.WithCustomField(c.Request().Context(), "role", "admin")
	log := loggie.FromContext(ctx)
	log.Info("hello echo")
	return c.JSON(200, map[string]string{"msg": "pong"})
})

🔍 Log Output (structured JSON)

{
  "level": "info",
  "msg": "hello gin",
  "trace_id": "123e4567-e89b-12d3-a456-426614174000",
  "user_id": 123
}

🧪 WithTimeout / Cancel

loggie works seamlessly with context.WithTimeout or context.WithCancel.

ctx := loggie.WithCustomField(r.Context(), "user_id", 999)
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()

log := loggie.FromContext(ctx)
log.Info("processing with timeout")

🧱 Custom Fields

Attach custom metadata across service layers without rewriting logger or trace logic.

ctx = loggie.WithCustomField(ctx, "order_id", "ORD-789")
log := loggie.FromContext(ctx)
log.Info("step 1") // order_id will appear

📌 Roadmap

  • Gin Middleware
  • Fiber v3 Middleware
  • Echo Middleware
  • Adapter for logrus
  • Adapter for slog (Go 1.21+)
  • Tracing integration (OpenTelemetry)
  • Unit tests & benchmarks

📄 License

MIT License © 2025 DITTHKR

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TraceId

func TraceId(ctx context.Context) string

TraceId extracts the trace_id string from the context. If none is found, returns "no-trace".

func WithCustomField

func WithCustomField(ctx context.Context, key string, value any) context.Context

WithCustomField adds a custom key-value pair to the context. These fields will be automatically injected into log output.

func WithLogger

func WithLogger(ctx context.Context, logger Logger) context.Context

WithLogger stores the given logger inside the context. It can be retrieved later using FromContext.

func WithTraceId

func WithTraceId(ctx context.Context) (context.Context, string)

WithTraceId adds a randomly generated trace_id to the context. It returns the new context and the trace ID.

Types

type Logger

type Logger interface {
	Info(msg string, fields ...any)
	Error(msg string, fields ...any)
	With(fields ...any) Logger
}

Logger is the main interface used for logging. It mimics a simplified version of structured loggers like Zap or Logrus.

func FromContext

func FromContext(ctx context.Context) Logger

FromContext retrieves the logger from context. If no logger is found, it returns a default no-op logger.

type ZapLogger

type ZapLogger struct {
	L *zap.Logger
}

func (*ZapLogger) Error

func (z *ZapLogger) Error(msg string, fields ...any)

func (*ZapLogger) Info

func (z *ZapLogger) Info(msg string, fields ...any)

func (*ZapLogger) With

func (z *ZapLogger) With(fields ...any) Logger

Directories

Path Synopsis
examples
echo command
fiber command
gin command

Jump to

Keyboard shortcuts

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