• Stars
    star
    118
  • Rank 298,180 (Top 6 %)
  • Language
    Go
  • Created about 10 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Golang HTTP Logged transport. It wraps an HTTP Transport to help log metadata about HTTP API requests and responses.

Small golang 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"

	"github.com/ernesto-jimenez/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

Copyright (c) 2015 Ernesto Jimenez

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

gogen

A set of go packages and command line tools to generate Go code
Go
95
star
2

scraperboard

Golang library to easily scrape websites based on simple XML declarations
Go
90
star
3

goautomock

Automatically generate mocks for interfaces with go generate
Go
73
star
4

remote-js

Debug mobile web pages easier by having an small remote js console.
JavaScript
47
star
5

gocompatible

Find packages dependent on yours and run their tests to ensure you maintain backwards compatibility.
Go
20
star
6

node-twitter-stream-echo

This service connects to Twitter Streaming API and listens in UNIX socket /tmp/tweets.sock where it echoes all the content received from twitter.
JavaScript
11
star
7

charguess

Ruby gem wich builds and install libcharguess-ruby
C++
9
star
8

tweeting_streambot

This bot will track keywords using Twitter's Streaming API and re-tweet all the tweets it finds
Ruby
8
star
9

cpes15

Material from the workshop at Campus Party Valencia 2011
JavaScript
5
star
10

linktest

Quickly check for broken links and resources in your website.
Go
4
star
11

twenty-mobail

Publica fotos y cambia tu estado en Tuenti desde tu teléfono móvil
Ruby
4
star
12

wifi-telefonica

iPhone app which shows nearby WiFi access points from Telefónica
Objective-C
4
star
13

Planetakipaper.safariextension

Publish sticky posts to Instapaper
JavaScript
3
star
14

crawler

Easily crawl websites in Go.
Go
3
star
15

lock.me.uk

Shell
2
star
16

techinsiders-android

Java
2
star
17

gopherdex

A collection of gopher drawings
CSS
2
star
18

chatgpt-happy-harvest

Small web game completely created via ChatGPT
JavaScript
2
star
19

reconnect

Manage reconnection of golang connections
Go
1
star
20

markdownify

Go
1
star
21

charlacampus2010

Ejemplos de la charla sobre mobile dev en la Campus Party 2010
1
star
22

planmytrip

Yahoo Hack Europe 2013
Ruby
1
star
23

r13-team-72

Ruby
1
star
24

devfest-london-2016

Code from the talk at DevFest London 2016
Go
1
star