forked from jackwhelpton/fasthttp-routing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
95 lines (83 loc) · 3.13 KB
/
logger.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2016 Qiang Xue. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// Package access provides an access logging handler for the ozzo routing package.
package access
import (
"fmt"
"strings"
"time"
"github.com/naharp/fasthttp-routing"
"github.com/valyala/fasthttp"
)
// LogFunc logs a message using the given format and optional arguments.
// The usage of format and arguments is similar to that for fmt.Printf().
// LogFunc should be thread safe.
type LogFunc func(format string, a ...interface{})
// LogWriterFunc takes in the request and responseWriter objects as well
// as a float64 containing the elapsed time since the request first passed
// through this middleware and does whatever log writing it wants with that
// information.
// LogWriterFunc should be thread safe.
type LogWriterFunc func(ctx *fasthttp.RequestCtx, elapsed float64)
// CustomLogger returns a handler that calls the LogWriterFunc passed to it for every request.
// The LogWriterFunc is provided with the http.Request and LogResponseWriter objects for the
// request, as well as the elapsed time since the request first came through the middleware.
// LogWriterFunc can then do whatever logging it needs to do.
//
// import (
// "log"
// "net/http"
//
// "github.com/naharp/fasthttp-routing"
// "github.com/naharp/fasthttp-routing/access"
// )
//
// func myCustomLogger(req http.Context, res access.LogResponseWriter, elapsed int64) {
// // Do something with the request, response, and elapsed time data here
// }
// r := routing.New()
// r.Use(access.CustomLogger(myCustomLogger))
func CustomLogger(loggerFunc LogWriterFunc) routing.Handler {
return func(c *routing.Context) error {
startTime := time.Now()
err := c.Next()
elapsed := float64(time.Since(startTime).Nanoseconds()) / 1e6
loggerFunc(c.RequestCtx, elapsed)
return err
}
}
// Logger returns a handler that logs a message for every request.
// The access log messages contain information including client IPs, time used to serve each request, request line,
// response status and size.
//
// import (
// "log"
// "github.com/naharp/fasthttp-routing"
// "github.com/naharp/fasthttp-routing/access"
// )
//
// r := routing.New()
// r.Use(access.Logger(log.Printf))
func Logger(logf LogFunc) routing.Handler {
var logger = func(ctx *fasthttp.RequestCtx, elapsed float64) {
ip := GetClientIP(ctx)
req := fmt.Sprintf("%s %s %s", string(ctx.Request.Header.Method()), string(ctx.RequestURI()), string(ctx.Request.URI().Scheme()))
logf(`[%s] [%.3fms] %s %d %d`, ip, elapsed, req, ctx.Response.StatusCode(), len(ctx.Response.Body()))
}
return CustomLogger(logger)
}
// GetClientIP returns the originating IP for a request.
func GetClientIP(ctx *fasthttp.RequestCtx) string {
ip := string(ctx.Request.Header.Peek("X-Real-IP"))
if ip == "" {
ip = string(ctx.Request.Header.Peek("X-Forwarded-For"))
if ip == "" {
ip = ctx.RemoteAddr().String()
}
}
if colon := strings.LastIndex(ip, ":"); colon != -1 {
ip = ip[:colon]
}
return ip
}