rospy overview: Initialization and Shutdown | Messages | Publishers and Subscribers | Services | Parameter Server | Logging | Names and Node Information | Time | Exceptions | tf/Overview | tf/Tutorials | Python Style Guide

ROS has its own topic-based mechanism, called rosout for recording log messages from nodes. These log messages are human-readable string messages that convey the status of a node.

rospy has several methods for writing log messages, all starting with "log":

rospy.logdebug(msg, *args)
rospy.loginfo(msg, *args)
rospy.logwarn(msg, *args)
rospy.logerr(msg, *args)
rospy.logfatal(msg, *args)

These levels have a one-to-one correspondence to ROS' logging verbosity levels.

Each rospy.log*() method can take in a string msg. If msg is a format string, you can pass in the arguments for the string separately, e.g.

rospy.logerr("%s returned the invalid value %s", other_name, other_value)

Reading log messages

There are four potential places a log message may end up depending on the verbosity level:

stdout

  • loginfo. Note that this may not be sent to the screen depending on the value of the roslaunch/XML/node output parameter.

stderr

  • logerr, logfatal, and logwarn.

Node log file

  • all. Your node's log file will be in ROS_ROOT/log or ~/.ros/log, unless you override it with the ROS_LOG_DIR environment variable. If you are using roslaunch, you can use the roslaunch-logs command to tell you the location of the log directory. See also: Override Logging Configuration.

/rosout topic

  • loginfo, logwarn, logerr, and logfatal. Your messages will not appear on the /rosout topic until your node is fully initialized, so you may not see the initial messages. If you wish to see logdebug messages on /rosout, you can pass in the log_level parameter to rospy.init_node(), e.g.:

    rospy.init_node('my_node', log_level=rospy.DEBUG)

Here is a table summarizing the above:

Debug

Info

Warn

Error

Fatal

stdout

X

stderr

X

X

X

log file

X

X

X

X

X

/rosout

o

X

X

X

X

Also note that this table is different for roscpp.

Example

Here's a quick example with a talker Node:

   1     topic = 'chatter'
   2     pub = rospy.Publisher(topic, String)
   3     rospy.init_node('talker', anonymous=True)
   4     rospy.loginfo("I will publish to the topic %s", topic)
   5     while not rospy.is_shutdown():
   6         str = "hello world %s"%rospy.get_time()
   7         rospy.loginfo(str)
   8         pub.publish(str)
   9         rospy.sleep(0.1)

Logging Periodically

New in Kinetic

Since Kinetic, rospy supports writing log messages periodically. The output will print a message at most once per "period" by rospy.log*_throttle(period, msg).

   1 while True:
   2     rospy.loginfo_throttle(60, "This message will print every 60 seconds")

Logging Once

New in Lunar

Since Lunar, rospy supports writing log messages only once after it is spawned. The output will print a message once by rospy.log*_once(msg).

   1 while True:
   2     rospy.loginfo_once("This message will print only once")

Advanced: Override Logging Configuration

By default, rospy and other ROS python libraries use $ROS_ROOT/../../etc/ros/python_logging.conf. This file is the standard fileConfig format used by the Python logging module (see https://docs.python.org/library/logging.config.html#configuration-file-format).

You can override the location of this configuration file by setting the ROS_PYTHON_LOG_CONFIG_FILE environment variable.

New in Lunar

Since Lunar, a yaml file (by default $ROS_ROOT/../../etc/ros/python_logging.yaml but it is reconfigurable as well) can be used to configure python logging as explained in the python logging documentation. This more recent configuration format will give you access to more settings than the traditional one. The recent diff related to this feature is on the github repo.

Logger Level GUI

rqt_logger_level provides a GUI to change rospy's logger level during runtime.

Wiki: rospy/Overview/Logging (last edited 2017-11-11 02:19:48 by AlexV)