From b904be939a4de01b61c00de5466109479bbc9158 Mon Sep 17 00:00:00 2001 From: spiral Date: Sat, 11 Feb 2023 11:13:46 -0500 Subject: [PATCH] feat(api): log requests as json instead of plaintext --- services/web-proxy/main.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/services/web-proxy/main.go b/services/web-proxy/main.go index 6546d935..92328c57 100644 --- a/services/web-proxy/main.go +++ b/services/web-proxy/main.go @@ -2,7 +2,8 @@ package main import ( "context" - "log" + "encoding/json" + "fmt" "net/http" "net/http/httputil" "strconv" @@ -115,7 +116,19 @@ func logTimeElapsed(resp *http.Response) error { "route": cleanPath(r.Host, r.URL.Path), }).Observe(elapsed.Seconds()) - log.Printf("[%s] \"%s %s%s\" %d - %vms %s\n", r.Header.Get("Fly-Client-IP"), r.Method, r.Host, r.URL.Path, resp.StatusCode, elapsed.Milliseconds(), r.Header.Get("User-Agent")) + log, _ := json.Marshal(map[string]interface{}{ + "remote_ip": r.Header.Get("Fly-Client-IP"), + "method": r.Method, + "host": r.Host, + "route": r.URL.Path, + "route_clean": cleanPath(r.Host, r.URL.Path), + "status": resp.StatusCode, + "elapsed": elapsed.Milliseconds(), + "user_agent": r.Header.Get("User-Agent"), + }) + fmt.Println(string(log)) + + // log.Printf("[%s] \"%s %s%s\" %d - %vms %s\n", r.Header.Get("Fly-Client-IP"), r.Method, r.Host, r.URL.Path, resp.StatusCode, elapsed.Milliseconds(), r.Header.Get("User-Agent")) return nil }