Hey everyone,
Lately I’ve been experimenting a little bit with the logging system in ros2, and thought it would be a fun project to just try out what would happen if we were to use spdlog as a underlying system for it. I’ve seen at least some interest from some folks here that wanted to use fmt for example (like ROS2 logging using FMT - ROS General - Open Robotics Discourse and GitHub - xqms/rosfmt: ROS wrapper for the fmt library), so I also wanted to take a crack at it and see what I could do ![]()
My approach works by disabling the auto-initialization of the logging system in rclcpp::init(), and by manually setting rcl global logging output handler to my custom (thread-safe) sink, which parses it with the same printf formatting, and then forwards it to its respective spdlog logger
This means that you can use spdlog for yourself, and enjoy all the benefits of the fmtlib, while maintaining compatiblity with other ros2 third party libraries ![]()
This also makes all log calls (optional, on by default) asynchronous, which could lead to some performance improvements, and this makes it super easy to customize, since you can just add all your custom spdlog sinks and all log calls will be forwarded to these (including from all of these third party libraries using rclcpp logging macros)!
You can find the project here!
https://github.com/jncfa/ros2_spdlog_logger/
If you have any comments or suggestions I would be really glad to hear them out!!
I think it would be wise to leave a small disclaimer about this package though: while this will work perfectly fine for most people, I am quite certain that the rcl logging system isn’t meant to be abused in this manner so please do keep that in mind, this was mostly done for fun and to learn about the internals of rcl/rclcpp ![]()
Also, I am aware that ros2 uses spdlog to write the file logs, but I did not find this flexible enough for what I wanted to do, I had some different ideas in mind ![]()
To see an example of how this works, you can check out this example code: