在 'ros2 launch' 期间启用 DEBUG 日志记录的 ROS2 技巧

问题

当你 ros2 launch 一个 ROS 包时,默认日志级别通常设置为 INFO,因此在遇到错误时你不会看到有用的信息,例如:

enable_ros2_debug.sh
[INFO] [launch]: All log files can be found below /home/user/.ros/log/2025-01-24-03-53-34-995687-user-desktop-2530786
[INFO] [launch]: Default logging verbosity is set to INFO
[ERROR] [launch]: Caught exception in launch (see debug for traceback): substitution args not supported:  No module named 'roslaunch'

解决方案

有一个功能请求允许通过命令行参数设置 ros2 launch 的日志级别。这尚未实现。

你可以选择显著修改 launch 文件(详见功能请求),或者你可以使用以下技巧

在你的 launch 文件顶部,添加以下 Python 代码

enable_debug_logging.py
import logging
logging.root.setLevel(logging.DEBUG)

虽然 Default logging verbosity is set to INFO 消息仍会被打印(因为它在你的 Python 文件之前执行),但大多数模块现在将以 DEBUG 级别记录日志。


Check out similar posts by category: ROS