Skip to content

Instantly share code, notes, and snippets.

@jdrew1303
Forked from nzjrs/rospy_logging.py
Created December 26, 2019 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdrew1303/1b3c4538b0f569b5c96c169c050e5df6 to your computer and use it in GitHub Desktop.
Save jdrew1303/1b3c4538b0f569b5c96c169c050e5df6 to your computer and use it in GitHub Desktop.
Reconnect python logging calls with the ROS logging system
class ConnectPythonLoggingToROS(logging.Handler):
MAP = {
logging.DEBUG:rospy.logdebug,
logging.INFO:rospy.loginfo,
logging.WARNING:rospy.logwarn,
logging.ERROR:rospy.logerr,
logging.CRITICAL:rospy.logfatal
}
def emit(self, record):
try:
self.MAP[record.levelno]("%s: %s" % (record.name, record.msg))
except KeyError:
rospy.logerr("unknown log level %s LOG: %s: %s" % (record.levelno, record.name, record.msg))
#reconnect logging calls which are children of this to the ros log system
logging.getLogger('trigger').addHandler(ConnectPythonLoggingToROS())
#logs sent to children of trigger with a level >= this will be redirected to ROS
logging.getLogger('trigger').setLevel(logging.DEBUG)
rospy.init_node('triggerbox_host', log_level=rospy.DEBUG)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment