6

I am having issues with the Python logger and the rospy logger. In the begining, the Python logger stopped logging as soon as a ROS node was initialized. I tried to solve the problem by adding a stream handler to the logger. Now it only logs if a ROS node is initialized.

I read about rospy and python logging conflicts in the ROS git but there seems to be no nice solution to use the python logger exclusively and independent from the ROS logger for my code.

some module initializing the logger:

    path = "/some_directory/"
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    logger = logging.getLogger('logger_name')

    # logging file handler for basic log infos
    file_handler_info = logging.FileHandler('{}log.log'.format(path))
    file_handler_info.setFormatter(formatter)
    file_handler_info.setLevel(logging.INFO)
    logger.addHandler(file_handler_info)

    # logging file handler for critical warnings
    file_handler_critical = logging.FileHandler('{}critical.log'.format(path))
    file_handler_critical.setFormatter(formatter)
    file_handler_critical.setLevel(logging.WARNING)
    logger.addHandler(file_handler_critical)

    # logging stream handler for console output
    stream_handler = logging.StreamHandler()
    stream_handler.setFormatter(formatter)
    stream_handler.setLevel(logging.DEBUG)
    logger.addHandler(stream_handler)

some other module starting the ROS node

logger.info("This message will not be displayed")
rospy.init_node('node_name', anonymous=True)
logger.info("This message will be displayed")
Sharky Bamboozle
  • 1,066
  • 16
  • 28

1 Answers1

1

rospy provides overriding the default 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.

Customize or create your own version of the python_logging.conf which is placed at /opt/ros/YOUR_ROS_DISTRO/etc/ros/ by default.

Maybe it is already enough to add the streamHandler to the root logger:

[logger_root]
level=DEBUG
handlers=fileHandler,streamHandler
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49