Master Real-Time Log Troubleshooting with Tail, Grep, and Zgrep
Learn how to efficiently locate and analyze Java exceptions and other errors in real-time by combining tail, grep, zgrep, and advanced command-line options, enabling complete stack traces, context preservation, compressed log handling, trend analysis, and performance optimization for faster root-cause identification.
Background
A new teammate, Xiao Wang, struggled with log inspection using only tail -f and grep, which showed only the exception type without the stack trace. The author demonstrates a more powerful approach to log troubleshooting.
Problems with naive log inspection
Single‑line trap: tail -f | grep captures only a fragment, losing the full stack trace.
Visual fatigue: Scrolling line‑by‑line consumes attention and makes context hard to follow.
Format barrier: Compressed logs require an extra unzip step, breaking the workflow.
Effective command‑line techniques
1. Capture full exception stack
Use -A to include following lines and pipe to less for paging.
grep -A50 "NullPointerException" application.log | less2. Real‑time monitoring with context
Combine tail -f with case‑insensitive, multi‑pattern grep.
tail -f application.log | grep -Ai30 "ERROR\|Exception"3. Analyze compressed logs without decompressing
zgrepsearches .gz files directly and keeps filenames.
zgrep -H -A50 "OutOfMemoryError" *.gz4. Exception trend statistics
Count occurrences across files and sort to reveal hotspots.
grep -c "ConnectionTimeout" *.log | sort -nr -t: -k25. Advanced parameter usage
Examples of noise filtering and context control.
grep -v "健康检查\|心跳" app.log | grep -A30 "异常"Extended toolchain
Line counting with wc
grep "ERROR" app.log | wc -lField extraction and aggregation with awk
awk '$7 == 500' app.logStream editing with sed
sed -n '/2023-11-15 14:00:00/,/2023-11-15 14:10:00/p' app.logSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
dbaplus Community
Enterprise-level professional community for Database, BigData, and AIOps. Daily original articles, weekly online tech talks, monthly offline salons, and quarterly XCOPS&DAMS conferences—delivered by industry experts.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
