Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issues/163: log client address without port #165

Merged
merged 4 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Most recent version is listed first.

## v0.0.22
- Panic middleware should include correct stack trace: https://github.com/komuw/ong/pull/164
- Log client address without port: https://github.com/komuw/ong/pull/165

## v0.0.21
- Improve performance of calling Csrf middleware multiple times: https://github.com/komuw/ong/pull/161
Expand Down
19 changes: 12 additions & 7 deletions middleware/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,19 @@ func Log(wrappedHandler http.HandlerFunc, domain string, l log.Logger) http.Hand
ResponseWriter: w,
}
defer func() {
clientAddress := r.RemoteAddr
if host, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
clientAddress = host
}

flds := log.F{
"requestAddr": r.RemoteAddr,
"method": r.Method,
"path": r.URL.EscapedPath(),
"code": lrw.code,
"status": http.StatusText(lrw.code),
"durationMS": time.Since(start).Milliseconds(),
"pid": os.Getpid(),
"clientAddress": clientAddress,
"method": r.Method,
"path": r.URL.Redacted(),
"code": lrw.code,
"status": http.StatusText(lrw.code),
"durationMS": time.Since(start).Milliseconds(),
"pid": os.Getpid(),
}
if ongError := lrw.Header().Get(ongMiddlewareErrorHeader); ongError != "" {
flds["ongError"] = ongError
Expand Down
2 changes: 1 addition & 1 deletion middleware/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestLogMiddleware(t *testing.T) {
fmt.Sprint(res.StatusCode),
"durationMS",
"logID",
"requestAddr",
"clientAddress",
"error",
} {
attest.Subsequence(t, logOutput.String(), v)
Expand Down
20 changes: 13 additions & 7 deletions middleware/panic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package middleware

import (
"fmt"
"net"
"net/http"
"os"

Expand Down Expand Up @@ -29,14 +30,19 @@ func Panic(wrappedHandler http.HandlerFunc, l log.Logger) http.HandlerFunc {
code,
)

clientAddress := r.RemoteAddr
if host, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
clientAddress = host
}

flds := log.F{
"err": fmt.Sprint(errR),
"requestAddr": r.RemoteAddr,
"method": r.Method,
"path": r.URL.EscapedPath(),
"code": code,
"status": status,
"pid": os.Getpid(),
"err": fmt.Sprint(errR),
"clientAddress": clientAddress,
"method": r.Method,
"path": r.URL.Redacted(),
"code": code,
"status": status,
"pid": os.Getpid(),
}
if ongError := w.Header().Get(ongMiddlewareErrorHeader); ongError != "" {
flds["ongError"] = ongError
Expand Down