在 Debian 上运行 Go(Golang)程序时遇到“日志报错”,一般可以按下面几个方向来定位、查看和分析。
Go 程序常见的日志来源:
log.Println / log.Fatal查看方式:
# 如果程序在前台运行
./yourapp
# 如果日志写到文件
tail -f /var/log/yourapp.log
如果 Go 程序是通过 systemd 启动的:
# 查看服务状态
systemctl status yourapp.service
# 查看实时日志
journalctl -u yourapp.service -f
# 查看最近 100 行
journalctl -u yourapp.service -n 100
Go 的 panic、未捕获错误通常输出到 stderr:
# 运行并保存错误
./yourapp 2> error.log
cat error.log
典型日志:
panic: runtime error: invalid memory address
goroutine 1 [running]:
排查:
goroutine 堆栈.go 文件和行号listen tcp :8080: bind: address already in use
# 查看端口占用
ss -ltnp | grep 8080
# 或
lsof -i:8080
permission denied
常见原因:
ls -l /var/log/yourapp
id yourappuser
exec: "gcc": executable file not found
Debian 上常缺依赖:
sudo apt update
sudo apt install -y build-essential
GODEBUG=http2debug=2 ./yourapp
GOTRACEBACK=all ./yourapp
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.LevelDebug,
}))
✅ 程序怎么启动的(前台 / systemd / docker)
✅ 报错是 panic 还是普通 error
✅ 日志里是否有文件路径和行号
✅ 是否是权限 / 端口 / 依赖问题
✅ 是否能本地复现
你可以直接贴:
cat /etc/os-release)go version)我可以帮你逐行分析报错原因。