FAQ
Hello everyone,

I'm trying to print a log on every http request like this:
date [url] - httpstatus
e.g.: 2012/10/24 15:30:23 [127.0.0.1:56485] / - 200

And I construct my own http.Handler like this:

type WrapHTTPHandler struct {
m *http.Handler
}

func (h *WrapHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
{
h.m.ServeHTTP(w, r)
log.Printf("[%s] %s\n", r.RemoteAddr, r.URL)
}

But I couldn't find out how to get the http status code.

Seems the status code is written directly to the response body, and there
is no status code stored in http.Header, is it possible to get the status
code here?


Thanks,
Jackie

--

Search Discussions

  • James McKaskill at Oct 24, 2012 at 3:21 pm

    On Wed, Oct 24, 2012 at 9:41 AM, Jackie Li wrote:
    Seems the status code is written directly to the response body, and there is
    no status code stored in http.Header, is it possible to get the status code
    here?
    ResponseWriter is an interface so you can just wrap it. For example
    https://github.com/jmckaskill/krb-httpd/blob/master/proxy.go#L678

    -- James

    --
  • Jackie Li at Oct 24, 2012 at 4:27 pm
    Why I didn't think of that?

    Thanks a lot!
    On Wed, Oct 24, 2012 at 4:21 PM, James McKaskill wrote:
    On Wed, Oct 24, 2012 at 9:41 AM, Jackie Li wrote:
    Seems the status code is written directly to the response body, and there is
    no status code stored in http.Header, is it possible to get the status code
    here?
    ResponseWriter is an interface so you can just wrap it. For example
    https://github.com/jmckaskill/krb-httpd/blob/master/proxy.go#L678

    -- James


    --
    Jackie

    --
  • Aroman at Oct 24, 2012 at 4:54 pm
    See also: http://golang.org/pkg/net/http/httptest/#ResponseRecorder
    On Wednesday, October 24, 2012 9:28:07 AM UTC-7, Jackie wrote:

    Why I didn't think of that?

    Thanks a lot!

    On Wed, Oct 24, 2012 at 4:21 PM, James McKaskill <ja...@foobar.co.nz<javascript:>
    wrote:
    On Wed, Oct 24, 2012 at 9:41 AM, Jackie Li <jackie...@gmail.com<javascript:>>
    wrote:
    Seems the status code is written directly to the response body, and there is
    no status code stored in http.Header, is it possible to get the status code
    here?
    ResponseWriter is an interface so you can just wrap it. For example
    https://github.com/jmckaskill/krb-httpd/blob/master/proxy.go#L678

    -- James


    --
    Jackie
    --
  • Benjamin Ellis at Oct 24, 2012 at 5:46 pm
    Add something to wrap the ResponseWriter?

    type statusLoggingResponseWriter struct {
    status int
    http.ResponseWriter
    }

    func (w *statusLoggingResponseWriter) WriteHeader(code int) {
    w.status = code
    w.ResponseWriter.WriteHeader(code)
    }

    type WrapHTTPHandler struct {
    m *http.Handler
    }

    func (h *WrapHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
    {
    myW := StatusLoggingResponseWriter{-1, w}
    h.m.ServeHTTP(myW, r)
    log.Printf("[%s] %s %d\n", r.RemoteAddr, r.URL, w.status)
    }
    On Wednesday, October 24, 2012 10:42:38 AM UTC-4, Jackie wrote:

    Hello everyone,

    I'm trying to print a log on every http request like this:
    date [url] - httpstatus
    e.g.: 2012/10/24 15:30:23 [127.0.0.1:56485] / - 200

    And I construct my own http.Handler like this:

    type WrapHTTPHandler struct {
    m *http.Handler
    }

    func (h *WrapHTTPHandler) ServeHTTP(w http.ResponseWriter, r
    *http.Request) {
    h.m.ServeHTTP(w, r)
    log.Printf("[%s] %s\n", r.RemoteAddr, r.URL)
    }

    But I couldn't find out how to get the http status code.

    Seems the status code is written directly to the response body, and there
    is no status code stored in http.Header, is it possible to get the status
    code here?


    Thanks,
    Jackie
    --
  • Jackie Li at Oct 24, 2012 at 7:25 pm
    That's exactly what I'm doing!

    Thanks for the reply!
    On Wed, Oct 24, 2012 at 4:01 PM, wrote:

    Add something to wrap the ResponseWriter?

    type statusLoggingResponseWriter struct {
    status int
    http.ResponseWriter
    }

    func (w *statusLoggingResponseWriter) WriteHeader(code int) {
    w.status = code
    w.ResponseWriter.WriteHeader(code)
    }

    type WrapHTTPHandler struct {
    m *http.Handler
    }

    func (h *WrapHTTPHandler) ServeHTTP(w http.ResponseWriter, r
    *http.Request) {
    myW := StatusLoggingResponseWriter{-1, w}
    h.m.ServeHTTP(myW, r)
    log.Printf("[%s] %s %d\n", r.RemoteAddr, r.URL, w.status)
    }
    On Wednesday, October 24, 2012 10:42:38 AM UTC-4, Jackie wrote:

    Hello everyone,

    I'm trying to print a log on every http request like this:
    date [url] - httpstatus
    e.g.: 2012/10/24 15:30:23 [127.0.0.1:56485] / - 200

    And I construct my own http.Handler like this:

    type WrapHTTPHandler struct {
    m *http.Handler
    }

    func (h *WrapHTTPHandler) ServeHTTP(w http.ResponseWriter, r
    *http.Request) {
    h.m.ServeHTTP(w, r)
    log.Printf("[%s] %s\n", r.RemoteAddr, r.URL)
    }

    But I couldn't find out how to get the http status code.

    Seems the status code is written directly to the response body, and there
    is no status code stored in http.Header, is it possible to get the status
    code here?


    Thanks,
    Jackie
    --



    --
    Jackie

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 24, '12 at 2:42p
activeOct 24, '12 at 7:25p
posts6
users4
websitegolang.org

People

Translate

site design / logo © 2017 Grokbase