loggie

package module
v0.2.2 Latest Latest
Warning

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

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

README ΒΆ

loggie 🧠⚑️ β€” Context-Aware Logger for Go

Context-aware, pluggable logger for Go web applications.
No more passing logger through every function β€” just use context.Context.


πŸš€ What is loggie?

loggie helps you embed a structured logger inside context.Context, so you can log from any layer β€” service, repository, or handler β€” with a consistent trace_id, user_id, or any custom field.

It supports Zap (now), and is extensible to Logrus, Slog, and more.


✨ Features

βœ… Structured logging with context.Context
βœ… Auto-generated trace_id per request
βœ… Custom fields via loggie.WithCustomField()
βœ… Middleware for Fiber / Gin / Echo
βœ… Pluggable backends (Zap, Logrus, Slog, etc.)
βœ… Fallback logger included (safe in any context)
βœ… Ready for Fx lifecycle and timeout-aware context.WithTimeout


πŸ“¦ Installation

go get github.com/ditthkr/loggie

🧱 Architecture

               β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
               β”‚ context.Context    β”‚
               β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                        β”‚
                β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”
                β”‚ loggie.Logger  │◄── (interface)
                β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                        β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β–Ό               β–Ό                β–Ό
  ZapLogger        LogrusLogger       SlogLogger
(implemented)     (planned)          (planned)


πŸ”Œ Logger Interface

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

You can plug any logger backend by implementing this interface.


βš™οΈ Middleware Usage

Fiber + Zap (fully working)
import (
    "github.com/ditthkr/loggie"
    "github.com/ditthkr/loggie/middleware/fiberlog"
    "go.uber.org/zap"
    "github.com/gofiber/fiber/v3"
)

func main() {
    rawLogger, _ := zap.NewProduction()
    defer rawLogger.Sync()

    adapter := &loggie.ZapLogger{L: rawLogger}
    app := fiber.New()
    app.Use(fiberlog.Middleware(adapter))

    app.Get("/ping", func(c *fiber.Ctx) error {
        log := loggie.FromContext(c.UserContext())
        log.Info("Ping received", "path", c.Path())
        return c.SendString("pong")
    })

    app.Listen(":8080")
}

πŸ§ͺ Sample Log Output:

{
  "level": "info",
  "msg": "Ping received",
  "trace_id": "b4a3f5a0...",
  "path": "/ping"
}

✍️ Custom Fields (e.g. user_id)

ctx = loggie.WithCustomField(ctx, "user_id", 42)

log := loggie.FromContext(ctx)
log.Info("Order created")

πŸ“€ Output:

{
  "msg": "Order created",
  "trace_id": "abc-xyz",
  "user_id": 42
}

🧰 Utilities

Function Purpose
FromContext(ctx) Retrieves logger from context
WithLogger(ctx, logger) Injects a logger
WithTraceId(ctx) Adds trace_id to context
TraceId(ctx) Retrieves trace_id
WithCustomField(ctx, key, value) Adds any structured field
DefaultLogger() Returns no-op fallback logger

πŸ“ Available Middleware

Framework Import Path Function
Fiber github.com/ditthkr/loggie/middleware/fiberlog fiberlog.Middleware()
Gin github.com/ditthkr/loggie/middleware/ginlog ginlog.Middleware()
Echo github.com/ditthkr/loggie/middleware/echolog echolog.Middleware()

All middlewares are generic and accept any loggie.Logger.


πŸ”Œ Current and Planned Logger Adapters

Logger Package / Status
Zap βœ… loggie.ZapLogger
Logrus πŸ•“ In progress
Slog πŸ•“ Planned for Go 1.21+

πŸ§ͺ Testing & Fallbacks

Even without injecting a logger, loggie will still work with a safe no-op fallback:

log := loggie.FromContext(context.Background())
log.Info("This is safe even without a logger")

πŸ“ƒ License

MIT Β© 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 DefaultLogger ΒΆ added in v0.2.1

func DefaultLogger() Logger

DefaultLogger returns the internal fallback logger used when no logger is found. This logger does nothing and is safe to call anywhere.

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
middleware

Jump to

Keyboard shortcuts

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