Skip to content

Instantly share code, notes, and snippets.

@iamnoah
Last active March 30, 2021 23:44
Show Gist options
  • Save iamnoah/12d87b035a0f02f1bd223e48179fefe9 to your computer and use it in GitHub Desktop.
Save iamnoah/12d87b035a0f02f1bd223e48179fefe9 to your computer and use it in GitHub Desktop.
Formats JSON log lines (naively assumed to start with "{") in console output while preserving the non-JSON lines
jlog() {
local FORMAT=${FORMAT:-'"\(.timestamp) \(.level)\t - \(.name) - \(.message)\t\t\(del(.timestamp, .message, .level, .name, .traceback))\(if .traceback then "\r\n\(.traceback | gsub("\n"; "\r\n"))" else "" end)\r"'}
tee >( grep --line-buffered -v "^{" > /dev/tty ) | grep --line-buffered "^{" | jq --raw-output --unbuffered "$FORMAT"
}
# USAGE: command that prints json logs | jlog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment