温馨提示×

nodejs ubuntu日志咋么排查

小樊
40
2026-10-18 05:43:27
栏目: 编程语言

在 Ubuntu 上排查 Node.js 日志和运行时问题,通常可以按下面几个方向来做。


一、先看 Node.js 进程本身有没有日志

1. 应用自己打的日志

很多 Node 服务会用:

  • console.log / console.error
  • winston / pino / log4js 等日志库

常见日志位置:

# 项目目录
./logs/
./log/
/var/log/yourapp/

查看:

tail -f ./logs/app.log

2. 前台运行(开发/调试)

如果是直接运行:

node app.js

日志会直接输出到终端。


二、如果是后台运行(非常重要)

1. PM2(最常见)

pm2 logs
pm2 logs appname
pm2 logs --lines 100

查看错误:

pm2 show appname
pm2 monit

重启并看日志:

pm2 restart appname
pm2 logs appname

2. systemd(Ubuntu 常见)

如果 Node 是用 systemctl 启动的:

systemctl status yourapp
journalctl -u yourapp -f
journalctl -u yourapp --since "1 hour ago"

例子:

journalctl -u node-app.service -n 200

3. nohup / & 启动

nohup node app.js > app.log 2>&1 &

查看:

tail -f app.log

三、系统级日志排查

1. 系统日志

tail -f /var/log/syslog

2. 内存 / OOM 被杀

dmesg | grep -i "out of memory"
dmesg | grep node

四、常见 Node.js 问题定位

1. 端口被占用

lsof -i:3000
netstat -tunlp | grep 3000

2. 进程是否在跑

ps aux | grep node

3. CPU / 内存异常

top
htop

五、错误日志怎么看(关键点)

重点看:

  • Error: / TypeError: / ECONNREFUSED
  • Cannot find module
  • listen EADDRINUSE
  • UnhandledPromiseRejection

示例:

Error: connect ECONNREFUSED 127.0.0.1:5432

→ 数据库没起或地址错


六、建议你这样排查(标准流程)

  1. 确认进程是否运行
    ps aux | grep node
    
  2. 看启动方式(PM2 / systemd / nohup)
  3. 看对应日志
  4. 看系统日志
  5. 复现错误 + 加 console.error

如果你愿意,可以把:

  • 启动方式(PM2 / systemd / 直接 node)
  • 报错日志贴出来

我可以帮你直接定位问题。

0 踩