httplogger

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2017 License: BSD-2-Clause Imports: 7 Imported by: 2

README

Description

httplogger is a http request logger middleware with no external dependencies.

It comes with CommonLogger and CombinedLogger middlewares wich logs the requests in Apache CommonLoger and CombinedLogger format respectively.

These middlewares are ported from gorilla handlers

Installation

Installation can be done as usual:

$ go get github.com/vma/httplogger

Usage

package main

import (
    "net/http"

    "github.com/vma/httplogger"
)

func main() {
    logger := httplogger.CombinedLogger(os.Stdout)
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello, World!\n"))
    })
    http.ListenAndServe(":3000", logger(http.DefaultServeMux))
}

This code will produce a log line like this one:

::1 - - [02/Jan/2017:20:07:27 +0100] "GET / HTTP/1.1" 200 13 0.012ms "" "curl/7.51.0"

You wil notice the additional answer time field. The difference with the CommonLog format is that the latter does not contain the two last fields (referrer and user agent)

The full explanation of the log formats can be found at http://stackoverflow.com/a/9234855

The CombinedLogger and CommonLogger functions accept an io.Writer parameter so, you can easily write to a file instead of stdout.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Middleware

type Middleware func(http.Handler) http.Handler

func CombinedLogger

func CombinedLogger(out io.Writer) Middleware

CombinedLogger and CommonLogger are middlewares that log all http requests and response in Apache CommonLog and CombinedLog formats. They also log the answer delay in ms.

These loggers are directly ported from gorilla handlers https://github.com/gorilla/handlers

CombinedLogger returns a middleware that logs HTTP requests to `out` Writer in combined log format

func CommonLogger

func CommonLogger(out io.Writer) Middleware

CommonLogger returns a middleware that logs HTTP requests to `out` Writer in common log format

type ResponseWriterWrapper

type ResponseWriterWrapper struct {
	http.ResponseWriter
	StatusCode int
	Bytes      int
	// contains filtered or unexported fields
}

ResponseWriterWrapper wraps http.ResponseWriter to catch the status code and byte count. It implements the WriteHeader() and Write() interfaces from http.ResponseWriter.

func (*ResponseWriterWrapper) Write

func (w *ResponseWriterWrapper) Write(b []byte) (int, error)

func (*ResponseWriterWrapper) WriteHeader

func (w *ResponseWriterWrapper) WriteHeader(status int)

Jump to

Keyboard shortcuts

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