httplog

package module
v0.0.0-...-348ce1c Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2025 License: MIT Imports: 10 Imported by: 0

README

Golang HTTP Logging Utilities

Provides several utilities for logging http requests and responses.

This package isn't well tested, and could benefit from several convenience features.

Particularly, I think it would be nice to have:

  • Decompressing based on the Content-Encoding header so that it's readable.
  • Formatting binary data as a hex dump.
  • Reformatting JSON content with indentation and newlines to make it more readable.
  • Stylizing text, to better distinguish headers keys, values, and content bodies.

I don't recommend holding your breath for any of that to happen.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(prefix string, err error)

Error logs an error message.

Currently, the only reason you'd use this would be if http.Client or http.Transport returned an error instead of a response.

func Handler

func Handler(w http.ResponseWriter, req *http.Request, next http.Handler)

Handler invokes the next http.Handler, logging both the request and the generated response.

func Middleware

func Middleware(next http.Handler) http.Handler

Middleware wraps the Handler function, turning it into http middleware.

func NewRequest

func NewRequest(ctx context.Context, method, url string, r io.Reader) (req *http.Request, recvPrefix string, err error)

NewRequest is semantically equivalent to http.NewRequest. The request will be logged immediately, and if http.NewRequestWithContext returns an error, that will be logged as well.

If this function does not return an error, it will also return a prefix that can be used with the Response or Error functions.

func Response

func Response(prefix string, resp *http.Response) *http.Response

Response immediately logs the status line and headers for the response, and then replaces its Body field with a proxy object that will log the body as it's read, and then print any trailers once EOF is reached.

func SetLogOutput

func SetLogOutput(w io.Writer)

Types

This section is empty.

Source Files

  • httplog.go

Jump to

Keyboard shortcuts

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