在 Linux 上排查 Node.js 日志丢失问题,通常可以从「进程、日志配置、系统机制、磁盘」几个方向入手。下面是一套比较实用的排查思路。
先明确:
是应用没写?还是写了但文件没有?还是被系统清掉了?
node app.js 2>&1 | tee /tmp/test.log
/tmp/test.log 有,说明应用写了tee 里都没有,说明日志根本没产生常见:
console.logwinstonpinolog4js重点检查:
fs.appendFile / createWriteStream示例检查:
process.on('uncaughtException', e => console.error(e));
process.on('unhandledRejection', e => console.error(e));
很多 Node 服务这样启动:
node app.js > log.txt 2>&1 &
但如果:
nohup 没加日志可能断掉。
✅ 正确方式:
nohup node app.js > log.txt 2>&1 &
或用 pm2:
pm2 start app.js
pm2 logs
如果你用:
systemctl start yourapp
那日志不在文件,而在:
journalctl -u yourapp.service -f
检查 service 文件:
[Service]
StandardOutput=journal
StandardError=journal
✅ 想写文件:
StandardOutput=append:/var/log/yourapp.log
检查:
ls -l /var/log/yourapp*
cat /etc/logrotate.d/yourapp
常见问题:
查看是否被删:
lsof | grep deleted
df -h
df -i
如果 inode 满,日志也写不进去。
dmesg | grep node
journalctl -xe
grep -i oom /var/log/messages
pm2 start app.js --log /var/log/node/app.log
pm2 flush
const winston = require('winston');
winston.add(new winston.transports.File({ filename: '/var/log/app.log' }));
# 1. 进程是否还在
ps aux | grep node
# 2. 日志文件
ls -l /var/log/yourapp.log
# 3. systemd 日志
journalctl -u yourapp -f
# 4. 被删文件
lsof | grep node | grep deleted
# 5. 磁盘
df -h && df -i
如果你愿意,可以把:
贴出来,我可以帮你精准定位是哪一类问题。