forked from yarf-framework/extras
/
yarf_middleware.go
46 lines (38 loc) · 1.03 KB
/
yarf_middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package logger
import (
"github.com/yarf-framework/yarf"
"log"
)
// Logger middleware it's a simple log module that uses the default golang's log package.
// The log output writer can be defined by default with the log.SetOutput(w io.Writer) function.
// For more complex environments where a default logger can't be used across the system,
// a custom solution to replace this should be implemented.
type Logger struct {
yarf.Middleware
}
// PreDispatch wraps the http.ResponseWriter with a new LoggerWritter
// so we can log information about the response.
func (l *Logger) PreDispatch(c *yarf.Context) error {
c.Response = &LoggerWriter{
Writer: c.Response,
}
return nil
}
func (l *Logger) End(c *yarf.Context) error {
// If nobody sets the status code, it's a 200
var code int
if _, ok := c.Response.(*LoggerWriter); ok {
code = c.Response.(*LoggerWriter).StatusCode
}
if code == 0 {
code = 200
}
log.Printf(
"| %s | %s | %d | %s",
c.GetClientIP(),
c.Request.Method,
code,
c.Request.URL.String(),
)
return nil
}