在 Linux 环境下用 Node.js 相关日志进行搜索,常见做法分几类:直接用 Linux 命令、Node.js 脚本读取日志、使用日志库/日志系统。下面按场景说明。
grep "error" app.log
grep -i "timeout" app.log
grep -n "ECONNREFUSED" app.log
tail -f app.log | grep "error"
grep "2026-01-21" app.log
grep -r "TypeError" logs/
适合需要在代码里分析日志:
const fs = require('fs');
const readline = require('readline');
async function searchLog(file, keyword) {
const rl = readline.createInterface({
input: fs.createReadStream(file),
crlfDelay: Infinity
});
for await (const line of rl) {
if (line.includes(keyword)) {
console.log(line);
}
}
}
searchLog('app.log', 'error');
winstonpinobunyan例如 bunyan 自带命令行工具:
bunyan app.log -c 'level >= 30'
如果是 JSON 日志(pino / bunyan):
cat app.log | jq 'select(.level == "error")'
winston-daily-rotate-file)console.log 直接打生产日志如果你有具体场景(比如:PM2 日志 / Docker 日志 / K8s 日志 / 某个 Node 框架),我可以给你更精确的命令。