samber / slog-gin

🚨 Gin middleware for slog logger
https://pkg.go.dev/github.com/samber/slog-gin
MIT License
115 stars 13 forks source link

Headers are printed as duplicate json keys #15

Closed CarlLindqvist closed 7 months ago

CarlLindqvist commented 8 months ago

Logging request/response headers adds each header as a duplicate "header" key in json output. This is invalid json. It would be better to have the headers as a json array.

samber commented 7 months ago

Can you make an example please?

CarlLindqvist commented 7 months ago

Sure. Here is a log output as json with a bunch of headers set (IPs obfuscated):

{
    "time":"2024-02-12T07:53:52.410079233Z",
    "level":"INFO",
    "msg":"",
    "request":{
       "time":"2024-02-12T07:53:52.40991004Z",
       "method":"GET",
       "host":"X.X.X.X",
       "path":"/",
       "query":"",
       "params":{},
       "route":"",
       "ip":"X.X.X.X",
       "referer":"",
       "length":0,
       "header":{
          "X-Amzn-Trace-Id":[
             "Root=1-65c9ce90-6b85b2375fa9aeb27161a2dd"
          ]
       },
       "header":{
          "User-Agent":[
             "Mozilla/4.0 (compatible; MSIE 6.0; Windows CE; IEMobile 7.11) XV6800"
          ]
       },
       "header":{
          "Accept-Charset":[
             "utf-8"
          ]
       },
       "header":{
          "Accept-Encoding":[
             "gzip"
          ]
       },
       "header":{
          "X-Forwarded-For":[
             "X.X.X.X"
          ]
       },
       "header":{
          "X-Forwarded-Proto":[
             "https"
          ]
       },
       "header":{
          "X-Forwarded-Port":[
             "443"
          ]
       },
       "user-agent":"Mozilla/4.0 (compatible; MSIE 6.0; Windows CE; IEMobile 7.11) XV6800"
    },
    "response":{
       "time":"2024-02-12T07:53:52.410061559Z",
       "latency":151289,
       "status":404,
       "length":0
    }
 }

This is invalid json, the headers are duplicate keys

samber commented 7 months ago

Oh! I see.

I read your first message too fast and was expecting duplicated "request" or "response" fields.

I just made a fix 👌

CarlLindqvist commented 7 months ago

Now it looks a lot better! 👍