Skip to content

Instantly share code, notes, and snippets.

@pads
Last active February 13, 2025 15:01
Show Gist options
  • Save pads/863653524af27a5fb6d537e90f95779b to your computer and use it in GitHub Desktop.
Save pads/863653524af27a5fb6d537e90f95779b to your computer and use it in GitHub Desktop.
Helper script to pretty print and filter docker JSON logs output
logs_help () {
echo "Usage:"
echo "logs <container name> <jq expression>"
}
CONTAINER=$1
FILTER="$2"
if [ ! $# -eq 1 ]; then
logs_help
else
# First argument passed to jq is to filter out log lines that are not valid JSON
docker-compose logs -f $1 --no-log-prefix 2>&1 | jq -R "fromjson? | $FILTER"
fi
@pads
Copy link
Author

pads commented Feb 10, 2025

Example to print everything:

logs api .

Example to get only the msg from the logs:

logs api {msg}

Example to get messages containing text:

logs api 'select(.msg | contains("THING"))'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment