2020-04-02 04:59:14 +05:30
|
|
|
package eventsubscribers
|
|
|
|
|
|
|
|
import (
|
2020-04-02 22:04:39 +05:30
|
|
|
"net/http"
|
2020-04-03 19:48:02 +05:30
|
|
|
"strings"
|
2020-04-02 04:59:14 +05:30
|
|
|
|
|
|
|
"github.com/mono83/slf"
|
|
|
|
"github.com/mono83/slf/wd"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Logger struct {
|
|
|
|
slf.Logger
|
|
|
|
}
|
|
|
|
|
2020-04-04 00:50:56 +05:30
|
|
|
func (l *Logger) ConfigureWithDispatcher(d Subscriber) {
|
2020-04-02 22:04:39 +05:30
|
|
|
d.Subscribe("skinsystem:after_request", l.handleAfterSkinsystemRequest)
|
2020-04-02 04:59:14 +05:30
|
|
|
}
|
|
|
|
|
2020-04-02 22:04:39 +05:30
|
|
|
func (l *Logger) handleAfterSkinsystemRequest(req *http.Request, statusCode int) {
|
2020-04-03 19:48:02 +05:30
|
|
|
path := req.URL.Path
|
|
|
|
if req.URL.RawQuery != "" {
|
|
|
|
path += "?" + req.URL.RawQuery
|
2020-04-02 22:04:39 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
l.Info(
|
2020-04-03 19:48:02 +05:30
|
|
|
":ip - - \":method :path\" :statusCode - \":userAgent\" \":forwardedIp\"",
|
|
|
|
wd.StringParam("ip", trimPort(req.RemoteAddr)),
|
2020-04-02 22:04:39 +05:30
|
|
|
wd.StringParam("method", req.Method),
|
2020-04-03 19:48:02 +05:30
|
|
|
wd.StringParam("path", path),
|
2020-04-02 22:04:39 +05:30
|
|
|
wd.IntParam("statusCode", statusCode),
|
|
|
|
wd.StringParam("userAgent", req.UserAgent()),
|
2020-04-03 19:48:02 +05:30
|
|
|
wd.StringParam("forwardedIp", req.Header.Get("X-Forwarded-For")),
|
2020-04-02 22:04:39 +05:30
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-04-03 19:48:02 +05:30
|
|
|
func trimPort(ip string) string {
|
|
|
|
// Don't care about possible -1 result because RemoteAddr will always contain ip and port
|
|
|
|
cutTo := strings.LastIndexByte(ip, ':')
|
|
|
|
|
|
|
|
return ip[0:cutTo]
|
|
|
|
}
|