在 Ubuntu 上排查 Node.js 日志和运行时问题,通常可以按下面几个方向来做。
很多 Node 服务会用:
console.log / console.errorwinston / pino / log4js 等日志库常见日志位置:
# 项目目录
./logs/
./log/
/var/log/yourapp/
查看:
tail -f ./logs/app.log
如果是直接运行:
node app.js
日志会直接输出到终端。
pm2 logs
pm2 logs appname
pm2 logs --lines 100
查看错误:
pm2 show appname
pm2 monit
重启并看日志:
pm2 restart appname
pm2 logs appname
如果 Node 是用 systemctl 启动的:
systemctl status yourapp
journalctl -u yourapp -f
journalctl -u yourapp --since "1 hour ago"
例子:
journalctl -u node-app.service -n 200
nohup node app.js > app.log 2>&1 &
查看:
tail -f app.log
tail -f /var/log/syslog
dmesg | grep -i "out of memory"
dmesg | grep node
lsof -i:3000
netstat -tunlp | grep 3000
ps aux | grep node
top
htop
重点看:
Error: / TypeError: / ECONNREFUSEDCannot find modulelisten EADDRINUSEUnhandledPromiseRejection示例:
Error: connect ECONNREFUSED 127.0.0.1:5432
→ 数据库没起或地址错
ps aux | grep node
console.error如果你愿意,可以把:
我可以帮你直接定位问题。