Help us shape the future of Go! Take the 2026 Go Developer Survey

httplogger

package module
v0.0.0-...-6bbba63 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2017 License: ISC Imports: 3 Imported by: 1

README

HTTPLogger Documentation

A Golang library that logs HTTP requests using custom logic.

Examples

Standard library
package main

import (
	"log"
	"net/http"
	"strings"

	"github.com/reujab/httplogger"
)

func main() {
	http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
		res.Write([]byte("Hello, world!"))
	})
	http.HandleFunc("/404", func(res http.ResponseWriter, req *http.Request) {
		http.NotFound(res, req)
	})
	http.ListenAndServe(":8080", httplogger.Wrap(http.DefaultServeMux.ServeHTTP, func(req *httplogger.Request) {
		if !strings.HasPrefix(req.URL.Path, "/socket.io/") {
			log.Println(req.IP, req.Method, req.URL, req.Status, req.Time)
		}
	}))
}
Gorilla Mux
package main

import (
	"log"
	"net/http"
	"strings"

	"github.com/gorilla/mux"
	"github.com/reujab/httplogger"
)

func main() {
	router := mux.NewRouter()

	router.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
		res.Write([]byte("Hello, world!"))
	}).Methods("GET")
	http.ListenAndServe(":8080", httplogger.Wrap(router.ServeHTTP, func(req *httplogger.Request) {
		if !strings.HasPrefix(req.URL.Path, "/socket.io/") {
			log.Println(req.IP, req.Method, req.URL, req.Status, req.Time)
		}
	}))
}
Log
2017/06/25 14:47:49 [::1]:45616 GET / 200 5.721µs
2017/06/25 14:47:51 [::1]:45616 GET /404 404 48.588µs

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Wrap

func Wrap(handler http.HandlerFunc, callback func(*Request)) http.HandlerFunc

Types

type Request

type Request struct {
	Request  *http.Request
	Response http.ResponseWriter

	IP     string
	Method string
	URL    *url.URL
	Status int
	Size   int
	Time   time.Duration
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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