在 Ubuntu 上,Node.js 本身并没有固定的“默认日志路径”,日志在哪里完全取决于你怎么运行 Node.js 程序以及是否配置了日志输出。下面按常见情况说明:
如果你在代码里用:
console.log('xxx')
或
fs.writeFileSync('/var/log/myapp.log', 'xxx')
那日志路径是你自己指定的,Node.js 不会自动存到某个固定目录。
如果你用 PM2 启动:
pm2 start app.js
~/.pm2/logs/
即:
/home/你的用户名/.pm2/logs/
查看日志:
pm2 logs
如果用 systemd 服务运行 Node.js:
journalctl -u your-node-service.service
journalctl -u your-node-service.service -f
日志由 systemd 管理,不在文件里,而是存在:
/var/log/journal/
如果你用 Nginx:
/var/log/nginx/access.log
/var/log/nginx/error.log
很多项目会手动放到:
/var/log/node/
/var/log/myapp/
你可以先确认:
ps aux | grep node
看看 Node 是怎么启动的,再决定去哪看日志。
如果你愿意,可以告诉我:
我可以帮你精确指出日志在哪。