json: support BORG_JSON_COMPACT env var for single-line output#9855
json: support BORG_JSON_COMPACT env var for single-line output#9855charmikadi wants to merge 6 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #9855 +/- ##
==========================================
+ Coverage 85.11% 85.13% +0.01%
==========================================
Files 93 93
Lines 15408 15421 +13
Branches 2326 2329 +3
==========================================
+ Hits 13114 13128 +14
+ Misses 1596 1595 -1
Partials 698 698 ☔ View full report in Codecov by Harness. |
| """Dump using BorgJSONEncoder.""" | ||
| return json.dumps(obj, sort_keys=True, indent=4, cls=BorgJsonEncoder) | ||
| indent = 4 | ||
| if os.environ.get("BORG_JSON_COMPACT"): |
There was a problem hiding this comment.
How about calling it BORG_JSON_INDENT and supporting all options that the python parameter supports?
indent (int | str | None) – If a positive integer or string, JSON array elements and object members will be pretty-printed with that indent level. A positive integer indents that many spaces per level; a string (such as "\t") is used to indent each level. If zero, negative, or "" (the empty string), only newlines are inserted. If None (the default), no newlines are inserted.
Like it is now, it could be confusing to users, because setting that to any non-empty value has the same effect. E.g. "1" would make output compact and "0" would also make output compact.
Description
When BORG_JSON_COMPACT is set, json_dump() produces single-line JSON instead of the default indented output. This makes it easier for log collectors to parse borg's JSON stdout as one event per line. Implementation uses an env var and not CLI flag.
Fixes #3605
Checklist
mastertoxor the relevant test subset)