2017-06-12 22:32:27 +02:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
wsGozilla "github.com/gorilla/websocket"
|
|
|
|
"golang.org/x/net/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getIP(r *http.Request) string {
|
|
|
|
ip := r.Header.Get("X-Forwarded-For")
|
|
|
|
if ip == "" {
|
|
|
|
ip = r.RemoteAddr
|
|
|
|
}
|
|
|
|
return ip
|
|
|
|
}
|
|
|
|
|
2017-06-13 00:21:19 +02:00
|
|
|
// init log entry with extra fields of interesting http request context
|
2017-06-12 22:32:27 +02:00
|
|
|
func HTTP(r *http.Request) *Entry {
|
|
|
|
return New().AddFields(map[string]interface{}{
|
|
|
|
"remote": getIP(r),
|
|
|
|
"method": r.Method,
|
|
|
|
"url": r.URL.RequestURI(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-13 00:21:19 +02:00
|
|
|
// init log entry with extra fields of interesting websocket request context
|
2017-06-12 22:32:27 +02:00
|
|
|
func WebsocketX(ws *websocket.Conn) *Entry {
|
|
|
|
r := ws.Request()
|
|
|
|
return New().AddFields(map[string]interface{}{
|
|
|
|
"remote": getIP(r),
|
|
|
|
"websocket": true,
|
|
|
|
"url": r.URL.RequestURI(),
|
|
|
|
})
|
|
|
|
}
|
2017-06-13 00:21:19 +02:00
|
|
|
|
|
|
|
// init log entry with extra fields of interesting websocket request context
|
2017-06-12 22:32:27 +02:00
|
|
|
func WebsocketGozilla(ws *wsGozilla.Conn) *Entry {
|
|
|
|
return New().AddFields(map[string]interface{}{
|
|
|
|
"remote": ws.RemoteAddr().String(),
|
|
|
|
"websocket": true,
|
|
|
|
})
|
|
|
|
}
|