Skip to content

Instantly share code, notes, and snippets.

@svallory
Created June 14, 2024 02:11
Show Gist options
  • Save svallory/17bafec482da1574a0ce3d16b71fd746 to your computer and use it in GitHub Desktop.
Save svallory/17bafec482da1574a0ce3d16b71fd746 to your computer and use it in GitHub Desktop.
Nicely format Docker logs
#!/usr/bin/env nu
# Tail the last 50 lines of docker-compose logs and follow
cat log.txt | lines
| each { sed 's/^[^|]*| //' }
| where { |line| $line =~ "^\{" }
| each { from json }
| each { |record|
let t = $record.t."$date"
let s = $record.s
let c = $record.c
let id = $record.id
let ctx = $record.ctx
let msg = $record.msg
let attr = if ("attr" in $record) {
echo $record.attr | to json | lines
| each { |line| echo $" ($line)" } | to text
} else { "N/A" }
let severity = match $s {
"F" => "\e[1;31mFATAL",
"E" => "\e[1;31mERROR",
"W" => "\e[1;33mWARNING",
"I" => "\e[1;34mINFO"
_ => $"\e[1;38;2;255;255;255;48;2;30;30;40m ($s)"
}
echo $"
\e[90m[($c)]\e[0m ($severity): \e[4m($msg)\e[0m
\e[1;32mattr:\e[0m ($attr)"
} | to text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment