Skip to content

Commit c60d0e1

Browse files
Add logger service description (ros2#3477)
* Add logger service description Signed-off-by: Barry Xu <[email protected]> Co-authored-by: Chris Lalancette <[email protected]>
1 parent 81fd8c6 commit c60d0e1

File tree

1 file changed

+72
-12
lines changed

1 file changed

+72
-12
lines changed

source/Tutorials/Demos/Logging-and-logger-configuration.rst

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -232,26 +232,86 @@ See `the source code <https://github.com/ros2/demos/blob/{REPOS_FILE_BRANCH}/log
232232
Logger level configuration: externally
233233
--------------------------------------
234234

235-
In the future there will be a generalized approach to external configuration of loggers at runtime (similar to how `rqt_logger_level <https://wiki.ros.org/rqt_logger_level>`__ in ROS 1 allows logger configuration via remote procedural calls).
236-
**This concept is not yet officially supported in ROS 2.**
237-
In the meantime, this demo provides an **example** service that can be called externally to request configuration of logger levels for known names of loggers in the process.
235+
ROS 2 nodes have services available to configure the logging level externally at runtime.
236+
These services are disabled by default.
237+
The following code shows how to enable the logger service while creating the node.
238238

239-
The demo previously started is already running this example service.
240-
To set the level of the demo's logger back to ``INFO``\ , call the service with:
239+
.. tabs::
241240

242-
.. code-block:: bash
241+
.. group-tab:: Linux
242+
243+
.. code-block:: C++
243244

244-
ros2 service call /config_logger logging_demo/srv/ConfigLogger "{logger_name: 'logger_usage_demo', level: INFO}"
245+
// Create a node with logger service enabled
246+
auto node = std::make_shared<rclcpp::Node>("NodeWithLoggerService", rclcpp::NodeOptions().enable_logger_service(true))
245247

246-
This service call will work on any logger that is running in the process provided that you know its name.
247-
This includes the loggers in the ROS 2 core, such as ``rcl`` (the common client library package).
248-
To enable debug logging for ``rcl``, call:
248+
.. group-tab:: Python
249+
250+
.. code-block:: python
251+
252+
# Create a node with logger service enabled
253+
node = Node('NodeWithLoggerService', enable_logger_service=True)
254+
255+
If you run one of the nodes as configured above, you will find 2 services when running ``ros2 service list``:
249256

250257
.. code-block:: bash
251258
252-
ros2 service call /config_logger logging_demo/srv/ConfigLogger "{logger_name: 'rcl', level: DEBUG}"
259+
$ ros2 service list
260+
...
261+
/NodeWithLoggerService/get_logger_levels
262+
/NodeWithLoggerService/set_logger_levels
263+
...
264+
265+
* get_logger_levels
266+
267+
Use this service to get logger levels for specified logger names.
268+
269+
Run ``ros2 service call`` to get logger levels for ``NodeWithLoggerService`` and ``rcl``.
270+
271+
.. code-block:: bash
272+
273+
$ ros2 service call /NodeWithLoggerService/get_logger_levels rcl_interfaces/srv/GetLoggerLevels '{names: ["NodeWithLoggerService", "rcl"]}'
274+
275+
requester: making request: rcl_interfaces.srv.GetLoggerLevels_Request(names=['NodeWithLoggerService', 'rcl'])
276+
277+
response:
278+
rcl_interfaces.srv.GetLoggerLevels_Response(levels=[rcl_interfaces.msg.LoggerLevel(name='NodeWithLoggerService', level=0), rcl_interfaces.msg.LoggerLevel(name='rcl', level=0)])
279+
280+
* set_logger_levels
281+
282+
Use this service to set logger levels for specified logger names.
283+
284+
Run ``ros2 service call`` to set logger levels for ``NodeWithLoggerService`` and ``rcl``.
285+
286+
.. code-block:: bash
287+
288+
$ ros2 service call /NodeWithLoggerService/set_logger_levels rcl_interfaces/srv/SetLoggerLevels '{levels: [{name: "NodeWithLoggerService", level: 20}, {name: "rcl", level: 10}]}'
289+
290+
requester: making request: rcl_interfaces.srv.SetLoggerLevels_Request(levels=[rcl_interfaces.msg.LoggerLevel(name='NodeWithLoggerService', level=20), rcl_interfaces.msg.LoggerLevel(name='rcl', level=10)])
291+
292+
response:
293+
rcl_interfaces.srv.SetLoggerLevels_Response(results=[rcl_interfaces.msg.SetLoggerLevelsResult(successful=True, reason=''), rcl_interfaces.msg.SetLoggerLevelsResult(successful=True, reason='')])
294+
295+
296+
There is also demo code showing how to set or get the logger level via the logger service.
297+
298+
* rclcpp: `demo code <https://github.com/ros2/demos/tree/{REPOS_FILE_BRANCH}/demo_nodes_cpp/src/logging/use_logger_service.cpp>`__
299+
300+
.. code-block:: bash
301+
302+
$ ros2 run demo_nodes_cpp use_logger_service
303+
304+
* rclpy: `demo code <https://github.com/ros2/demos/tree/{REPOS_FILE_BRANCH}/demo_nodes_py/demo_nodes_py/logging/use_logger_service.py>`__
305+
306+
.. code-block:: bash
307+
308+
$ ros2 run demo_nodes_py use_logger_service
309+
310+
.. warning::
253311

254-
You should see debug output from ``rcl`` start to show.
312+
Currently, there is a limitation that ``get_logger_levels`` and ``set_logger_levels`` services are not thread-safe.
313+
This means that you need to ensure that only one thread is calling the services at a time.
314+
Please see the details in https://github.com/ros2/rcutils/issues/397
255315

256316
Using the logger config component
257317
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)