通過 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));
數據可視化與監控
隱私與優化
工具鏈推薦: