在 Linux 下检索 Node.js 日志,通常取决于日志写在哪、以什么形式存在。下面按常见场景给你一套实用方法。
如果你直接运行:
node app.js
日志通常在:
node app.js > app.log 2>&1
常见位置:
./logs/app.log/var/log/xxx.log~/.pm2/logs/pm2 logs
pm2 logs app-name
cat app.log
less app.log
tail -f app.log # 实时查看
grep "ERROR" app.log
grep -i "timeout" app.log # 忽略大小写
grep -C 5 "Exception" app.log # 上下各5行
tail -f app.log | grep "ERROR"
grep "2026-01-21" app.log
grep "2026-01-21 10:" app.log
tail -n 1000 app.log | grep "ERROR"
{"level":"error","message":"db failed","time":"2026-01-21T10:00:00Z"}
grep '"level":"error"' app.log
或配合 jq:
cat app.log | jq 'select(.level=="error")'
如果 Node.js 是用 systemd 启动的:
journalctl -u node-app.service
journalctl -u node-app.service -f
journalctl -u node-app.service --since "2026-01-21 09:00"
less 内搜索less app.log
/ERROR
awk 提取字段awk '/ERROR/ {print $1, $2, $0}' app.log
zgrep(压缩日志)zgrep "ERROR" app.log.1.gz
grep "user_id=123" app.log | grep "ERROR"
grep -c "ERROR" app.log
grep / jq / journalctl如果你愿意,可以告诉我:
我可以直接给你针对性的命令模板。