在 Debian 上实时查看 Golang 程序的日志,通常有几种常见场景,下面按最实用的方式给你总结。
如果你是前台运行程序:
go run main.go
# 或
./yourapp
日志会直接打印在终端,实时可见,无需额外操作。
nohup ./yourapp > app.log 2>&1 &
实时查看日志:
tail -f app.log
退出查看:Ctrl + C
sudo vim /etc/systemd/system/yourapp.service
示例:
[Unit]
Description=Golang App
After=network.target
[Service]
ExecStart=/opt/yourapp/yourapp
WorkingDirectory=/opt/yourapp
User=www-data
Restart=always
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl start yourapp
sudo systemctl enable yourapp
实时查看日志:
journalctl -u yourapp -f
✅ 这是 Debian 上最推荐的方式
loglog.Println("hello")
默认输出到 stderr
logrus / zap例如:
logrus.Info("start server")
只要没重定向,都能用:
tail -f app.log
# 或
journalctl -u yourapp -f
如果你是:
app.2026-01-21.loglogrotate实时查看最新日志:
tail -F app.log
(-F 比 -f 更稳,文件切换也不会断)
查看进程是否在跑:
ps aux | grep yourapp
看端口:
ss -lntp
| 场景 | 命令 |
|---|---|
| 前台运行 | 直接看终端 |
| nohup | tail -f app.log |
| systemd | journalctl -u 服务名 -f |
| 日志轮转 | tail -F app.log |
如果你是 Docker / Kubernetes / 特定日志库(zap/logrus),告诉我,我可以给你更精确的方案。