Http Server and logging?

13,372 views
Skip to first unread message

Jens-Uwe Mager

unread,
Dec 27, 2011, 11:21:12 AM12/27/11
to golan...@googlegroups.com
It appears to be rather easy to serve some content using go, but I have not yet found an easy module to use that would give me a decent access log for my http service. Does anybody have a pointer to some code to use?

Krzysztof Kowalik

unread,
Dec 27, 2011, 11:25:23 AM12/27/11
to golan...@googlegroups.com
use the log package. you can also write custom http.ServeMux or wrapper for it which logs all requests

cheers,
nu7

2011/12/27 Jens-Uwe Mager <jum...@gmail.com>

Jens-Uwe Mager

unread,
Dec 27, 2011, 5:02:08 PM12/27/11
to golan...@googlegroups.com
The hint with the log package is easy to follow, but the custom http.ServeMux appears to be rather complicated. Is there any example out there already?
Message has been deleted

Kyle Lemons

unread,
Dec 27, 2011, 6:10:36 PM12/27/11
to golan...@googlegroups.com
On Tue, Dec 27, 2011 at 3:05 PM, D Smithson <d_smi...@rocketmail.com> wrote:
Here's an example: http://camlistore.org/code/?p=camlistore.git;a=blob;f=website/logging.go;hb=HEAD

I think a previous commenter mentioned ServeMux because it's an example of a http.Handler that delegates to other handlers, not because you want to do all the routing stuff as part of logging.

Or a 6-line wrapper:


package main

import (
    "fmt"
    "http"
    "log"
)

func Log(handler http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        log.Printf("%s %s %s", r.RemoteAddr, r.Method, r.URL)
handler.ServeHTTP(w, r)
    })
}

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:])
    })
    http.ListenAndServe(":80", Log(http.DefaultServeMux))
}

Krzysztof Kowalik

unread,
Dec 27, 2011, 6:29:34 PM12/27/11
to Kyle Lemons, golan...@googlegroups.com
Yeap, i like your way even more, i was doing something like this:

Jens-Uwe Mager

unread,
Dec 27, 2011, 6:31:34 PM12/27/11
to golan...@googlegroups.com
Thank you! That gives me a start.

Travis Keep

unread,
Apr 24, 2013, 9:22:19 AM4/24/13
to golan...@googlegroups.com
You can also try github.com/keep94/weblogs

ajays...@gmail.com

unread,
Feb 20, 2015, 2:29:55 AM2/20/15
to golan...@googlegroups.com

Ajay S

unread,
Feb 20, 2015, 2:31:39 AM2/20/15
to golan...@googlegroups.com
You can try this

https://github.com/ajays20078/go-http-logger

On Wednesday, April 24, 2013 at 6:52:19 PM UTC+5:30, Travis Keep wrote:
Reply all
Reply to author
Forward
0 new messages