通过 Debian Node.js 日志分析用户行为,可按以下步骤操作:
日志记录
const winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [new winston.transports.File({ filename: 'combined.log' })]
});
logger.info({ event: 'page_view', userId: 'user123', page: '/home' });
日志传输与存储
winston-daily-rotate-file 插件)避免文件过大。日志解析与分析
grep、awk)提取特定行为日志(如页面访问、按钮点击)。const fs = require('fs');
const readline = require('readline');
const pageViews = {};
readline.createInterface({ input: fs.createReadStream('combined.log') })
.on('line', (line) => {
const match = line.match(/"GET \/([^"]+) HTTP/1.1/);
if (match) pageViews[match[1]] = (pageViews[match[1]] || 0) + 1;
})
.on('close', () => console.log(pageViews));
数据可视化与监控
隐私与优化
工具链推荐: