温馨提示×

Nodejs日志Debian如何定位

小樊
46
2026-07-27 14:05:50
栏目: 编程语言

Debian 系统上定位 Node.js 日志,可以按下面思路一步步来,基本就能找到问题所在。


一、先确认 Node.js 应用“有没有写日志”

1️⃣ 应用自身是否配置了日志

常见情况:

  • 使用 console.log / console.error
  • 使用日志库:
    • winston
    • pino
    • morgan(HTTP 请求日志)
    • bunyan

检查代码中是否有类似:

winston.add(new winston.transports.File({ filename: 'app.log' }));

二、Debian 下最常见的日志位置

✅ 1. 应用目录下的日志文件(最常见)

your-app/
├── logs/
│   ├── app.log
│   ├── error.log

搜索:

find / -name "*.log" 2>/dev/null | grep -E "node|app"

✅ 2. systemd 管理的 Node.js 服务(非常常见)

如果你是用 systemd 启动的 Node.js:

systemctl status your-node-service

查看日志:

journalctl -u your-node-service -f

查看历史日志:

journalctl -u your-node-service --since "2025-01-01"

这是 Debian 上最推荐的方式


✅ 3. PM2 管理的 Node.js 应用

如果用了 pm2

pm2 logs
pm2 logs app-name

日志一般在这里:

~/.pm2/logs/

✅ 4. Docker 容器中的 Node.js

docker logs 容器名
docker logs -f 容器名

三、系统级日志(辅助排查)

Debian 系统日志

/var/log/syslog
/var/log/messages

查看 Node 崩溃信息:

grep -i node /var/log/syslog

四、如果没有任何日志(常见原因)

❌ 1. 日志只输出到 stdout

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

❌ 2. systemd 没写文件

systemd 默认不写文件,而是走 journal

❌ 3. 日志路径是相对路径

fs.createWriteStream('logs/app.log')

可能写到了工作目录,而不是你以为的目录。


五、快速定位方案(推荐你直接用)

✅ 一步定位 Node 进程日志

ps aux | grep node

然后:

ls -l /proc/<pid>/fd

可以看到:

  • stdout
  • stderr
  • 打开的日志文件

六、生产环境最佳实践(建议)

✅ 使用 systemd + journald
✅ 或 PM2 + 日志文件
✅ 不要只用 console.log


七、如果你愿意,我可以帮你精准定位

你可以直接贴出:

  1. 启动方式(systemd / pm2 / docker / 手动)
  2. Node.js 启动命令
  3. 是否报过错

我可以一步一步帮你 精确到哪一行日志、在哪个文件

0