chrly/eventsubscribers/logger.go

42 lines
1015 B
Go
Raw Normal View History

package eventsubscribers
import (
"net/http"
2020-04-03 19:48:02 +05:30
"strings"
"github.com/mono83/slf"
"github.com/mono83/slf/wd"
)
type Logger struct {
slf.Logger
}
func (l *Logger) ConfigureWithDispatcher(d Subscriber) {
d.Subscribe("skinsystem:after_request", l.handleAfterSkinsystemRequest)
}
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
}
l.Info(
2020-04-03 19:48:02 +05:30
":ip - - \":method :path\" :statusCode - \":userAgent\" \":forwardedIp\"",
wd.StringParam("ip", trimPort(req.RemoteAddr)),
wd.StringParam("method", req.Method),
2020-04-03 19:48:02 +05:30
wd.StringParam("path", path),
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-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]
}