温馨提示×

debian js日志怎么检索

小樊
40
2026-10-15 22:01:17
栏目: 编程语言

在 Debian 上检索 JS(JavaScript)日志,通常取决于日志写在哪、用什么运行环境(Node.js / 浏览器 / 前端构建工具)。下面按常见场景说明。


一、Node.js 服务日志检索

1. 日志直接输出到终端 / 文件

如果你用的是:

node app.js

或

npm start

日志一般在:

  • 标准输出(终端)
  • 日志文件(如 logs/app.log)

检索方式

# 查看文件日志
cat logs/app.log

# 实时查看
tail -f logs/app.log

# 按关键字检索
grep "ERROR" logs/app.log

# 忽略大小写
grep -i "error" logs/app.log

# 显示行号
grep -n "TypeError" logs/app.log

# 多关键字
grep -E "error|warn" logs/app.log

2. 使用 PM2 管理 Node.js

如果是 PM2 启动的:

pm2 logs
pm2 logs app_name
pm2 logs --lines 200

检索历史日志:

pm2 logs --json | grep "error"

3. 使用 systemd(Debian 常见)

如果 Node 服务是 systemd 服务:

journalctl -u your-node-service
journalctl -u your-node-service -f
journalctl -u your-node-service | grep "error"

按时间:

journalctl -u your-node-service --since "2026-01-21 00:00"

二、前端 JS 日志(浏览器)

浏览器 JS 日志不在 Debian 服务器上,在用户浏览器中。

查看方式

  • Chrome / Firefox:
    • F12 → Console
    • Network(看请求失败)

如果你收集了前端日志(推荐)

常见方案:

  • Sentry -ELK / Grafana Loki
  • 自建日志接口(前端 console.error → 后端)

检索时在后端日志或日志平台中查。


三、Nginx / Apache 中的 JS 错误(静态资源)

如果是 JS 文件 404 / 500:

Nginx

tail -f /var/log/nginx/access.log
grep "app.js" /var/log/nginx/access.log
grep "500" /var/log/nginx/error.log

Apache

tail -f /var/log/apache2/access.log
grep ".js" /var/log/apache2/access.log

四、常用高效检索技巧(Debian)

# 实时过滤
tail -f app.log | grep "ERROR"

# 统计错误次数
grep -c "ERROR" app.log

# 查看前后内容
grep -A 5 -B 5 "TypeError" app.log

# 按时间范围(日志带时间)
sed -n '/10:00/,/10:30/p' app.log

五、如果你能补充,我可以更精准

你可以告诉我:

  1. JS 是 Node.js 还是前端
  2. 日志在 文件 / systemd / PM2 / Docker
  3. 想检索的是 error / 某个接口 / 某个用户

我可以直接给你一条可用命令。

0 踩