logger

package module
v0.0.0-...-883c1fe Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2015 License: MIT Imports: 15 Imported by: 0

README

goji/logger

goji/logger provides request logging and ID generation middleware for Goji, a HTTP multiplexer written in Go.

The middleware functions include:

  • Logger - a request logger that logs the method, remote IP and execution time of the request to stdout. When outputting to a terminal, the output is colorized for readability.
  • RequestID generates a random base62 identifier for the request. When used before the Logger middleware, the RequestID is included in the log output.

Here's what it looks like:

Install It

$ go get github.com/goji/logger

Example

Here's an example of the logger.RequestID and logger.Logger middleware in action:

package main

import (
	"fmt"
	"log"
	"net/http"

	"golang.org/x/net/context"

	"github.com/goji/logger"
	"goji.io"
	"goji.io/pat"
)

func main() {
	mux := goji.NewMux()
    // Include logger.RequestID before logger.Logger, and the request ID will
    // automatically be added to the log output.
	mux.UseC(logger.RequestID)
	mux.UseC(logger.Logger)

	mux.HandleFuncC(pat.Get("/"), SomeHandler)

	log.Fatal(http.ListenAndServe("localhost:8000", mux))
}

func SomeHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Goji!\n")
}

Note: Provided you implement the goji.Handler interface in your own application, this middleware can be used beyond just Goji.

License

MIT licensed. See the LICENSE file for details.

Documentation

Index

Constants

View Source
const RequestIDKey = "reqID"

Key to use when setting the request ID.

Variables

This section is empty.

Functions

func GetReqID

func GetReqID(ctx context.Context) string

GetReqID returns a request ID from the given context if one is present. Returns the empty string if a request ID cannot be found.

func Logger

func Logger(h goji.Handler) goji.Handler

Logger is a middleware that logs the start and end of each request, along with some useful data about what was requested, what the response status was, and how long it took to return. When standard output is a TTY, Logger will print in color, otherwise it will print in black and white.

Logger prints a request ID if one is provided.

Logger has been designed explicitly to be Good Enough for use in small applications and for people just getting started with Goji. It is expected that applications will eventually outgrow this middleware and replace it with a custom request logger, such as one that produces machine-parseable output, outputs logs to a different service (e.g., syslog), or formats lines like those printed elsewhere in the application.

func RequestID

func RequestID(h goji.Handler) goji.Handler

RequestID is a middleware that injects a request ID into the context of each request. A request ID is a string of the form "host.example.com/random-0001", where "random" is a base62 random string that uniquely identifies this go process, and where the last number is an atomically incremented request counter.

Types

type WriterProxy

type WriterProxy interface {
	http.ResponseWriter
	// Status returns the HTTP status of the request, or 0 if one has not
	// yet been sent.
	Status() int
	// BytesWritten returns the total number of bytes sent to the client.
	BytesWritten() int
	// Tee causes the response body to be written to the given io.Writer in
	// addition to proxying the writes through. Only one io.Writer can be
	// tee'd to at once: setting a second one will overwrite the first.
	// Writes will be sent to the proxy before being written to this
	// io.Writer. It is illegal for the tee'd writer to be modified
	// concurrently with writes.
	Tee(io.Writer)
	// Unwrap returns the original proxied target.
	Unwrap() http.ResponseWriter
}

WriterProxy is a proxy around an http.ResponseWriter that allows you to hook into various parts of the response process.

Jump to

Keyboard shortcuts

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