Skip to content

jixwanwang/loghandler

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

loghandler

The default Go HTTP handler doesn't do any logging. This is a handler middleware package that logs to an io.Writer and StatsD (optionally).

go get github.com/Hebo/loghandler

loghandler is based on Gorilla's handler code, and adds support for StatsD and tracking request duration.

Example Output

199.9.252.10 - - [03/Feb/2014:16:32:41 -0800] "GET /validate HTTP/1.1" 200 239 (2424μs)
199.9.250.239 - - [03/Feb/2014:16:32:41 -0800] "GET /validate HTTP/1.1" 200 244 (2490μs)
199.9.252.10 - - [03/Feb/2014:16:32:41 -0800] "GET /validate HTTP/1.1" 200 235 (3024μs)

Getting Started

package main

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

        "github.com/Hebo/loghandler"
)

func main() {
        http.HandleFunc("/dowork", func(w http.ResponseWriter, r *http.Request) {
                // Set statsd stat name
                loghandler.SetStat(w, "do.work")
                time.Sleep(300 * time.Millisecond)
                fmt.Fprintf(w, "Hello!")
        })

        // Wrap default handler and serve that instead
        handler := loghandler.NewLoggingHandler(os.Stdout, nil, http.DefaultServeMux)

        if err := http.ListenAndServe(":8080", handler); err != nil {
                log.Fatalf("Could not start server: %s", err.Error())
        }
}

To add stats support, simply change the nil in NewLoggingHandler to something that implements StatsLogger.

So Easy!

About

HTTP handler middleware that does logging

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%