ROS使用rospy.loginfo()出现“WARNING: cannot load logging configuration file, logging is disabled”的解决方案

问题简介

好友运行ROS,想使用rospy.loginfo来打印日志,但是却出现了以上报错。我和他除了版本不同没什么不同,觉得很奇怪,我的环境是Ubuntu 18.04 melodic的ROS版本没有遇到这个问题,他的是Ubuntu 16.04 Kinetic就遇到了这个问题“WARNING: cannot load logging configuration file, logging is disabled”,于是我就来查为什么会出现这个问题,看到ROS日志相关官方提示,提示如下:

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.

解决方案

看了上述的官方资料,知道缺少相关配置文件,于是按照python logging模块相关配置文件,在/etc/ros下按照如下步骤写下即可解决

Step1: /etc/ros 下创建该文件

cd /etc/ros
sudo vim python_logging.conf

Step2 在文件内写入如下代码保存退出即可,参数可根据需要自己调整:

[loggers]
keys=root,fileLogger,rotatingFileLogger
 
[handlers]
keys=consoleHandler,fileHandler,rotatingFileHandler
 
[formatters]
keys=simpleFormatter
 
[logger_root]
level=DEBUG
handlers=consoleHandler
 
[logger_fileLogger]
level=DEBUG
# 该logger中配置的handler
handlers=fileHandler
# logger 的名称
qualname=fileLogger
propagate=0
 
[logger_rotatingFileLogger]
level=DEBUG
# 这样配置,rotatingFileLogger中就同时配置了consoleHandler,rotatingFileHandler
# consoleHandler 负责将日志输出到控制台
# rotatingFileHandler 负责将日志输出保存到文件中
handlers=consoleHandler,rotatingFileHandler
qualname=rotatingFileLogger
propagate=0
 
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)
 
[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=simpleFormatter
args=('logs/logging.log', 'a')
 
[handler_rotatingFileHandler]
class=handlers.RotatingFileHandler
level=WARNING
formatter=simpleFormatter
args=("logs/rotating_logging.log", "a", 1*1024*1024, 5)
 
[formatter_simpleFormatter]
#format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
format=%(asctime)s - %(module)s - %(thread)d - %(levelname)s : %(message)s
datefmt=%Y-%m-%d %H:%M:%S

测试代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import rospy
import numpy
import sys
from std_msgs.msg import String,Float64
def main():
    topic = 'chatter'
    pub = rospy.Publisher(topic, String,queue_size=1)
    rospy.init_node('talker', anonymous=True)
    rospy.loginfo("I will publish to the topic %s", topic)
    while not rospy.is_shutdown():
        str = "hello world %s"%rospy.get_time()
        rospy.loginfo(str)
        pub.publish(str)
        rospy.sleep(0.1)
if __name__ == '__main__':
        main()

测试代码输出:

2019-08-05 18:15:04 - test_log - 139813274478336 - INFO : hello world 1565000104.7
2019-08-05 18:15:04 - test_log - 139813274478336 - INFO : hello world 1565000104.8
2019-08-05 18:15:04 - test_log - 139813274478336 - INFO : hello world 1565000104.9
2019-08-05 18:15:05 - test_log - 139813274478336 - INFO : hello world 1565000105.0
2019-08-05 18:15:05 - test_log - 139813274478336 - INFO : hello world 1565000105.11
2019-08-05 18:15:05 - test_log - 139813274478336 - INFO : hello world 1565000105.21
2019-08-05 18:15:05 - test_log - 139813274478336 - INFO : hello world 1565000105.31
2019-08-05 18:15:05 - test_log - 139813274478336 - INFO : hello world 1565000105.41

[1] http://wiki.ros.org/rospy/Overview/Logging#overriding_node_log_file
[2] https://docs.python.org/3/library/logging.config.html#configuration-file-format
[3] https://blog.csdn.net/cxx654/article/details/83216337

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值