Logging with builtin http FileServer

1,279 views
Skip to first unread message

Jon Renner

unread,
Dec 18, 2013, 11:01:38 PM12/18/13
to golan...@googlegroups.com
How would I go about getting a log of traffic to the built-in http File Server?

This is what I have so far, yet it does not work (no fileserver):

Pranav Raja

unread,
Dec 19, 2013, 2:02:38 AM12/19/13
to golan...@googlegroups.com
Something like this (http://play.golang.org/p/6E6grsIts9) works for me locally.

Craig Mason-Jones

unread,
Dec 19, 2013, 10:07:09 PM12/19/13
to golan...@googlegroups.com
Gorilla's LoggingHandler or CombinedLoggingHandler makes this very easy.

http://www.gorillatoolkit.org/pkg/handlers

An example:

package main

import (
    "net/http"
    "os"
    "github.com/gorilla/handlers"
)

func main() {
    http.Handle("/", handlers.CombinedLoggingHandler(os.Stdout, http.FileServer(http.Dir("."))))
    http.ListenAndServe(":8123", nil)
}

All the best,
Craig
Reply all
Reply to author
Forward
0 new messages