logger

package module
v0.0.0-...-008a4be 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: 8 Imported by: 0

README

Description

logger is a http request logger for the pressly/chi go HTTP router.

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/logger

Usage

package main

import (
    "net/http"

    "github.com/pressly/chi"
    "github.com/vma/logger"
)

func main() {
    r := chi.NewRouter()

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

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

func CombinedLogger

func CombinedLogger(out io.Writer) func(next http.Handler) http.Handler

CombinedLogger and CommonLogger are middlewares that log to stderr 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) func(next http.Handler) http.Handler

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

Types

This section is empty.

Jump to

Keyboard shortcuts

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