GitLab在Ubuntu系統中的日志集中存儲在/var/log/gitlab/目錄下,按組件分類(如Rails應用、Sidekiq后臺任務、Nginx反向代理等)。常用日志文件及查看方式如下:
/var/log/gitlab/gitlab-rails/production.log,可使用tail -f實時查看最新請求與錯誤。/var/log/gitlab/sidekiq/current,適合排查任務失敗問題。/var/log/gitlab/nginx/gitlab_access.log(訪問日志)和/var/log/gitlab/nginx/gitlab_error.log(錯誤日志)。/var/log/postgresql/postgresql-<version>-main.log,用于排查數據庫性能或連接問題。此外,可使用gitlab-ctl tail命令查看所有GitLab服務的實時日志,或指定特定服務(如gitlab-ctl tail nginx查看Nginx日志);通過journalctl -u gitlab-runsvdir查看GitLab服務的系統級日志。
sudo gitlab-ctl status檢查各組件狀態(如unicorn、sidekiq、nginx),若某組件顯示“down”或“fail”,則為故障點。sudo gitlab-ctl tail unicorn),定位具體錯誤(如端口沖突、配置文件錯誤)。/etc/gitlab/gitlab.rb中external_url設置錯誤)、資源不足(如內存不足導致unicorn無法啟動)。sudo systemctl stop <service_name>);修正配置文件后運行sudo gitlab-ctl reconfigure重新加載配置;增加系統內存或調整GitLab資源限制(如unicorn的worker_processes)。/var/log/gitlab/nginx/gitlab_error.log),常見提示為“upstream prematurely closed connection”。/var/log/gitlab/unicorn/current),若日志顯示“failed to start a new unicorn master”,則unicorn無法啟動。/var/opt/gitlab/unicorn/pid),若存在殘留PID文件,刪除后重啟GitLab(sudo gitlab-ctl restart);確保unicorn配置(/etc/gitlab/gitlab.rb中的unicorn['port'])與Nginx配置一致。/var/log/gitlab/sidekiq/current),定位任務執行中的錯誤(如依賴安裝失敗、腳本執行錯誤)。.gitlab-ci.yml),確保語法正確(如縮進、指令格式);確認Runner已正確注冊(gitlab-runner status)且具有執行任務的權限;安裝任務所需的依賴(如在流水線中添加apt-get install -y <package>)。~/.ssh/id_rsa.pub)是否已添加到GitLab賬戶的“SSH Keys”設置中,或HTTPS令牌是否有效。/var/log/gitlab/gitlab-rails/production.log),搜索“permission denied”關鍵詞,確認是用戶權限還是項目權限問題。top或htop命令查看系統資源使用情況,若CPU、內存占用率持續高于80%,則需優化。/var/log/gitlab/gitlab-rails/production.log),搜索“out of memory”或“timeout”關鍵詞,確認是否因資源不足導致任務失敗。sudo fallocate -l 2G /swapfile→sudo chmod 600 /swapfile→sudo mkswap /swapfile→sudo swapon /swapfile);清理過期流水線的緩存與構建產物(gitlab-rake gitlab:cleanup:orphan_job_artifacts);優化unicorn配置(如減少worker_processes數量)。logrotate工具自動壓縮或刪除舊日志(/etc/logrotate.d/gitlab),避免日志文件占用過多磁盤空間。sudo gitlab-backup create),將備份文件保存至異地(如云存儲),故障時可快速恢復(sudo gitlab-backup restore BACKUP=xxx)。