在CentOS上进行Golang日志分析,可按以下步骤操作:
log可直接输出日志到文件或标准输出;第三方库如logrus、zap支持结构化日志、日志轮转等功能,需在代码中配置输出路径和格式。logrus.SetOutput(os.OpenFile("app.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)) logrus.SetFormatter(&logrus.JSONFormatter{}) systemd服务捕获日志:在服务配置文件中指定StandardOutput和StandardError路径,将日志写入系统日志文件。logrotate管理日志文件,避免过大:创建配置文件/etc/logrotate.d/myapp,设置按天轮转、压缩等规则。grep过滤关键字:grep 'error' /path/to/logfile.log。awk统计分析:如统计错误数量awk '/error/ {count++} END {print count}' logfile.log。zap等库输出JSON格式日志,便于ELK等工具解析。工具推荐:
logrotate+GoAccess(轻量级,快速生成报告)。