httplog

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2020 License: MIT Imports: 5 Imported by: 10

README

httplog

Package httplog provides a standard http.RoundTripper transport that can be used with standard HTTP clients to log the raw (outgoing) HTTP request and response.

Example

// _example/main.go
package main

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

	"github.com/kenshaw/httplog"
)

func main() {
	cl := &http.Client{
		Transport: httplog.NewPrefixedRoundTripLogger(nil, os.Stdout),
	}
	req, err := http.NewRequest("GET", "https://google.com", nil)
	if err != nil {
		log.Fatal(err)
	}
	res, err := cl.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer res.Body.Close()
}

Documentation

Overview

Package httplog provides a standard http.RoundTripper transport that can be used with standard HTTP clients to log the raw (outgoing) HTTP request and response.

Example:

// _example/main.go
package main

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

	"github.com/kenshaw/httplog"
)

func main() {
	cl := &http.Client{
		Transport: httplog.NewPrefixedRoundTripLogger(nil, os.Stdout),
	}
	req, err := http.NewRequest("GET", "https://google.com", nil)
	if err != nil {
		log.Fatal(err)
	}
	res, err := cl.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer res.Body.Close()
}

NewPrefixedTripLogger provides a convenient wrapper around using the standard library's io.Writer, and standard output func signatures for fmt.Printf and log.Printf.

Index

Constants

This section is empty.

Variables

View Source
var DefaultTransport = http.DefaultTransport

DefaultTransport is the default transport used by the HTTP logger.

Functions

This section is empty.

Types

type RoundTripLogger

type RoundTripLogger struct {
	// contains filtered or unexported fields
}

RoundTripLogger provides a standard http.RoundTripper transport that can be used with standard HTTP clients to log the raw (outgoing) HTTP request and response.

func NewPrefixedRoundTripLogger

func NewPrefixedRoundTripLogger(transport http.RoundTripper, logger interface{}) *RoundTripLogger

NewPrefixedRoundTripLogger creates a new HTTP transport that logs the raw (outgoing) HTTP request and response to the provided logger.

Prefixes requests and responses with "-> " and "<-", respectively. Adds an additional blank line ("\n\n") to the output of requests and responses.

Valid types for logger:

io.Writer
func(string, ...interface{}) (int, error) // fmt.Printf
func(string, ...interface{}) // log.Printf

Note: will panic() when an unknown logger type is passed.

func NewRoundTripLogger

func NewRoundTripLogger(transport http.RoundTripper, reqf, resf func([]byte)) *RoundTripLogger

NewRoundTripLogger creates a new HTTP transport that logs the raw (outgoing) HTTP request and response.

func (*RoundTripLogger) RoundTrip

func (l *RoundTripLogger) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip satisfies the http.RoundTripper interface.

Directories

Path Synopsis
_example/main.go
_example/main.go

Jump to

Keyboard shortcuts

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