在 Debian 上“采集 JS 日志”通常分两种场景:前端 JS(浏览器)日志 和 Node.js(服务端 JS)日志。下面分别说明常见做法。
前端 JS 运行在用户浏览器里, Debian 本身不能直接“抓”浏览器日志,需要:
在前端代码里收集 console、错误信息并发送到服务器:
window.onerror = function (msg, url, line, col, error) {
navigator.sendBeacon('/log', JSON.stringify({
msg, url, line, col, stack: error && error.stack
}));
};
或用 fetch / axios 上报。
常见方式:
location /log {
access_log /var/log/js/access.log json;
return 200;
}
app.post('/log', (req, res) => {
fs.appendFileSync('/var/log/js.log', JSON.stringify(req.body) + '\n');
res.send('ok');
});
如果你在 Debian 上运行 Node.js:
const winston = require('winston');
winston.add(new winston.transports.File({ filename: '/var/log/node/app.log' }));
systemctl edit yourapp.service
[Service]
StandardOutput=append:/var/log/node/app.out.log
StandardError=append:/var/log/node/app.err.log
journalctl -u yourapp.service -f
JS → Nginx/Node → 日志文件 → Filebeat → Elasticsearch / Loki
Debian 常用组件:
rsyslogfilebeatpromtailfluentdconsole.log('test');
fetch('/log', { method:'POST', body: 'hello' });
mkdir -p /var/log/js
nc -l -p 8080 >> /var/log/js/log.txt
如果你能说明:
我可以给你更具体的方案。