kail icon indicating copy to clipboard operation
kail copied to clipboard

Add json and zerolog writers

Open johncsnyder opened this issue 6 years ago • 4 comments

Ive been using kail for some time now and I find it very useful. Ive seen this open PR https://github.com/boz/kail/pull/24, and implemented this as a separate writer struct as suggested. This would also address this issue https://github.com/boz/kail/issues/31.

Ive added two writers (a) A simple json writer enabled with the flag --json which attempts to parse json logs and if succesful, formats the output as indented json, otherwise falls back to the default behavior. Optionally, a jsonpath filter can be specified Eg --jsonpath=$.message which selected the message field. If the selected field is a string, it is formatted as plain text. (b) A zerolog json writer enabled with the flag --zerolog, which formats json, if applicable, using the zerolog console writer, otherwise falls back to the default behavior. Ive added cli flags to customize the zero field names, if necessary. The formatted output is compact with nice colors etc.

The following cli options are added:

     --json                  formats json logs, with optional jsonpath
      --jsonpath=$.message    filters json logs when --json is enabled
      --zerolog               formats zerolog json logs
      --zerolog-timestamp-field="time"
                              sets the zerolog timestamp field name
      --zerolog-level-field="level"
                              sets the zerolog level field name
      --zerolog-message-field="message"
                              sets the zerolog message field name
      --zerolog-error-field="error"
                              sets the zerolog error field name

Note: This would add two dependencies: github.com/yalp/jsonpath , and github.com/rs/zerolog. I believe the zerolog console writer is pretty much standalone and could be copied in without importing the full zerolog package, if that would be better.

In practice, I'm using the zerolog writer frequently, but added the basic json writer for more general use cases. zerolog assumes a flat json log structure.

johncsnyder avatar Sep 27 '19 13:09 johncsnyder

I'd like to see this merged! I think the only thing missing is adding those new options to the README as well.

terrafying avatar Feb 03 '21 19:02 terrafying

bump

terrafying avatar Oct 06 '21 18:10 terrafying

OK, I'll take a look shortly, @terrafying.

boz avatar Oct 07 '21 00:10 boz

It's been a while since i've used this actively, but happy to make updates if necessary

johncsnyder avatar May 04 '22 16:05 johncsnyder

@johncsnyder could you rebase/rework to review?

mnaser avatar Jan 05 '23 15:01 mnaser

@johncsnyder also what are your thoughts on the approach in https://github.com/boz/kail/pull/48 ?

mnaser avatar Jan 05 '23 15:01 mnaser

@johncsnyder I have updated changes with other writers, but removed jsonpath change as that library is not maintained and have many issues.

chandanpasunoori avatar Jan 05 '23 22:01 chandanpasunoori

@mnaser @chandanpasunoori I've not had much time recently to take a look, but thank you for pushing forward, looks good 🙏

johncsnyder avatar Jan 07 '23 02:01 johncsnyder