CodeTheRobot

Ubuntu Linux for Robotics · Lesson 89 of 96

Logging and metrics

journald, log rotation, ROS log levels and collecting metrics from a running robot.

Estimated time · 45–75 min

Learning goals

What you will understand

  • Explain logging and metrics in your own words
  • Use it in a small workstation example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

On Ubuntu a robot's services log to the systemd journal (journalctl -u service), ROS 2 writes its own logs to ~/.ros/log, and system health comes from tools such as top, free, df, sensors and vcgencmd or tegrastats. Limit journal size (journalctl --vacuum-size) so logs never fill the disk.

Metrics show trends: loop rate, message age, CPU and memory, temperature, battery, error counts. Publish them as a ROS topic and collect them with Prometheus or a bag, and watch percentiles (p95, p99), not just averages. Rotate or delete old rosbags automatically.

Guided lesson

Work through these steps

  1. 1.

    Open a terminal and record the current state before changing anything.

  2. 2.

    Read the logging and metrics commands and predict what each one does.

  3. 3.

    Run the commands one at a time and compare each output with your prediction.

  4. 4.

    Write the commands that worked in a setup note you can repeat later.

Worked example

Read it before you run it

journalctl -u robot-bringup.service --since '10 min ago'
# Read the last ten minutes of the robot's service log.
journalctl --disk-usage
# See how much space the journal uses.
sudo journalctl --vacuum-size=500M
# Limit the journal to 500 MB.
du -sh ~/.ros/log
# Check the size of ROS logs.
ros2 topic hz /diagnostics
# Check that the diagnostics are published regularly.
find ~/bags -mtime +14 -type d -exec rm -r {} +
# Delete recordings older than 14 days (check the path first).

What the example is doing

  1. 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
  2. 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
  3. 3.Comments beginning with # explain safety checks and are not executed by the shell.

Watch it run · The robot's computer

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

robot computercpumemdiskdriverstoppedplannerstoppedloggerstoppedsafetystoppedTHE ROBOT'S COMPUTER

Demo for Logging and metrics. Expected result: The size of the journal on disk

Practice code

Now type it yourself

Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.

# TODO: print how much disk space the systemd journal uses

Expected output

The size of the journal on disk
Stuck? Show the full solution (try it yourself first)
journalctl --disk-usage

Expected result

How you know it worked

  • Each command completes without an unexplained error.
  • Your notes contain the command, the important output, and what it proves.

Practical exercise

Apply the lesson

Read a service's log for the last hour, limit the journal to 500 MB, write a cron job that deletes bags older than 14 days, and log CPU temperature and memory to a file every 10 seconds.

Before you continue

  • You recorded the starting state
  • Every command and output is understood
  • The change can be repeated from your notes

If something goes wrong

A command says permission denied.

Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.

A command or package is not found.

Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.