http_logger

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2024 License: MIT Imports: 3 Imported by: 0

README

http-logger

Simple HTTP request/response logger for net/http package

Documentation

Overview

Simple package to log http requests and response info Meant for use with net/http package Built on top of go.uber.org/zap

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithLogging

func WithLogging(handler http.Handler, logger *zap.Logger) http.Handler

WithLogging wraps http.Handler with logging middleware It logs request method, path, remote address, response status and latency in nanoseconds See ExampleWithLogging for usage

Example
// Create a zap test logger
logger, _ := zap.NewProduction()
defer logger.Sync()

http.HandleFunc("/api/data", func(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusCreated)
	w.Write([]byte("Success!"))
})

// Wrap the test handler with our logging middleware
http.ListenAndServe(":8080", http_logger.WithLogging(http.DefaultServeMux, logger))
Output:

Types

type ResponseWriterWithLogging

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

func (*ResponseWriterWithLogging) Write

func (w *ResponseWriterWithLogging) Write(b []byte) (int, error)

func (*ResponseWriterWithLogging) WriteHeader

func (w *ResponseWriterWithLogging) WriteHeader(status int)

WriteHeader wraps original http.ResponseWriter WriteHeader method to store status code

Jump to

Keyboard shortcuts

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