ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

ros2 logging - filtering by log name via executable arguments

asked 2022-07-27 11:53:54 -0500

updated 2022-07-28 00:31:28 -0500

Lets say we have two different loggers in the same program and node.

Logger l1 = rclcpp::get_logger("l1");
Logger l2 = rclcpp::get_logger("l2");

Is it possible to filter logging messages shown in the screen based on that log name using program arguments?

I am looking something similar to what we already do to setup log levels for packages when running a node. ie:

ros2 run mypkg mynode --ros-args --log-level DEBUG --log-level somepackage:=INFO

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2022-07-28 06:08:30 -0500

William gravatar image

I'm not sure what you mean by "filter". You can already control log levels independently using something like --log-level l1:=INFO. But if you mean you want to only print from one logger, then you could do something like this to filter out most other things:

% ./install/lib/demo_nodes_cpp/talker --ros-args --log-level FATAL --log-level l1:=INFO
[INFO] [1659006256.611685000] [l1]: l1 info

In that example I modified the talker demo with these lines:

diff --git a/demo_nodes_cpp/src/topics/talker.cpp b/demo_nodes_cpp/src/topics/talker.cpp
index 5058722..aae7e1b 100644
--- a/demo_nodes_cpp/src/topics/talker.cpp
+++ b/demo_nodes_cpp/src/topics/talker.cpp
@@ -37,6 +37,14 @@ public:
   explicit Talker(const rclcpp::NodeOptions & options)
   : Node("talker", options)
   {
+    rclcpp::Logger l1 = rclcpp::get_logger("l1");
+    rclcpp::Logger l2 = rclcpp::get_logger("l2");
+
+    RCLCPP_INFO(l1, "l1 info");
+    RCLCPP_INFO(l2, "l2 info");
+    RCLCPP_DEBUG(l1, "l1 debug");
+    RCLCPP_DEBUG(l2, "l2 debug");
+
     // Create a function for when messages are to be sent.
     setvbuf(stdout, NULL, _IONBF, BUFSIZ);
     auto publish_message =

You can also control more than one at a time:

% ./install/lib/demo_nodes_cpp/talker --ros-args --log-level FATAL --log-level l1:=INFO --log-level l2:=DEBUG --log-level talker:=INFO
[INFO] [1659006445.024564000] [l1]: l1 info
[INFO] [1659006445.024583000] [l2]: l2 info
[DEBUG] [1659006445.024586000] [l2]: l2 debug
[INFO] [1659006446.039449000] [talker]: Publishing: 'Hello World: 1'
[INFO] [1659006447.039343000] [talker]: Publishing: 'Hello World: 2'
[INFO] [1659006448.036625000] [talker]: Publishing: 'Hello World: 3'
[INFO] [1659006449.036557000] [talker]: Publishing: 'Hello World: 4'

Not sure if that answers your question, but if not, please provide more details.

edit flag offensive delete link more

Comments

Great! That was the way I thought was. But, I tried that and didn't work. Obviously I did something wrong. Thank you very much.

So the main conclusion I get is that logger levels in program arguments refer to logger names not packages or nodes.

Thanks.

Pablo Iñigo Blasco gravatar image Pablo Iñigo Blasco  ( 2022-07-28 08:51:18 -0500 )edit

That's right, logger names are just strings, and nodes just create a logger named after themselves, but otherwise they're not special.

William gravatar image William  ( 2022-07-28 15:29:09 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2022-07-27 11:53:54 -0500

Seen: 226 times

Last updated: Jul 28 '22