cactus / go-camo

A secure image proxy server
MIT License
254 stars 48 forks source link

Structured JSON logging #52

Closed igorwwwwwwwwwwwwwwwwwwww closed 3 years ago

igorwwwwwwwwwwwwwwwwwwww commented 3 years ago

Description

GitLab SRE here. :)

This patch introduces support for logging in JSON format (via new --log-json flag), and also changes the request logging to be properly structured.

This allows for easier diagnosis when there is an issue in production, as we can parse the fields out into Elasticsearch (or other log processing tools) and filter on them.

Before:

{"time": "2021-05-11T17:26:16.493664000+02:00", "level": "D", "msg": "client request", "extra": {"req": "&{GET /a/b HTTP/1.1 1 1 map[Accept:[*/*] User-Agent:[curl/7.64.1] Via:[go-camo]] {} <nil> 0 [] false go-camo map[] map[] <nil> map[] 127.0.0.1:52466 /a/b <nil> <nil> <nil> 0xc000030e80}"}}

After:

{"time": "2021-05-11T17:33:44.199222000+02:00", "level": "D", "msg": "client request", "extra": {"proto": "HTTP/1.1", "header": "map[Accept:[*/*] User-Agent:[curl/7.64.1] Via:[go-camo]]", "transfer_encoding": "[]","method": "GET", "path": "/a/b", "content_length": "0", "host": "go-camo", "remote_addr": "127.0.0.1:52613"}}

There's still room for improvement wrt nested maps, but this is already a lot more machine friendly.

Checklist

dropwhile commented 3 years ago

lgtm. Thanks!