I’ve created a log encoder module for Caddy.
You can find it at github.com/leodido/caddy-jsonselect-encoder.
It lets the user choose what info to emit in the JSON logs (it wraps the default json
log encoder).
How? Specifying a selector, like so:
log {
format jsonselect "{level} {ts} {request>host} {duration}"
}
Also, it lets the user change the resulting JSON structure (for example by changing the keys).
I was inspired by the format-encoder (thank you!) which is somewhat very similar to this.
Anyway, there are some differences in the internals of the plugin:
- the parsing of the selector happens at provisioning time, not at encoding time
- you can specify to which key (even nested ones) you want to set the value your extracting from the standard JSON entries
For example, you can select what to emit and define the keys to match the Stackdriver log entry format.
log {
format jsonselect "{severity:level} {timestamp:ts} {httpRequest>requestMethod:request>method} {httpRequest>protocol:request>proto} {httpRequest>status:status} {httpRequest>responseSize:size} {httpRequest>userAgent:request>headers>User-Agent>[0]}" {
time_format "rfc3339_nano"
level_format "upper"
}
}
Which outputs:
{"severity":"INFO","timestamp":"2021-07-19T14:48:56.262966Z","httpRequest":{"protocol":"HTTP/2.0","requestMethod":"GET","responseSize":17604,"status":200,"userAgent":"Mozilla/5.0 ..."}}
I’ve built it for the company (Gitpod) I’m currently working but I believe it can be useful to other Caddy lovers
Feel free to ask me questions, review it, give me advice on how to further improve it.
Thank you!