improve logging

Fixes #381
This commit is contained in:
Nicola Murino
2021-07-24 20:11:17 +02:00
parent 83c7453957
commit c41ae116eb
30 changed files with 237 additions and 150 deletions

View File

@@ -2,6 +2,7 @@ package logger
import (
"fmt"
"net"
"net/http"
"time"
@@ -40,6 +41,7 @@ func (l *StructuredLogger) NewLogEntry(r *http.Request) middleware.LogEntry {
}
fields := map[string]interface{}{
"local_addr": getLocalAddress(r),
"remote_addr": r.RemoteAddr,
"proto": r.Proto,
"method": r.Method,
@@ -77,3 +79,14 @@ func (l *StructuredLoggerEntry) Panic(v interface{}, stack []byte) {
Str("panic", fmt.Sprintf("%+v", v)).
Send()
}
func getLocalAddress(r *http.Request) string {
if r == nil {
return ""
}
localAddr, ok := r.Context().Value(http.LocalAddrContextKey).(net.Addr)
if ok {
return localAddr.String()
}
return ""
}