loghttp

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2023 License: Apache-2.0 Imports: 5 Imported by: 4

Documentation

Overview

Package loghttp exposes a http.Handler that logs requests via log.Printf().

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler struct {
	http.Handler
	// contains filtered or unexported fields
}

Handler wraps a http.Handler and logs the request and response.

It handles Hijack() for websocket support.

Example
package main

import (
	"log"
	"net/http"
	"time"

	"github.com/maruel/serve-dir/loghttp"
)

func main() {
	// Serves the current directory over HTTP and logs all requests.
	log.SetFlags(log.Lmicroseconds)
	s := &http.Server{
		Addr:           ":6060",
		Handler:        &loghttp.Handler{Handler: http.FileServer(http.Dir("."))},
		ReadTimeout:    10. * time.Second,
		WriteTimeout:   24 * 60 * 60 * time.Second,
		MaxHeaderBytes: 256 * 1024 * 1024 * 1024,
	}
	log.Fatal(s.ListenAndServe())
}
Output:

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

It logs the handler's HTTP status code and the response size.

Jump to

Keyboard shortcuts

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