在 Ubuntu 上分析 Node.js 日志,通常可以按下面几个层次来做:日志在哪 → 怎么看 → 怎么分析 → 用什么工具。
取决于你代码或框架:
console.log / console.errorwinston、plog、bunyan 等日志库/var/log/myapp/
/home/user/myapp/logs/
如果你用 systemd 启动 Node.js:
journalctl -u your-node-service
实时看:
journalctl -u your-node-service -f
/var/log/nginx/access.log
/var/log/nginx/error.log
pm2 logs
pm2 logs appname
tail -f app.log
grep -i error app.log
grep -i "uncaught" app.log
grep -c "Error" app.log
sed -n '/2026-01-21 10:00/,/2026-01-21 11:00/p' app.log
awk '{print $1}' access.log | sort | uniq -c | sort -nr
重点看:
ErrorTypeErrorENOENTECONNREFUSEDUnhandledPromiseRejection示例:
grep -A 10 "UnhandledPromiseRejection" app.log
heap out of memorytop
htop
df -h
free -m
用 winston + JSON:
{"level":"error","message":"db failed","stack":"..."}
分析:
jq '.level=="error"' app.log
如果你愿意,可以把一段 Node.js 日志贴出来,我可以直接帮你分析具体问题是啥。