logger

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package logger provides HTTP request logging middleware for the rig framework.

The logger middleware records information about each HTTP request including:

  • HTTP method and path
  • Response status code
  • Request latency
  • Client IP address
  • Request ID (if available from requestid middleware)

Basic Usage

r := rig.New()
r.Use(logger.New())

With Custom Configuration

r.Use(logger.New(logger.Config{
    Format:    logger.FormatJSON,
    SkipPaths: []string{"/health", "/ready"},
    Output:    os.Stderr,
}))

Output Formats

The middleware supports two output formats:

  • FormatText (default): Human-readable text format
  • FormatJSON: Structured JSON format for log aggregation systems

Status Code Tracking

Note: Due to the design of the rig framework, the logger cannot capture the exact HTTP status code. It infers the status based on whether an error was returned from the handler:

  • No error: 200 OK
  • Error returned: 500 Internal Server Error

For more accurate status tracking, consider using a custom response writer wrapper in your application.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(config ...Config) rig.MiddlewareFunc

New creates a new logger middleware with the given configuration.

The middleware logs each request after it completes, including:

  • Timestamp
  • HTTP status code (inferred from error)
  • Request latency
  • Client IP address
  • HTTP method and path
  • Request ID (if requestid middleware is used)

Types

type Config

type Config struct {
	// Format specifies the log output format.
	// Default: FormatText
	Format Format

	// Output is the writer where logs will be written.
	// Default: os.Stdout
	Output io.Writer

	// SkipPaths is a list of URL paths that should not be logged.
	// Useful for health check endpoints that are called frequently.
	// Example: []string{"/health", "/ready", "/metrics"}
	SkipPaths []string

	// TimeFormat specifies the format for timestamps.
	// Default: "2006-01-02 15:04:05"
	TimeFormat string
}

Config defines the configuration for the logger middleware.

type Format

type Format string

Format represents the log output format.

const (
	// FormatText outputs logs in human-readable text format.
	// Example: 2024-01-15 10:30:45 | 200 |   1.234ms | 192.168.1.1 | GET /api/users
	FormatText Format = "text"

	// FormatJSON outputs logs in structured JSON format.
	// Useful for log aggregation systems like ELK, Splunk, or CloudWatch.
	FormatJSON Format = "json"
)

type LogEntry

type LogEntry struct {
	Timestamp string `json:"timestamp"`
	Status    int    `json:"status"`
	Latency   string `json:"latency"`
	LatencyMs int64  `json:"latency_ms"`
	ClientIP  string `json:"client_ip"`
	Method    string `json:"method"`
	Path      string `json:"path"`
	RequestID string `json:"request_id,omitempty"`
	Error     string `json:"error,omitempty"`
	UserAgent string `json:"user_agent,omitempty"`
}

LogEntry represents a single log entry in JSON format.

Jump to

Keyboard shortcuts

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