在 Linux 下排查 Node.js 日志,通常可以按下面几个层次来做:看日志在哪 → 看日志内容 → 动态排查 → 进程/资源 → 代码层。
常见位置:
logs/ls -lh /path/to/project/logs
console.log / console.errorwinston / pino / log4jspm2 logs
pm2 logs app_name
pm2 logs --lines 100
日志文件一般在:
~/.pm2/logs/
journalctl -u your-node-service
journalctl -u your-node-service -f
journalctl -u your-node-service --since "10 min ago"
tail -f app.log
tail -n 200 app.log
tail -f app.log | grep ERROR
grep -i "timeout" app.log
典型日志:
Error: connect ECONNREFUSED
at ...
重点看:
Error: 后面的信息at 堆栈(定位文件和行号)日志可能:
FATAL ERROR: Ineffective mark-compacts near heap limit
查看内存:
free -h
top
ps aux | grep node
或
pm2 status
端口是否监听:
netstat -tunlp | grep 3000
# 或
ss -tunlp | grep 3000
top
htop
lsof -p <pid>
df -h
du -sh *
| 问题 | 排查点 |
|---|---|
| 接口慢 | 日志耗时、DB 查询、CPU |
| 进程挂掉 | PM2 / systemd 日志 |
| 内存涨 | heapdump、leak |
| 请求失败 | nginx / 反向代理日志 |
winston / pinoinfo / warn / error如果你愿意,可以把 具体的报错日志或启动方式(PM2 / Docker / systemd) 发出来,我可以帮你精确分析。