httplogger

package module
Version: v0.0.0-...-33c3f17 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2021 License: MIT Imports: 3 Imported by: 0

README

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)

Documentation

Index

Constants

This section is empty.

Variables

DefaultLoggedTransport wraps http.DefaultTransport to log using DefaultLogger

Functions

func NewLoggedTransport

func NewLoggedTransport(rt http.RoundTripper, log HTTPLogger) http.RoundTripper

NewLoggedTransport takes an http.RoundTripper and returns a new one that logs requests and responses

Types

type DefaultLogger

type DefaultLogger struct {
}

DefaultLogger is an http logger that will use the standard logger in the log package to provide basic information about http responses

func (DefaultLogger) LogRequest

func (dl DefaultLogger) LogRequest(*http.Request)

LogRequest doens't do anything since we'll be logging replies only

func (DefaultLogger) LogResponse

func (dl DefaultLogger) LogResponse(req *http.Request, res *http.Response, err error, duration time.Duration)

LogResponse logs path, host, status code and duration in milliseconds

type HTTPLogger

type HTTPLogger interface {
	LogRequest(*http.Request)
	LogResponse(*http.Request, *http.Response, error, time.Duration)
}

HTTPLogger defines the interface to log http request and responses

Source Files

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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