Logging with ros fix

This commit is contained in:
Artem30801
2019-04-18 21:04:15 +03:00
parent 1ae3438c15
commit e2f0e40c1b
2 changed files with 26 additions and 1 deletions

View File

@@ -16,13 +16,15 @@ parent_dir = os.path.dirname(current_dir)
sys.path.insert(0, parent_dir)
import messaging_lib as messaging
import ros_logging
logging.basicConfig( # TODO all prints as logs
level=logging.DEBUG, # INFO
format="%(asctime)s [%(name)-7.7s] [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s",
handlers=[
logging.FileHandler("client_logs.log"),
logging.StreamHandler()
logging.StreamHandler(),
ros_logging.RosHandler(),
])
ConfigOption = collections.namedtuple("ConfigOption", ["section", "option", "value"])

23
Drone/ros_logging.py Normal file
View File

@@ -0,0 +1,23 @@
import logging
import rospy
class RosHandler(logging.Handler):
level_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.level_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))
def route_logger_to_ros(logger_name):
logging.getLogger(logger_name).addHandler(RosHandler())