Parsing JSON Logs in the Terminal
Process structured JSON logs with fx. Filter by log level, search for errors, and extract relevant information from log streams.
View Streaming Logs
Use fx with streaming mode for line-delimited JSON logs:
bash
tail -f app.log | fxEach line is parsed as a separate JSON object and displayed interactively.
Filter by Log Level
Show only error logs:
bash
... | fx 'filter(l => l.level === "error")'Search Log Messages
Find logs containing specific text:
bash
... | fx 'filter(l => l.message.includes("database"))'Count Logs by Level
Aggregate log counts:
bash
cat app.log | fx --slurp 'x => {
const counts = {};
x.forEach(l => counts[l.level] = (counts[l.level] || 0) + 1);
return counts;
}'Output:
json
{
"info": 1523,
"warn": 45,
"error": 12,
"debug": 890
}Extract Unique Error Types
List distinct error messages:
bash
cat app.log | fx --slurp 'filter(l => l.level === "error")' 'map(l => l.message)' uniqOutput:
json
[
"Connection refused",
"Timeout exceeded",
"Invalid token"
]Format Logs for Reading
Create a human-readable output:
bash
cat app.log | fx 'map(l => `[${l.level.toUpperCase()}] ${l.timestamp}: ${l.message}`)'Output:
[ERROR] 2024-01-15T10:23:45Z: Connection refused
[WARN] 2024-01-15T10:23:46Z: High memory usage
[INFO] 2024-01-15T10:23:47Z: Request completed