I have been using the ROS 2 rosbag2_recorder package to record message data for analysis and testing purposes. Recently, I've been exploring the possibility of capturing only error messages in my bag files.

I noticed that the /rosbag2_recorder/snapshot service provided by the package allows me to take a snapshot of the ROS 2 system's message data at a particular moment. However, I would like to know if it is possible to configure this service call in a way that it captures only error messages.

I am aware that the rosbag2_interfaces/Snapshot service request message has parameters like topics and max_cache_size, but I couldn't find any specific option or flag related to capturing only error messages.

Could someone please guide me on how I can modify the rosbag2_recorder service call to capture only error messages? Are there any additional parameters or configurations that can be used for this purpose? Or is there an alternative approach to achieve this goal?

details about snapshot: here

Any suggestions, insights, or examples would be greatly appreciated.

Thank you in advance for your help!


1 Answer 1


Welcome to Robotics, Dragon. You're mentioning wanting to make a rosbag, but then you also mention wanting to "capture error messages." Error messages are logged, and it's not super clear to me what you're trying to do, so my first suggestion would be that you investigate logging options and see if a log file is sufficient for your needs.

If you need to have logs included with your rosbag, you could also add the /rosout_agg topic to be included with your bagfile and you'd have all of the logs included.

If you need only the error messages and you need them included in the bagfile, then you could consider writing a re-publisher that checks the log level from messages published to /rosout_agg and then, if they're at or above ERROR, write them to a new topic like /rosout_errors. Then you can add /rosout_errors to your list of topics for the rosbag recording.

You're mentioning ROS2, which I'm not currently using, but for Python and ROS this would look like the following:

import rospy
from rosgraph_msgs.msg import Log

def callback(data):
    if data.level >= Log.ERROR:

pub = rospy.Publisher('rosout_errors', Log, queue_size=10)
rospy.init_node('rosout_republisher', anonymous=True)
rospy.Subscriber("rosout_agg", Log, callback)

I believe the primary difference you'd have to make here is that in ROS2 the Log message is in rcl_interfaces instead of rosgraph_msgs, but again I don't have ROS2 and can't verify that. The code I posted above works for ROS.

  • $\begingroup$ Thank you for the response @Chuck, i'm trying to record the ros2 bag file using python and these ros2 bags file can be used later on for testing purpose. But these files are too big to store for a week and used for the testing. So, i'm thinking is there any way to trigger the ros2 bag so that it can only record ERROR log from all the topics and in this way the size of file will be less instead of storing all the msg from the topics. $\endgroup$
    – Dragon
    Jun 1, 2023 at 10:46
  • $\begingroup$ @Dragon It's not clear to me what exactly you're trying to do or what issue you're having, then. If it's a matter of storage size then you need more disk space and there's no other way around that. The answer here is based on your problem statement - Could someone please guide me on how I can modify the rosbag2_recorder service call to capture only error messages? The mechanism I posted reads all ros messages, picks only the ones that are errors (or fatal) and re-publishes them to a new topic. Then you can subscribe to the new topic when creating your ROS bag. $\endgroup$
    – Chuck
    Jun 1, 2023 at 10:52
  • $\begingroup$ So in ROS2 the have snapshot mode here to trigger the ros2 bag to record whenever you want but again it can record everything from the topics, and in my case i just eant to trigger when an error msg is arrived on the topic. $\endgroup$
    – Dragon
    Jun 1, 2023 at 10:53
  • $\begingroup$ The rosbag record should only capture messages on the topics you specify, so if you rosbag record -o "my_test_errors.bag" "/rosout_errors" with something publishing on /rosout_errors (as provided above) then you could let rosbag record run for a year and if there is only one message captured in that time then the output rosbag only has one message in it. $\endgroup$
    – Chuck
    Jun 1, 2023 at 10:56
  • $\begingroup$ Thank you for your input. i will try this way. $\endgroup$
    – Dragon
    Jun 1, 2023 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.