• openstack日志模块


    一、简单的python日志模块介绍

    http://www.cnblogs.com/tuzkee/p/3974193.html

    http://blog.csdn.net/jgood/article/details/4340740

    二、日志的配置

      openstack服务程序在启动的时候都会对日志系统进行设置,这里以nova-api为例,在它的的启动代码里:

    nova/cmd/api.py

    def main():
        config.parse_args(sys.argv)
        logging.setup("nova")
        ...

      由于在/etc/init.d/openstack-nova-api控制脚本里已经传入参数,所以sys.argv为['/usr/bin/nova-api', '--logfile', '/var/log/nova/api.log'],即在配置参数里面设置了日志系统的文件记录地址。接着看日志的设置代码如下:

    nova/openstack/common/log.py

    def setup(product_name):
        if CONF.log_config:
            _load_log_config(CONF.log_config)
        else:
            _setup_logging_from_conf()
        sys.excepthook = _create_logging_excepthook(product_name)
    
    
    def _setup_logging_from_conf():
        """
        获取根logger,此后创建的所有logger都继承了它的配置,所以此后创建的logger基本不需要配置就可以直接使用
        """
        log_root = getLogger(None).logger    
        for handler in log_root.handlers:
                log_root.removeHandler(handler)
    
        if CONF.use_syslog:
            facility = _find_facility_from_conf()
            syslog = logging.handlers.SysLogHandler(address='/dev/log',
                                                    facility=facility)
            log_root.addHandler(syslog)
        """设置日志输出位置"""
        logpath = _get_log_file_path()#获取刚刚设置的日志文件路径
        if logpath:
            filelog = logging.handlers.WatchedFileHandler(logpath)
            log_root.addHandler(filelog)
    
        if CONF.use_stderr:
            streamlog = ColorHandler()
            log_root.addHandler(streamlog)
    
        elif not CONF.log_file:
            streamlog = logging.StreamHandler(sys.stdout)
            log_root.addHandler(streamlog)
    
        if CONF.publish_errors:
            handler = importutils.import_object(
                "nova.openstack.common.log_handler.PublishErrorsHandler",
                logging.ERROR)
            log_root.addHandler(handler)
        """设置日志格式"""
        datefmt = CONF.log_date_format
        for handler in log_root.handlers:
            if CONF.log_format:
                handler.setFormatter(logging.Formatter(fmt=CONF.log_format,
                                                       datefmt=datefmt))
                log_root.info('Deprecated: log_format is now deprecated and will '
                              'be removed in the next release')
            else:
                handler.setFormatter(ContextFormatter(datefmt=datefmt))
        """设置日志级别"""
        if CONF.debug:
            log_root.setLevel(logging.DEBUG)
        elif CONF.verbose:
            log_root.setLevel(logging.INFO)
        else:
            log_root.setLevel(logging.WARNING)
    
        for pair in CONF.default_log_levels:
            mod, _sep, level_name = pair.partition('=')
            level = logging.getLevelName(level_name)
            logger = logging.getLogger(mod)
            logger.setLevel(level)

    三、日志的使用

      首先导入并获取logger

    from nova.openstack.common import log as logging
    LOG = logging.getLogger(__name__)

      其中的LOG是经过openstack定制的logger,可见如下代码:

    def getLogger(name='unknown', version='unknown'):
        if name not in _loggers:
            _loggers[name] = ContextAdapter(logging.getLogger(name),
                                            name,
                                            version)
        return _loggers[name]
    
    class ContextAdapter(BaseLoggerAdapter):
        warn = logging.LoggerAdapter.warning
    
        def __init__(self, logger, project_name, version_string):
            self.logger = logger
            self.project = project_name
            self.version = version_string

      接着就可以使用logger了,例如:

    LOG.error(xxx)
  • 相关阅读:
    怎么把pdf转换成word文件
    怎么把pdf文件转换成word文件教程
    pdf转换成word转换器哪个好用
    pdf格式怎么转换成word格式
    pdf文件怎么转换成word教程
    怎么把pdf文件转换成word文件
    福大软工1816 · 第二次作业
    福大软工1816 · 第一次作业
    ER图
    数据定义分析、数据操纵分析、数据完整性分析、数据安全性分析、并发处理分析、数据库性能分析
  • 原文地址:https://www.cnblogs.com/littlebugfish/p/4063760.html
Copyright © 2020-2023  润新知