要在JavaScript中统计Linux日志数据,有几种主要方法可以实现。以下是几种常见方案:
const fs = require('fs');
const readline = require('readline');
async function analyzeLogFile(filePath) {
const fileStream = fs.createReadStream(filePath);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
const stats = {
errorCount: 0,
warningCount: 0,
infoCount: 0,
ipAddresses: new Map(),
httpStatusCodes: new Map()
};
for await (const line of rl) {
// 统计错误级别
if (line.includes('ERROR')) stats.errorCount++;
if (line.includes('WARNING')) stats.warningCount++;
if (line.includes('INFO')) stats.infoCount++;
// 提取IP地址统计
const ipMatch = line.match(/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/);
if (ipMatch) {
const ip = ipMatch[0];
stats.ipAddresses.set(ip, (stats.ipAddresses.get(ip) || 0) + 1);
}
// 提取HTTP状态码
const httpStatusMatch = line.match(/\s(\d{3})\s/);
if (httpStatusMatch) {
const status = httpStatusMatch[1];
stats.httpStatusCodes.set(status, (stats.httpStatusCodes.get(status) || 0) + 1);
}
}
return stats;
}
// 使用示例
analyzeLogFile('/var/log/nginx/access.log')
.then(stats => console.log(stats))
.catch(err => console.error(err));
const { exec } = require('child_process');
// 使用grep预处理日志
exec('grep "ERROR" /var/log/syslog | wc -l', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`Number of ERROR entries: ${stdout}`);
});
const { exec } = require('child_process');
exec('awk \'{print $1}\' /var/log/nginx/access.log | sort | uniq -c | sort -nr',
(error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log('Top IP addresses:');
console.log(stdout);
});
const winston = require('winston');
const { ElasticsearchTransport } = require('winston-elasticsearch');
// 配置日志传输到Elasticsearch
const esTransport = new ElasticsearchTransport({
level: 'info',
clientOpts: { node: 'http://localhost:9200' }
});
const logger = winston.createLogger({
transports: [esTransport]
});
// 然后可以使用Elasticsearch的聚合查询进行统计分析
如果需要在浏览器中分析,可以先使用Node.js处理后端日志数据,然后通过API提供给前端:
// 后端API端点
const express = require('express');
const app = express();
const logStats = require('./logAnalyzer'); // 假设这是你的分析模块
app.get('/api/log-stats', async (req, res) => {
try {
const stats = await logStats.getStats();
res.json(stats);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.listen(3000, () => console.log('Server running on port 3000'));
// 前端使用fetch获取数据
fetch('/api/log-stats')
.then(response => response.json())
.then(data => {
// 使用Chart.js等库可视化数据
console.log('Log statistics:', data);
renderCharts(data);
});
使用Node.js的tail
命令实现实时监控:
const { spawn } = require('child_process');
const tail = spawn('tail', ['-f', '/var/log/syslog']);
tail.stdout.on('data', (data) => {
const lines = data.toString().split('\n');
lines.forEach(line => {
if (line) console.log('New log entry:', line);
// 这里可以添加实时分析逻辑
});
});
tail.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
tail.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
以上方法可以根据你的具体需求和环境选择最适合的方案。