Node.js日志自动化分析实现指南

自动化分析的基础是结构化日志(如JSON格式),便于后续工具解析和提取关键信息。推荐使用以下Node.js日志库:
const winston = require('winston');const logger = winston.createLogger({level: 'info',format: winston.format.combine(winston.format.timestamp(),winston.format.json()),transports: [new winston.transports.File({ filename: 'error.log', level: 'error' }),new winston.transports.File({ filename: 'combined.log' })]});日志文件过大会影响分析效率,需通过日志轮转限制文件大小、数量并压缩旧日志。常见方法:
winston-daily-rotate-file实现每日轮转,配置示例如下:const DailyRotateFile = require('winston-daily-rotate-file');const transport = new DailyRotateFile({filename: 'application-%DATE%.log',datePattern: 'YYYY-MM-DD-HH',zippedArchive: true, // 压缩旧日志maxSize: '20m',// 单个文件最大20MBmaxFiles: '14d'// 保留14天日志});logrotate(Linux自带),通过配置文件(如/etc/logrotate.d/node-app)实现自动轮转:/var/log/node-app.log {dailyrotate 7compressmissingoknotifemptycopytruncatedateext}pm2-logrotate插件实现轮转:pm2 install pm2-logrotatepm2 set pm2-logrotate:max_size 10M# 单个文件最大10MBpm2 set pm2-logrotate:retain 7# 保留7个文件pm2 set pm2-logrotate:compress true # 压缩旧日志将分散的日志集中存储,便于统一分析。常见方案:
file输入插件读取Node.js日志文件,使用grok过滤器解析日志(如提取时间戳、日志级别、消息),最后将处理后的日志发送到Elasticsearch。loki传输插件将Node.js日志发送到Loki,再通过Grafana进行查询和可视化。利用工具对集中存储的日志进行分析和可视化,快速识别问题:
nodejs-logs-*),通过“Discover”页面搜索特定日志(如loglevel: "error"),然后添加可视化组件(如柱状图、折线图)到仪表盘,展示错误率、请求量等关键指标的变化趋势。Logs面板查询level="error"的日志,通过Stat面板显示错误总数。import osfrom collections import Counterdef analyze_error_logs(log_file):error_levels = ['error', 'critical']level_counts = Counter()with open(log_file, 'r') as file:for line in file:for level in error_levels:if f'"{level}"' in line or f'[{level}]' in line:level_counts[level] += 1breakreturn level_countsif __name__ == '__main__':log_file = '/var/log/nodejs/error.log'counts = analyze_error_logs(log_file)for level, count in counts.items():print(f'{level.upper()}: {count}')将脚本添加到cron作业(如每天凌晨1点运行),自动生成错误报告并发送邮件。设置实时监控和告警规则,在日志出现异常(如错误率飙升、响应时间过长)时及时通知运维人员:
prom-client库在Node.js应用中暴露指标(如HTTP请求延迟、错误计数):const promClient = require('prom-client');const httpRequestDuration = new promClient.Histogram({name: 'http_request_duration_ms',help: 'Duration of HTTP requests in ms',labelNames: ['method', 'route', 'code'],buckets: [0.1, 5, 15, 50, 100, 200, 300, 400, 500] // 桶边界});app.use((req, res, next) => {const start = Date.now();res.on('finish', () => {const duration = Date.now() - start;httpRequestDuration.labels(req.method, req.route.path, res.statusCode).observe(duration);});next();});prometheus.yml中添加Node.js应用的scrape配置)。level: "error" AND message: "timeout")设置告警,支持邮件、PagerDuty等通知渠道。