A Go net/http logger library with support for pluggable loggers (Fork of: https://github.com/ernesto-jimenez/httplogger)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
James Mills 33c3f1790d
Add README badges for Go Report Card and Go Reference
2 weeks ago
example Added support for Go111Modules and renamed package import 2 weeks ago
.gitignore Initial commit 7 years ago
LICENSE Added support for Go111Modules and renamed package import 2 weeks ago
LICENSE.old Added support for Go111Modules and renamed package import 2 weeks ago
README.md Add README badges for Go Report Card and Go Reference 2 weeks ago
go.mod Added support for Go111Modules and renamed package import 2 weeks ago
httplogger.go Fix doc 7 years ago

README.md

httplogger

Go Report Card Go Reference

Small Go library useful for logging API requests.

It wraps any http.Transport to log its requests and responses, including the duration time.

Usage

See example/example.go

package main

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

	"go.mills.io/httplogger"
)

func main() {
	client := http.Client{
		Transport: httplogger.NewLoggedTransport(http.DefaultTransport, newLogger()),
	}

	client.Get("http://google.com")
}

type httpLogger struct {
	log *log.Logger
}

func newLogger() *httpLogger {
	return &httpLogger{
		log: log.New(os.Stderr, "log - ", log.LstdFlags),
	}
}

func (l *httpLogger) LogRequest(req *http.Request) {
	l.log.Printf(
		"Request %s %s",
		req.Method,
		req.URL.String(),
	)
}

func (l *httpLogger) LogResponse(req *http.Request, res *http.Response, err error, duration time.Duration) {
	duration /= time.Millisecond
	if err != nil {
		l.log.Println(err)
	} else {
		l.log.Printf(
			"Response method=%s status=%d durationMs=%d %s",
			req.Method,
			res.StatusCode,
			duration,
			req.URL.String(),
		)
	}
}

Output:

$ go run example/example.go
log - 2014/08/17 02:19:19 Request GET http://google.com
log - 2014/08/17 02:19:19 Response method=GET status=302
durationMs=85 http://google.com
log - 2014/08/17 02:19:19 Request GET
http://www.google.co.uk/?gfe_rd=cr&ei=GwPwU4GtPMKo8we3koKwDg
log - 2014/08/17 02:19:20 Response method=GET status=200
durationMs=138
http://www.google.co.uk/?gfe_rd=cr&ei=GwPwU4GtPMKo8we3koKwDg

LICENSE

httplogger is licensed under the terms of the MIT License.

Previously based off of httplogger by Ernesto JimC)nez (See LICENSE.old)