logger

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

README

Logger

logger is a native, highly-optimized wrapper around Go's standard library log/slog. It provides advanced capabilities like zero-allocation PII Redaction, automatic Trace ID propagation, and HTTP Telemetry middleware without forcing developers to learn a proprietary logging API.

By strictly utilizing slog.JSONHandler, the output is natively structured for seamless ingestion into any log aggregation platform or visualization tool.

Features

  • Native slog Support: Operates purely as a standard slog.Handler.
  • PII Redaction: Intercepts and masks sensitive keys (e.g. password, credit_card) with zero-allocation.
  • Trace ID Context Propagation: Automatically extracts and injects Trace IDs into deeply nested logs.
  • HTTP Middleware: Drop-in net/http telemetry middleware that logs request/response durations and status codes.

Installation

go get github.com/kishan-thanki/logger

Quick Start

For a complete, runnable application demonstrating the logger, see the Examples Directory.

Initialize the handler and set it as your global Go logger:

package main

import (
	"log/slog"
	"net/http"

	"github.com/kishan-thanki/logger"
	"github.com/kishan-thanki/logger/middleware"
)

func main() {
	// 1. Initialize the Logger Toolkit
	handler := logger.New(
		logger.WithLevel("INFO"),
		logger.WithTraceID(true),
		logger.WithRedaction("password", "token", "credit_card"),
	)

	// 2. Set it as the standard Go Logger
	slog.SetDefault(handler)

	// 3. (Optional) Wrap your HTTP Router with the Telemetry Middleware
	mux := http.NewServeMux()
	mux.HandleFunc("/api/login", func(w http.ResponseWriter, r *http.Request) {
		// Log messages natively. "password" is automatically redacted!
		slog.InfoContext(r.Context(), "Processing login request",
			slog.String("username", "admin"),
			slog.String("password", "super_secret"),
		)
	})

	loggedMux := middleware.HTTP(mux)
	http.ListenAndServe(":8080", loggedMux)
}

Advanced Usage

Context Injection (Trace IDs)

When executing background jobs or deep database calls, you can inject a Trace ID into the context. Any slog.InfoContext call will automatically extract and attach it to the JSON output.

ctx := logger.InjectTraceID(context.Background(), "trace-1234")
slog.ErrorContext(ctx, "Database connection failed")
// Output: {"level":"ERROR","msg":"Database connection failed","trace_id":"trace-1234"}

License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractTraceID

func ExtractTraceID(ctx context.Context) string

func InjectTraceID

func InjectTraceID(ctx context.Context, traceID string) context.Context

func New

func New(opts ...Option) *slog.Logger

New creates a new highly-optimized slog.Logger configured for production usage.

Types

type Config

type Config struct {
	Level      *slog.LevelVar
	RedactKeys []string
	TraceID    bool
	Source     bool
	Output     io.Writer
}

type Option

type Option func(*Config)

func WithDynamicLevel

func WithDynamicLevel(lvl *slog.LevelVar) Option

func WithLevel

func WithLevel(level string) Option

func WithOutput

func WithOutput(w io.Writer) Option

func WithRedaction

func WithRedaction(keys ...string) Option

func WithSource

func WithSource(enabled bool) Option

func WithTraceID

func WithTraceID(enabled bool) Option

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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