在 Debian 上排查 Golang 程序日志问题,一般可以从「程序自身日志 → 系统/服务日志 → 运行环境 → 调试手段」几个层面来做。下面按常见场景给你一个系统性的排查思路。
先搞清楚程序运行方式,不同方式日志位置不同:
./app
systemctl status yourapp
docker logs container_name
nohup ./app > app.log 2>&1 &
常见日志库:
log(标准库)logruszapslog(Go 1.21+)检查代码中是否:
log.SetOutput(os.Stdout)
或:
log.SetOutput(file)
如果输出到文件,确认:
ls -l /var/log/yourapp.log
tail -f /var/log/yourapp.log
如果程序是 systemd 管理的:
journalctl -u yourapp.service -f
journalctl -u yourapp.service --since "10 min ago"
查看是否:
sudo tail -f /var/log/syslog
sudo tail -f /var/log/messages # 某些 Debian 版本
常见错误:
permission deniedaddress already in useout of memoryps aux | grep yourapp
ss -tulnp | grep 8080
ls -ld /var/log
ls -l /var/log/yourapp.log
Golang 常因:
/var/logProtectSystem=strictdefer func() {
if r := recover(); r != nil {
log.Println("panic:", r)
}
}()
或运行:
GOTRACEBACK=all ./app
GODEBUG=http2debug=2 ./app
GODEBUG=schedtrace=1000 ./app
go version
file yourapp
ldd yourapp # 静态/动态链接问题
systemctl status yourappjournalctl -u yourapp -fps aux | grep yourapp-log.level=debug 或类似参数strace 看系统调用(极端情况)| 问题 | 原因 |
|---|---|
| 没日志 | 输出到 stdout 被 systemd 吞 |
| 权限错误 | Debian 严格权限 |
| OOM | Go 内存占用高 |
| 时区不对 | Debian 时区未设置 |
| 日志不刷 | 未 flush / 缓冲 |
如果你愿意,可以告诉我:
我可以给你更精准的命令和配置示例。