Linux 软件包安装日志
- Tier: 基础版, 专业版, 旗舰版
- Offering: 私有化部署
极狐GitLab 包含一个高级日志系统,极狐GitLab 中的每个服务和组件都会输出系统日志。以下是管理这些日志在 Linux 软件包安装中的配置设置和工具。
在服务器上的控制台中查看日志
如果你想要 "tail" ,即查看极狐GitLab 日志的实时更新,你可以使用 gitlab-ctl tail。
shell1# Tail all logs; press Ctrl-C to exit 2sudo gitlab-ctl tail 3 4# Drill down to a sub-directory of /var/log/gitlab 5sudo gitlab-ctl tail gitlab-rails 6 7# Drill down to an individual file 8sudo gitlab-ctl tail nginx/gitlab_error.log
在控制台中查看日志并保存到文件
通常情况下,同时在控制台中显示日志并将其保存到文件以便于稍后调试/分析是很有用的。你可以使用 tee 工具来实现这一点。
shell# Use 'tee' to tail all the logs to STDOUT and write to a file at the same time sudo gitlab-ctl tail | tee --append /tmp/gitlab_tail.log
配置默认日志目录
在你的 /etc/gitlab/gitlab.rb 文件中,有许多 log_directory 键用于不同类型的日志。取消注释并更新你想要放置在其他位置的所有日志的值:
ruby# For example: gitlab_rails['log_directory'] = "/var/log/gitlab/gitlab-rails" puma['log_directory'] = "/var/log/gitlab/puma" registry['log_directory'] = "/var/log/gitlab/registry" ...
Gitaly 和 Mattermost 有不同的日志目录配置:
ruby1gitaly['configuration'] = { 2 logging: { 3 dir: "/var/log/gitlab/registry" 4 } 5} 6mattermost['log_file_directory'] = "/var/log/gitlab/registry"
运行 sudo gitlab-ctl reconfigure 以使用这些设置配置你的实例。
runit 日志
在 Linux 软件包安装中,由 runit 管理 的服务使用 svlogd 生成日志数据。
- 日志写入一个名为 current 的文件。
- 定期,这个日志会被压缩并使用 TAI64N 格式重命名,例如:@400000005f8eaf6f1a80ef5c.s。
- 压缩日志上的文件系统时间戳将与极狐GitLab 最后一次写入该文件的时间一致。
- zmore 和 zgrep 允许查看和搜索压缩或未压缩的日志。
你可以通过 /etc/gitlab/gitlab.rb 使用以下设置修改 svlogd 设置:
ruby1# Below are the default values 2logging['svlogd_size'] = 200 * 1024 * 1024 # rotate after 200 MB of log data 3logging['svlogd_num'] = 30 # keep 30 rotated log files 4logging['svlogd_timeout'] = 24 * 60 * 60 # rotate after 24 hours 5logging['svlogd_filter'] = "gzip" # compress logs with gzip 6logging['svlogd_udp'] = nil # transmit log messages via UDP 7logging['svlogd_prefix'] = nil # custom prefix for log messages 8 9# Optionally, you can override the prefix for e.g. Nginx 10nginx['svlogd_prefix'] = "nginx"
Logrotate
极狐GitLab 内置的 logrotate 服务管理所有日志,除了那些由 runit 捕获的。这项服务将轮换、压缩,并最终删除日志数据,如 gitlab-rails/production.log 和 nginx/gitlab_access.log。你可以在 /etc/gitlab/gitlab.rb 中配置常见的 logrotate 设置、配置每个服务的 logrotate 设置,并完全禁用 logrotate。
配置常见的 logrotate 设置
所有 logrotate 服务通用的设置可以在 /etc/gitlab/gitlab.rb 文件中设置。这些设置对应于每个服务的 logrotate 配置文件中的配置选项。查看 logrotate 手册页 (man logrotate) 以获取详细信息。
ruby1logging['logrotate_frequency'] = "daily" # rotate logs daily 2logging['logrotate_maxsize'] = nil # logs will be rotated when they grow bigger than size specified for `maxsize`, even before the specified time interval (daily, weekly, monthly, or yearly) 3logging['logrotate_size'] = nil # do not rotate by size by default 4logging['logrotate_rotate'] = 30 # keep 30 rotated logs 5logging['logrotate_compress'] = "compress" # see 'man logrotate' 6logging['logrotate_method'] = "copytruncate" # see 'man logrotate' 7logging['logrotate_postrotate'] = nil # no postrotate command by default 8logging['logrotate_dateformat'] = nil # use date extensions for rotated files rather than numbers e.g. a value of "-%Y-%m-%d" would give rotated files like production.log-2016-03-09.gz
配置单个服务的 logrotate 设置
你可以通过 /etc/gitlab/gitlab.rb 自定义每个单独服务的 logrotate 设置。例如,要为 nginx 服务自定义 logrotate 频率和大小,可以使用:
rubynginx['logrotate_frequency'] = nil nginx['logrotate_size'] = "200M"
禁用 logrotate
你还可以在 /etc/gitlab/gitlab.rb 中使用以下设置禁用内置的 logrotate 服务:
rubylogrotate['enable'] = false
Logrotate notifempty 设置
logrotate 服务以 notifempty 的不可配置默认值运行,解决以下问题:
- 不必要地旋转空日志,并且通常存储许多空日志。
- 一次性日志对长期故障排除有用,但在 30 天后被删除,例如数据库迁移日志。
Logrotate 一次性和空日志处理
现在日志根据需要由 logrotate 旋转和重新创建,一次性日志仅在更改时旋转。设置此设置后,可以进行一些整理:
- 可以删除空的一次性日志,如 gitlab-rails/gitlab-rails-db-migrate*.log。
- 由旧版本的极狐GitLab 旋转和压缩的空日志。这些空日志通常大小为 20 字节。
手动运行 logrotate
Logrotate 是一个计划任务,但它也可以按需触发。
要使用 logrotate 手动触发极狐GitLab 日志旋转,请使用以下命令:
shell/opt/gitlab/embedded/sbin/logrotate -fv -s /var/opt/gitlab/logrotate/logrotate.status /var/opt/gitlab/logrotate/logrotate.conf
增加 logrotate 触发频率
logrotate 脚本每 50 分钟触发一次,并在尝试旋转日志之前等待 10 分钟。
要修改这些值:
-
编辑 /etc/gitlab/gitlab.rb:
rubylogrotate['pre_sleep'] = 600 # sleep 10 minutes before rotating after start-up logrotate['post_sleep'] = 3000 # wait 50 minutes after rotating
-
重新配置极狐GitLab:
shellsudo gitlab-ctl reconfigure
UDP 日志转发
- Tier: 专业版, 旗舰版
- Offering: 极狐GitLab私有化部署
Linux 软件包安装可以使用 svlogd 中的 UDP 日志功能,并通过 UDP 将非 svlogd 日志发送到兼容 syslog 的远程系统。要配置 Linux 软件包安装以通过 UDP 发送 syslog 协议消息,请使用以下设置:
rubylogging['udp_log_shipping_host'] = '1.2.3.4' # Your syslog server # logging['udp_log_shipping_hostname'] = nil # Optional, defaults the system hostname logging['udp_log_shipping_port'] = 1514 # Optional, defaults to 514 (syslog)
设置 udp_log_shipping_host 将为每个 runit 管理 的服务添加一个 svlogd_prefix 用于指定的主机名和服务。
示例日志消息:
plaintext1Jun 26 06:33:46 ubuntu1204-test production.log: Started GET "/root/my-project/import" for 127.0.0.1 at 2014-06-26 06:33:46 -0700 2Jun 26 06:33:46 ubuntu1204-test production.log: Processing by ProjectsController#import as HTML 3Jun 26 06:33:46 ubuntu1204-test production.log: Parameters: {"id"=>"root/my-project"} 4Jun 26 06:33:46 ubuntu1204-test production.log: Completed 200 OK in 122ms (Views: 71.9ms | ActiveRecord: 12.2ms) 5Jun 26 06:33:46 ubuntu1204-test gitlab_access.log: 172.16.228.1 - - [26/Jun/2014:06:33:46 -0700] "GET /root/my-project/import HTTP/1.1" 200 5775 "https://172.16.228.169/root/my-project/import" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36" 62014-06-26_13:33:46.49866 ubuntu1204-test sidekiq: 2014-06-26T13:33:46Z 18107 TID-7nbj0 Sidekiq::Extensions::DelayedMailer JID-bbfb118dd1db20f6c39f5b50 INFO: start 72014-06-26_13:33:46.52608 ubuntu1204-test sidekiq: 2014-06-26T13:33:46Z 18107 TID-7muoc RepositoryImportWorker JID-57ee926c3655fcfa062338ae INFO: start
使用自定义 NGINX 日志格式
默认情况下,NGINX 访问日志将使用 "combined" NGINX 格式的一个版本,设计用来隐藏嵌入查询字符串中的潜在敏感信息。如果你想使用自定义日志格式字符串,可以在 /etc/gitlab/gitlab.rb 中指定它。
rubynginx['log_format'] = 'my format string $foo $bar' mattermost_nginx['log_format'] = 'my format string $foo $bar'
JSON 日志
结构化日志可以通过 JSON 导出,以便由 Elasticsearch、Splunk 或其他日志管理系统解析。默认情况下,所有支持的服务都启用了 JSON 格式。
PostgreSQL 在没有外部插件的情况下不支持 JSON 日志。然而,它支持 CSV 格式的日志:
rubypostgresql['log_destination'] = 'csvlog' postgresql['logging_collector'] = 'on'
需要重启数据库以使其生效。
文本日志
已经建立日志收集系统的客户可能不希望使用 JSON 日志格式。可以通过在 /etc/gitlab/gitlab.rb 中设置以下内容来配置文本格式,然后运行 gitlab-ctl reconfigure:
ruby1gitaly['configuration'] = { 2 logging: { 3 format: "" 4 } 5} 6gitlab_shell['log_format'] = 'text' 7gitlab_workhorse['log_format'] = 'text' 8registry['log_formatter'] = 'text' 9sidekiq['log_format'] = 'text' 10gitlab_pages['log_format'] = 'text'
根据涉及的服务,日志格式中的属性名称有一些变化(例如,容器注册表使用 log_formatter,Gitaly 和 Praefect 都使用 logging_format)。
rbtrace
极狐GitLab 附带了 rbtrace,它允许你跟踪 Ruby 代码,查看所有运行的线程,获取内存转储等。然而,这不是默认启用的。要启用它,将 ENABLE_RBTRACE 变量定义到环境中:
rubygitlab_rails['env'] = {"ENABLE_RBTRACE" => "1"}
然后重新配置系统并重启 Puma 和 Sidekiq。要在 Linux 软件包安装中运行此命令,请以 root 身份运行:
ruby/opt/gitlab/embedded/bin/ruby /opt/gitlab/embedded/bin/rbtrace
配置日志级别/详细程度
你可以配置极狐GitLab Rails、容器注册表、极狐GitLab Shell 和 Gitaly 的最低日志级别(详细程度):
-
编辑 /etc/gitlab/gitlab.rb 并设置日志级别:
ruby1gitlab_rails['env'] = { 2 "GITLAB_LOG_LEVEL" => "WARN", 3} 4registry['log_level'] = 'info' 5gitlab_shell['log_level'] = 'INFO' 6gitaly['configuration'] = { 7 logging: { 8 level: "warn" 9 } 10}
-
重新配置极狐GitLab:
shellsudo gitlab-ctl reconfigure
你不能编辑某些极狐GitLab 日志的 log_level,例如 production_json.log、graphql_json.log 等。另见覆盖默认日志级别。
设置自定义日志群组
极狐GitLab 支持为配置的日志目录分配一个自定义群组。
可以配置全局 logging['log_group'] 设置在你的 /etc/gitlab/gitlab.rb 文件中,也可以配置每个服务的 log_group 设置,例如 gitaly['log_group']。添加 log_group 设置时,你需要运行 sudo gitlab-ctl reconfigure 来配置你的实例。
设置全局或每个服务的 log_group 将:
- 更改每个服务日志目录(或使用全局设置的所有日志目录)的权限为 0750,以允许配置的群组成员读取日志目录的内容。
- 配置 runit 使用指定的 log_group 写入和旋转日志:无论是每个服务还是所有 runit 管理的服务。
自定义日志群组限制
不由 runit 管理的服务(例如 /var/log/gitlab/gitlab-rails 中的 gitlab-rails 日志)的日志将不继承配置的 log_group 设置。
群组必须已经存在于主机上。Linux 软件包安装不会在运行 sudo gitlab-ctl reconfigure 时创建群组。