Debian 上 Node.js 日志缺失通常不是 Node 本身的问题,而是运行方式、systemd、输出位置或日志轮转导致的。下面按最常见原因给你一个排查清单。
如果你用 systemd 启动 Node.js:
[Service]
ExecStart=/usr/bin/node /opt/app/index.js
问题:
console.log() 默认输出到 stdout&、nohup、或 fork 方式运行,日志可能丢失✅ 正确做法:
[Service]
ExecStart=/usr/bin/node /opt/app/index.js
StandardOutput=journal
StandardError=journal
然后查看日志:
journalctl -u your-service-name -f
常见于:
node app.js > /dev/null 2>&1
或:
nohup node app.js >/dev/null &
✅ 检查启动脚本、PM2、Dockerfile、systemd 是否有 /dev/null
比如:
process.on('SIGTERM', () => process.exit());
或手动 fork() 后父进程退出
✅ 解决:
例如:
fs.createWriteStream('/var/log/node/app.log')
但:
/var/log/node 不存在
✅ 检查:
ls -ld /var/log/node
✅ 并确认权限:
chown -R node:node /var/log/node
常见问题:
✅ 示例(winston):
logger.add(new transports.File({
filename: '/var/log/node/app.log',
level: 'info'
}));
systemd 日志可能:
查看当前限制:
journalctl --disk-usage
✅ 可调整:
/etc/systemd/journald.conf
SystemMaxUse=200M
如果你在 Docker + Debian:
console.log 不会进文件✅ 正确方式:
console.log('hello')
docker logs <container>
node -e "console.log('test')"
journalctl -u your-node-service -n 100
ls -l /proc/$(pgrep node)/fd
ps aux | grep node
✅ 生产环境用 PM2
pm2 start app.js
pm2 logs
✅ 或 systemd + journal
✅ 不要手动 nohup + 重定向
你可以贴出以下任意一项:
我可以直接告诉你 为什么日志没出现、该改哪里 ✅