Skip to content

Instantly share code, notes, and snippets.

@lamchau
Last active March 6, 2024 19:55
Show Gist options
  • Save lamchau/e6e61c7dec8d6400a4fac42f9726cb11 to your computer and use it in GitHub Desktop.
Save lamchau/e6e61c7dec8d6400a4fac42f9726cb11 to your computer and use it in GitHub Desktop.

Log File Navigator (Optional)

lnav is a log file navigator that is capable of reading and analyzing log files in real time. It is a terminal application and is the a robust tool for navigating and analyzing log files.

It's similar to multitail but more powerful more discoverable UX.

Some notable features:

  • Creates a unified view across multiple formats and types (e.g. JSON, syslog + ftl and temporal).
  • Persistent log sessions (e.g. filters, queries, and views).
  • Alternative querying via SQL (e.g. SELECT * FROM log WHERE level = 'error').
  • Histogram view (e.g. log levels, time intervals) for a quick overview.
  • Unique coloring based on value types (e.g. multiple payloads will be colored differently).

1. Install lnav definition

.lnav/install.sh

2. Getting logs from ftl

ftl dev . \
	--log-level=debug \
	--log-json 2>&1 |
	tee -a "logs/ftl-$(date +'%F').log.json"
ftl serve \
	--allow-origins "*" \
	--log-level=debug \
	--log-json 2>&1 |
	tee -a "logs/ftl-$(date +'%F').log.json"

3. Analyzing logs

lnav -r logs/*
{
"$schema": "https://lnav.org/schemas/format-v1.schema.json",
"ftl_json": {
"title": "FTL JSON log format",
"description": "FTL JSON log format",
"timestamp-field": "time",
"json": true,
"file-pattern": "ftl-.*\\.log\\.json",
"level-field": "level",
"level": {
"debug": "debug",
"info": "info",
"warning": "warn",
"error": "error",
"fatal": "fatal"
},
"body-field": "message",
"value": {
"message": {
"kind": "string",
"identifier": false
},
"attributes": {
"kind": "json",
"identifier": true
},
"time": {
"kind": "string",
"identifier": false
}
},
"highlights": {
"fatal": {
"color": "Red1",
"pattern": "CRITICAL|FATAL|fatal"
},
"error": {
"color": "OrangeRed1",
"pattern": "ERROR"
},
"warning": {
"color": "Orange1",
"pattern": "WARN"
},
"info": {
"color": "Blue1",
"pattern": "INFO"
},
"debug": {
"color": "Grey62",
"pattern": "DEBUG"
},
"ipv4": {
"color": "Yellow1",
"pattern": "\\b(?:[0-9]{1,3}\\.){3}[0-9]{1,3}:[0-9]+\\b"
}
},
"line-format": [
{
"field": "__timestamp__",
"timestamp-format": "%Y-%m-%dT%H:%M:%S.%N%Z"
},
" ",
{
"field": "level",
"text-transform": "uppercase",
"auto-width": true,
"min-width": 7,
"align": "right",
"prefix": "[",
"suffix": "]"
},
" ",
{
"field": "message"
}
]
}
}
#!/usr/bin/env bash
if ! command -v lnav &> /dev/null; then
echo "error: lnav is not installed. Please install it first."
exit 1
fi
version="$(lnav --version | cut -d'.' -f2)"
if [[ -z "$version" || "$version" -lt 12 ]]; then
echo "error: lnav version 0.12 or higher is required"
exit 1
fi
lnav_format="$(lnav -m format ftl_json source 2> /dev/null)"
if [ -n "$lnav_format" ]; then
confirm="n"
# prevent loss of customizations
# - https://github.com/tstack/lnav/issues/1240
echo "warning: ftl_json format already exists '$lnav_format'"
read -r -p "overwrite? [y/n] " confirm
if [[ "$confirm" =~ [NnQq] ]]; then
exit 0
fi
fi
# always resolve to script directory to install the format
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)"
find "$script_dir" -name '*.json' -print0 | xargs lnav -i
@lamchau
Copy link
Author

lamchau commented Mar 6, 2024

extracted from TBD54566975/ftl#1018

@lamchau
Copy link
Author

lamchau commented Mar 6, 2024

image

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