在 Debian 上跑 Node.js 时“日志缺失”通常不是 Node 本身把日志丢了,而是运行方式 + 系统日志机制不匹配导致的。下面按最常见原因逐条说明。
node app.js 前台运行(没接系统日志)如果你这样启动:
node app.js
✅ 解决:
pm2 / systemd / forever 管理const fs = require('fs');
console.log = (...args) => fs.appendFileSync('app.log', args.join(' ') + '\n');
console.logDebian 默认用 systemd + journald。
如果你用:
# /etc/systemd/system/nodeapp.service
[Service]
ExecStart=/usr/bin/node /opt/app/app.js
✅ 日志应在:
journalctl -u nodeapp -f
❌ 常见错误:
StandardOutput=null✅ 建议配置:
StandardOutput=journal
StandardError=journal
Debian 上 journal 可能:
Storage=volatile 限制检查:
journalctl --disk-usage
cat /etc/systemd/journald.conf | grep Storage
Node 里:
console.error('error') // ✅ 会进 stderr
console.log('info') // ⚠ 可能被忽略
systemd 默认只捕获 stderr。
如果你在 Debian 上跑 Docker:
docker logs 容器名
而不是去找文件。
Node 异步日志(如 winston、pino)在进程被杀时可能丢。
✅ 解决:
process.on('SIGTERM', () => logger.end(() => process.exit()));
# 是否是 systemd 服务
systemctl status 你的服务
# 看日志
journalctl -u 你的服务 -n 100
# 是否真的在写文件
lsof -p $(pidof node)
# 文件权限
ls -l /var/log/你的日志
如果你愿意,可以告诉我:
console.log 还是框架(winston / pino)我可以直接帮你定位是哪一类问题。