2018-03-28 18:49:41 +00:00
|
|
|
// +build !binary_log
|
|
|
|
|
2017-05-20 08:48:00 +00:00
|
|
|
package hlog_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2017-05-20 09:22:57 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"net/http/httptest"
|
2017-05-20 08:48:00 +00:00
|
|
|
|
2022-11-03 15:18:09 +00:00
|
|
|
"tuxpa.in/a/zlog"
|
|
|
|
"tuxpa.in/a/zlog/hlog"
|
2017-05-20 08:48:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// fake alice to avoid dep
|
2017-05-20 09:22:57 +00:00
|
|
|
type middleware func(http.Handler) http.Handler
|
|
|
|
type alice struct {
|
|
|
|
m []middleware
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a alice) Append(m middleware) alice {
|
|
|
|
a.m = append(a.m, m)
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
func (a alice) Then(h http.Handler) http.Handler {
|
|
|
|
for i := range a.m {
|
|
|
|
h = a.m[len(a.m)-1-i](h)
|
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
2017-05-20 08:48:00 +00:00
|
|
|
|
2017-05-20 09:22:57 +00:00
|
|
|
func init() {
|
2022-03-20 19:19:42 +00:00
|
|
|
zlog.TimestampFunc = func() time.Time {
|
2017-05-20 09:22:57 +00:00
|
|
|
return time.Date(2001, time.February, 3, 4, 5, 6, 7, time.UTC)
|
|
|
|
}
|
|
|
|
}
|
2017-05-20 08:48:00 +00:00
|
|
|
|
|
|
|
func Example_handler() {
|
2022-03-20 19:19:42 +00:00
|
|
|
log := zlog.New(os.Stdout).With().
|
2017-05-20 09:22:57 +00:00
|
|
|
Timestamp().
|
2017-05-20 08:48:00 +00:00
|
|
|
Str("role", "my-service").
|
2017-05-20 09:22:57 +00:00
|
|
|
Str("host", "local-hostname").
|
2017-05-20 08:48:00 +00:00
|
|
|
Logger()
|
|
|
|
|
|
|
|
c := alice{}
|
|
|
|
|
|
|
|
// Install the logger handler with default output on the console
|
|
|
|
c = c.Append(hlog.NewHandler(log))
|
|
|
|
|
2021-12-21 12:07:54 +00:00
|
|
|
// Install some provided extra handlers to set some request's context fields.
|
|
|
|
// Thanks to those handlers, all our logs will come with some pre-populated fields.
|
2017-05-20 08:48:00 +00:00
|
|
|
c = c.Append(hlog.RemoteAddrHandler("ip"))
|
|
|
|
c = c.Append(hlog.UserAgentHandler("user_agent"))
|
|
|
|
c = c.Append(hlog.RefererHandler("referer"))
|
2017-05-20 09:22:57 +00:00
|
|
|
//c = c.Append(hlog.RequestIDHandler("req_id", "Request-Id"))
|
2017-05-20 08:48:00 +00:00
|
|
|
|
|
|
|
// Here is your final handler
|
|
|
|
h := c.Then(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Get the logger from the request's context. You can safely assume it
|
|
|
|
// will be always there: if the handler is removed, hlog.FromRequest
|
|
|
|
// will return a no-op logger.
|
|
|
|
hlog.FromRequest(r).Info().
|
|
|
|
Str("user", "current user").
|
|
|
|
Str("status", "ok").
|
2021-12-21 12:07:54 +00:00
|
|
|
Msg("Something happened")
|
2017-05-20 08:48:00 +00:00
|
|
|
}))
|
|
|
|
http.Handle("/", h)
|
|
|
|
|
2017-05-20 09:22:57 +00:00
|
|
|
h.ServeHTTP(httptest.NewRecorder(), &http.Request{})
|
|
|
|
|
2021-12-21 12:07:54 +00:00
|
|
|
// Output: {"level":"info","role":"my-service","host":"local-hostname","user":"current user","status":"ok","time":"2001-02-03T04:05:06Z","message":"Something happened"}
|
2017-05-20 08:48:00 +00:00
|
|
|
}
|