middleware

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 7 Imported by: 0

README

middleware

import "github.com/brpaz/lib-go/middleware"

Index

func InjectLogger

func InjectLogger(logger *logging.Logger) func(http.Handler) http.Handler

InjectLogger returns middleware that stores logger in the request context. Downstream handlers retrieve it via [logging.FromContext]. Use this when you need context logger access without full request logging.

func RequestLogger

func RequestLogger(logger *logging.Logger, opts ...RequestLoggerOption) func(http.Handler) http.Handler

RequestLogger returns middleware that injects a request-scoped logger into the context and logs each request on completion with method, path, status, and duration. Downstream handlers retrieve the logger via [logging.FromContext].

type RequestLoggerOption

RequestLoggerOption configures RequestLogger.

type RequestLoggerOption func(*requestLoggerConfig)

func WithIgnorePaths
func WithIgnorePaths(paths ...string) RequestLoggerOption

WithIgnorePaths sets paths to skip logging entirely. A pattern ending in "*" matches by prefix (e.g. "/assets/*" matches "/assets/app.js"); any other pattern must match exactly (e.g. "/favicon.ico").

func WithRequestBodyLogging
func WithRequestBodyLogging(maxBytes int64) RequestLoggerOption

WithRequestBodyLogging enables logging of the request body up to maxBytes. The body is restored so downstream handlers still receive it. Use only in development — request bodies often contain sensitive data.

func WithResponseBodyLogging
func WithResponseBodyLogging(maxBytes int64) RequestLoggerOption

WithResponseBodyLogging enables logging of the response body up to maxBytes. Use only in development — response bodies can be large and may contain sensitive data.

Generated by gomarkdoc

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InjectLogger

func InjectLogger(logger *logging.Logger) func(http.Handler) http.Handler

InjectLogger returns middleware that stores logger in the request context. Downstream handlers retrieve it via logging.FromContext. Use this when you need context logger access without full request logging.

func RequestLogger

func RequestLogger(logger *logging.Logger, opts ...RequestLoggerOption) func(http.Handler) http.Handler

RequestLogger returns middleware that injects a request-scoped logger into the context and logs each request on completion with method, path, status, and duration. Downstream handlers retrieve the logger via logging.FromContext.

Types

type RequestLoggerOption

type RequestLoggerOption func(*requestLoggerConfig)

RequestLoggerOption configures RequestLogger.

func WithIgnorePaths

func WithIgnorePaths(paths ...string) RequestLoggerOption

WithIgnorePaths sets paths to skip logging entirely. A pattern ending in "*" matches by prefix (e.g. "/assets/*" matches "/assets/app.js"); any other pattern must match exactly (e.g. "/favicon.ico").

func WithRequestBodyLogging

func WithRequestBodyLogging(maxBytes int64) RequestLoggerOption

WithRequestBodyLogging enables logging of the request body up to maxBytes. The body is restored so downstream handlers still receive it. Use only in development — request bodies often contain sensitive data.

func WithResponseBodyLogging

func WithResponseBodyLogging(maxBytes int64) RequestLoggerOption

WithResponseBodyLogging enables logging of the response body up to maxBytes. Use only in development — response bodies can be large and may contain sensitive data.

Jump to

Keyboard shortcuts

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