GitLab System Administration - Hands-on Lab: Manage GitLab Logs

This Hands-On Guide walks you through managing GitLab logs on a virtual machine."

Estimated time to complete: 30 minutes

Task A. View active logs

The gitlab-ctl command allows you to tail all GitLab log files as well as filter by GitLab service.

  1. From a shell session on your GitLab instance, run the following command to view all active GitLab logs.
1
sudo gitlab-ctl tail

Amidst all the output, you should notice the command shows the full file path to each log. Most GitLab logs live in /var/log/gitlab. (Note: You can type CTRL-C to exit the tail command.)

  1. You can also view GitLab logs by service. Run the following command to view only NGINX logs (i.e. log files in /var/log/gitlab/nginx).
1
sudo gitlab-ctl tail nginx

You should now see the most recent entries of log files specific to the NGINX web server.

  1. Finally, you can drill down to an individual log file.
1
sudo gitlab-ctl tail nginx/gitlab_access.log

Task B. Set minimum log levels

Admins are able to set minimum log levels for some GitLab services. Note that only some services such as NGINX and Gitaly let admins change the minimum logging level, and even then only for some log files. The log_level for other services, such as Sidekiq and Redis, cannot be changed.

  1. Check the current minimum log levels for GitLab services.
1
sudo grep -n -E 'log_level|logging_level' /etc/gitlab/gitlab.rb
  1. Note the line number for nginx['logging_level'].

  2. Change the minimum log level for nginx. Replace “1731” with the appropriate line number from the grep output in the previous step.

1
2
sudo sed -i '1731s/warn/error/' /etc/gitlab/gitlab.rb
sudo sed -i '1731s/# //' /etc/gitlab/gitlab.rb
  1. Re-run the grep command from Step 1 to verify the line was modified as intended.

  2. Reconfigure to apply the changes.

1
sudo gitlab-ctl reconfigure

Task C. Manage log retention

GitLab uses logrotate to manage retention of all logs except those managed by the runit service manager (runit uses a separate service logging daemon called svlogd). Log retention can be configured in /etc/gitlab/gitlab.rb.

  1. Examine default logrotate retention settings.
1
sudo grep -n 'logrotate' /etc/gitlab/gitlab.rb
  1. Optional: View the default retention settings for the runit-managed logs.
1
sudo grep -n 'svlogd' /etc/gitlab/gitlab.rb
  1. It appears logrotate (and svlogd) rotate log files every day, and retain 30 days worth of logs. We can verify this by looking inside the service log directories.
1
sudo ls /var/log/gitlab/puma

Note the gzipped archive files for Puma’s stdout and stderr logs from previous days.

  1. Change logrotate’s behavior to rotate log files weekly. As before, modify the line sed edits accordingly using the line number from the grep output.
1
2
sudo sed -i '1234s/daily/weekly/g' /etc/gitlab/gitlab.rb
sudo sed -i '1234s/# //' /etc/gitlab/gitlab.rb
  1. Change logrotate’s retention period to 1 year of retained log files. As before, modify the line sed edits accordingly using the line number from the grep output.
1
2
sudo sed -i '1234s/30/52/g' /etc/gitlab/gitlab.rb
sudo sed -i '1234s/# //' /etc/gitlab/gitlab.rb
  1. Run the following again to ensure your changes are properly written to gitlab.rb.
1
sudo grep -n 'logrotate' /etc/gitlab/gitlab.rb
  1. Reconfigure to apply the changes.
1
sudo gitlab-ctl reconfigure

Task D. Change log formatting

Many logs are JSON formatted by default. Admins may wish to configure text formatting depending on the log ingestion system used, or for readability.

  1. Check the current log formats for Gitaly.

    1
    
    sudo grep -n -F "gitaly['configuration']" -A20 /etc/gitlab/gitlab.rb 
    

    This command will find the start of the gitaly configuration with grep, then display the 20 lines that follow using the -A20 flag. This will show the full configuration of the Gitaly configuration file

  2. Within the output, locate the following lines and note their line numbers

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    # gitaly['configuration'] = {
    ...
    #   logging: {
    #     dir: "/var/log/gitlab/gitaly",
    #     level: 'warn',
    #     format: 'json',
    #     sentry_dsn: 'https://<key>:<secret>@sentry.io/<project>',
    #     sentry_environment: 'production',
    # },
    
  3. Run sudo gitlab-ctl tail gitaly/current to see the current JSON output for Gitaly logging.

  4. Change Gitaly’s log format from JSON to text formatting. In your preferred text editor, locate the line gitaly['configuration]. Inside this configuration object, you will see a logging configuration similar to below:

    1
    2
    3
    4
    5
    6
    
    sudo sed -i '2588s/json/text/' /etc/gitlab/gitlab.rb
    sudo sed -i '2574s/# //' /etc/gitlab/gitlab.rb 
    sudo sed -i '2588s/# //' /etc/gitlab/gitlab.rb
    sudo sed -i '2585s/# //' /etc/gitlab/gitlab.rb 
    sudo sed -i '2591s/# //' /etc/gitlab/gitlab.rb 
    sudo sed -i '2591s/,/ }/' /etc/gitlab/gitlab.rb
    

    With this sed commands, you are first replacing the JSON format with text. Next, you are removing the comments in front of the format and Gitaly configuration blocks to enable them.

  5. Rerun your grep command to view your configuration: sudo grep -n -F "gitaly['configuration']" -A20 /etc/gitlab/gitlab.rb . The end result will look similar to below:

1
2
3
4
5
6
7
8
9
gitaly['configuration'] = {
...
   logging: {
#     dir: "/var/log/gitlab/gitaly",
#     level: 'warn',
      format: 'text'
#     sentry_dsn: 'https://<key>:<secret>@sentry.io/<project>',
#     sentry_environment: 'production',
}}
  1. Reconfigure to apply the change.
1
sudo gitlab-ctl reconfigure
  1. Verify the updated formatting.
1
sudo gitlab-ctl tail gitaly/current

You should see the log output is now text formatted instead of JSON formatted.

Lab Guide Complete

You have completed this lab exercise. You can view the other lab guides for this course.

Suggestions?

If you’d like to suggest changes to the GitLab System Admin Basics Hands-on Guide, please submit them via merge request.

Last modified April 26, 2024: Update Lab Descriptions (05f28a88)