// Logger ... func Logger(c lars.Context) { start := time.Now() c.Next() stop := time.Now() path := c.Request().URL.Path if path == "" { path = "/" } log.Printf("%s %d %s %s", c.Request().Method, c.Response().Status(), path, stop.Sub(start)) }
// Gzip returns a middleware which compresses HTTP response using gzip compression // scheme. func Gzip(c lars.Context) { c.Response().Header().Add(lars.Vary, lars.AcceptEncoding) if strings.Contains(c.Request().Header.Get(lars.AcceptEncoding), lars.Gzip) { w := writerPool.Get().(*gzip.Writer) w.Reset(c.Response().Writer()) defer func() { w.Close() writerPool.Put(w) }() gw := gzipWriter{Writer: w, ResponseWriter: c.Response().Writer()} c.Response().Header().Set(lars.ContentEncoding, lars.Gzip) c.Response().SetWriter(gw) } c.Next() }
// LoggingAndRecovery handle HTTP request logging + recovery func LoggingAndRecovery(c lars.Context) { t1 := time.Now() defer func() { if err := recover(); err != nil { trace := make([]byte, 1<<16) n := runtime.Stack(trace, true) log.Printf(" %srecovering from panic: %+v\nStack Trace:\n %s%s", Red, err, trace[:n], Reset) HandlePanic(c, trace[:n]) return } }() c.Next() var color string res := c.Response() req := c.Request() code := res.Status() switch { case code >= http.StatusInternalServerError: color = Underscore + Blink + Red case code >= http.StatusBadRequest: color = Red case code >= http.StatusMultipleChoices: color = Yellow default: color = Green } t2 := time.Now() log.Printf("%s %d %s[%s%s%s] %q %v %d\n", color, code, Reset, color, req.Method, Reset, req.URL, t2.Sub(t1), res.Size()) }