Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LogHandler ¶
Example ¶
logrus.SetLevel(logrus.DebugLevel) var handle http.HandlerFunc = func(rw http.ResponseWriter, req *http.Request) { time.Sleep(20 * time.Millisecond) switch req.Method { case http.MethodGet: rw.WriteHeader(http.StatusOK) _, _ = rw.Write([]byte(`{"status":"ok"}`)) case http.MethodPost: rw.WriteHeader(http.StatusNoContent) case http.MethodDelete: rw.WriteHeader(http.StatusBadRequest) _, _ = rw.Write([]byte(`{"key":"StatusBadRequest","msg":"something wrong"}`)) case http.MethodPut: rw.WriteHeader(http.StatusInternalServerError) _, _ = rw.Write([]byte(`{"key":"StatusInternalServerError","msg":"internal server error"}`)) } } handler := LogHandler(logrus.WithField("service", ""))(handle).(*loggerHandler) for _, method := range []string{http.MethodGet, http.MethodPut, http.MethodDelete, http.MethodPost} { req, _ := http.NewRequest(method, "/", nil) handler.ServeHTTP(testify.NewMockResponseWriter(), req) }
Output:
Types ¶
type LoggerResponseWriter ¶
type LoggerResponseWriter struct { StatusCode int ErrMsg bytes.Buffer // contains filtered or unexported fields }
func (*LoggerResponseWriter) Header ¶ added in v1.14.7
func (rw *LoggerResponseWriter) Header() http.Header
func (*LoggerResponseWriter) Write ¶
func (rw *LoggerResponseWriter) Write(data []byte) (int, error)
func (*LoggerResponseWriter) WriteHeader ¶
func (rw *LoggerResponseWriter) WriteHeader(statusCode int)
Click to show internal directories.
Click to hide internal directories.