Go to the documentation of this file.00001
00002
00003 import os
00004 import json
00005 import logging
00006
00007
00008 __use_ros_logging = True
00009
00010
00011 class Logger(object):
00012 """ logger class
00013 """
00014 info = None
00015 warning = None
00016 degbug = None
00017 critical = None
00018 exception = None
00019
00020
00021 def setup_logging(
00022 default_dict={},
00023 default_path='logger_config.json',
00024 default_level=logging.INFO,
00025 use_ros_logging=False
00026 ):
00027 """ setup logging configuration
00028 default_dict / default_path の両者が設定されている場合、両方のデータを読み込む
00029 データの優先度としては、default_pathに記述されたものが優先度が高い
00030
00031 :param default_dict: default logging config dict
00032 :param default_path: default logging config path
00033 :param default_level: default log level
00034 """
00035 global __use_ros_logging
00036 __use_ros_logging = use_ros_logging
00037 setup_dict = {}
00038 setup_dict.update(default_dict)
00039
00040 if os.path.exists(default_path):
00041 with open(default_path, 'rb') as config_file:
00042 setup_dict = json.load(config_file)
00043
00044 if len(setup_dict.keys()):
00045 logging.config.dictConfig(setup_dict)
00046 else:
00047 FORMAT = '[%(levelname)s] %(message)s @%(filename)s:%(funcName)s:%(lineno)d'
00048 logging.basicConfig(format=FORMAT, level=default_level)
00049
00050
00051 def get_logger(name=''):
00052 """ get logger
00053
00054 :param name: logger name
00055 :returns: logger
00056 """
00057 global __use_ros_logging
00058 logger = logging.getLogger(name)
00059 try:
00060 if __use_ros_logging:
00061 import rospy
00062 logger = Logger()
00063 logger.info = rospy.loginfo
00064 logger.debug = rospy.logdebug
00065 logger.warn = rospy.logwarn
00066 logger.err = rospy.logerr
00067 logger.critical = rospy.logfatal
00068 logger.exception = rospy.logfatal
00069 except:
00070 logger.info('rospy package is not available')
00071
00072 return logger