1 . 所在模块,一般在openstack/common/log.py,其实最主要的还是调用了python中的logging模块;

入口函数在
def setup(product_name, version='unknown'):
    """Setup logging."""
    if CONF.log_config_append:
        _load_log_config(CONF.log_config_append)
    else:
        _setup_logging_from_conf(product_name, version)
    sys.excepthook = _create_logging_excepthook(product_name)

如果配置文件中设置了log_config_append,log_config_append 就是logging的配置,形式如下面这样的:

在函数_load_log_config中实现:
def _load_log_config(log_config_append):
    try:
        logging.config.fileConfig(log_config_append,
                                  disable_existing_loggers=False)
    except moves.configparser.Error as exc:
        raise LogConfigError(log_config_append, str(exc))

LogConfigError是一个自定义的Exception,也在log.py中;

如果没有设置log_config_append,则通过_setup_logging_from_conf函数来进行log的设置;
508     log_root = getLogger(None).logger
509     for handler in log_root.handlers:
510         log_root.removeHandler(handler)
511
512     if CONF.use_syslog:
513         facility = _find_facility_from_conf()
514         # TODO(bogdando) use the format provided by RFCSysLogHandler
515         #   after existing syslog format deprecation in J
516         if CONF.use_syslog_rfc_format:
517             syslog = RFCSysLogHandler(address='/dev/log',
518                                       facility=facility)
519         else:
520             syslog = logging.handlers.SysLogHandler(address='/dev/log',
521                                                     facility=facility)
522         log_root.addHandler(syslog)
523
524     logpath = _get_log_file_path()
525     if logpath:
526         filelog = logging.handlers.WatchedFileHandler(logpath)
527         log_root.addHandler(filelog)
528
529     if CONF.use_stderr:
530         streamlog = ColorHandler()
531         log_root.addHandler(streamlog)
532
533     elif not logpath:
534         # pass sys.stdout as a positional argument
535         # python2.6 calls the argument strm, in 2.7 it's stream
536         streamlog = logging.StreamHandler(sys.stdout)
537         log_root.addHandler(streamlog)
538
539     if CONF.publish_errors:
540         handler = importutils.import_object(
541             "openstack.common.log_handler.PublishErrorsHandler",
542             logging.ERROR)
543         log_root.addHandler(handler)
544
545     datefmt = CONF.log_date_format
546     for handler in log_root.handlers:
547         # NOTE(alaski): CONF.log_format overrides everything currently.  This
548         # should be deprecated in favor of context aware formatting.
549         if CONF.log_format:
550             handler.setFormatter(logging.Formatter(fmt=CONF.log_format,
551                                                    datefmt=datefmt))
552             log_root.info('Deprecated: log_format is now deprecated and will '
553                           'be removed in the next release')
554         else:
555             handler.setFormatter(ContextFormatter(project=project,
556                                                   version=version,
557                                                   datefmt=datefmt))
558
559     if CONF.debug:
560         log_root.setLevel(logging.DEBUG)
561     elif CONF.verbose:
562         log_root.setLevel(logging.INFO)
563     else:
564         log_root.setLevel(logging.WARNING)
565
566     for pair in CONF.default_log_levels:
567         mod, _sep, level_name = pair.partition('=')
568         level = logging.getLevelName(level_name)
569         logger = logging.getLogger(mod)
570         logger.setLevel(level)

508~510 行;首先取得root logger,在logging中,有个默认的rootlogger,任何其他的logger都是这个rootlogger的继承,然后将handler清空,logger可以设置handler,handler是负责将传给logger的信息显示出来的,如显示到stdout,输出到文件等等;

512~522 行,是否写系统的syslog,一般linux 是/dev/log, mac是/var/run/syslog

524到527行,_get_log_file_path函数取得log path,实际上就是判断conf中是否有设置conf.log_file和conf.log_dir; 
watchedfilehandler是将log信息输出到文件,watched的具体含义是当发现输出到文件有变化(A file is deemed to have changed if its device or inode have changed),当发现文件有变化的时候,
会将之前的文件流关闭,文件会再一次用一个新的文件流打开;这个在使用log rotation机制的时候是有用的。

529到531行,如果设置了conf.stderr,,通过ColorHandler来进行

533到537行,如果没有设置logpath,  那么stream handler(标准输出)加入到root log里面;

539到543行,如果设置了publish_errors,则调用publisherrorhandler
22 class PublishErrorsHandler(logging.Handler):
23     def emit(self, record):
24         if ('openstack.common.notifier.log_notifier' in
25                 cfg.CONF.notification_driver):
26             return
27         notifier.api.notify(None, 'error.publisher',
28                             'error_notification',
29                             notifier.api.ERROR,
30                             dict(error=record.getMessage()))
publisherrorhandler重写了emit方法,将log信息用notifier发送出去;

545 到557行对log的格式进行设置的部分;

559 到 564 在DEBUG < INFO < WARNING 之间选择一个,作为rootlogger的log level,其他的log如果没有单独设置level,会默认继承rootLogger的level;DEBUG和VERBOSE可以通过配置文件进行配置;

566,570可以通过CONF./default_log_levels分别针对不同的模块进行不同的level级别的设置;

setup函数基本分析完毕,可以如下使用setup函数;
from higgs.agent.common import config
config.setup_logging(cfg.CONF)

但一般在开始的常用方法为
from openstack.common import log as logging
LOG = logging.getLogger(__name__)

这个时候其实时调用openstack/common/log.py中的ContextAdapter类;
ContextAdapter类继承了BaseLoggerAdapter类继承于logging.LoggerAdapter类;LoggerAdapter的作用主要是
可以允许你在打印日志时,对日志做一些统一的处理,加入一些上下文信息(contextual information),例如在一个网络应用程序里面,你很有可能想在log中包含一些客户端信息
(比如客户端的username,ipaddress等)。
LoggerAdapter的实现方式更像是一个代理或者适配器,实例化一个LoggerAdapter的时候,你需要传递一个Logger实例和一个dict-like object包含你的上下文信息,然后,LoggerAdapter也要info、debug、
error、critical等方法,并且他们的函数签名与Logger本身的签名是一样的,然后在LoggerAdapter所做的事情就类似于下面的片段;

def debug(self, msg, *args, **kwargs):
    """Delegate a debug call to the underlying logger,
    after adding contextual information from this adapter instance."""
    msg, kwargs =self.process(msg, kwargs)
    self.logger.debug(msg, *args, **kwargs)

首先调用process方法对参数进行一定的处理,然后再将处理后的参数传给底层的logger进行调用;
在openstack里面主要是为了在log中加入了一些project、version等信息;

还有一个LazyLogger,延迟加载的logger;

openstack 中 log模块分析的更多相关文章

  1. 【转】Openstack中oslo_config模块学习

    OpenStack的项目貌似越来越多了,在Grizzly版之前,每个项目都得实现一套处理配置文件的代码.在每个项目的源码中基本上都可以找到openstack/common/cfg.py,inipars ...

  2. Android架构分析之LOG模块

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android版本:2.3.7_r1 Linux内核版本:android-goldfish-2.6.29 Andro ...

  3. frp源码剖析-frp中的log模块

    前言&引入 一个好的log模块可以帮助我们排错,分析,统计 一般来说log中需要有时间.栈信息(比如说文件名行号等),这些东西一般某些底层log模块已经帮我们做好了.但在业务中还有很多我们需要 ...

  4. nodejs中相互引用(循环引用)的模块分析

    话不多少,直接上源码吧: modA.js: module.exports.test = 'A'; const modB = require('./05_modB'); console.log( 'mo ...

  5. Openstack中RabbitMQ RPC代码分析

    在Openstack中,RPC调用是通过RabbitMQ进行的. 任何一个RPC调用,都有Client/Server两部分,分别在rpcapi.py和manager.py中实现. 这里以nova-sc ...

  6. 使用Python中的log模块将loss输出到终端与保存到文件

    记得之前对深度学习中得loss输出,经常自己会将输出流重新定向到一个文件中, 比如 python main.py > & | tee log.txt 对于caffe这种c++框架而言,用 ...

  7. 探索 OpenStack 之(14):OpenStack 中 RabbitMQ 的使用

    本文是 OpenStack 中的 RabbitMQ 使用研究 两部分中的第一部分,将介绍 RabbitMQ 的基本概念,即 RabbitMQ 是什么.第二部分将介绍其在 OpenStack 中的使用. ...

  8. Multipath在OpenStack中的faulty device的成因及解决(part 1)

    | 版权:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.如有问题,可以邮件:wangxu198709@gmail.com 简介: Multip ...

  9. 【转】python模块分析之logging日志(四)

    [转]python模块分析之logging日志(四) python的logging模块是用来写日志的,是python的标准模块. 系列文章 python模块分析之random(一) python模块分 ...

随机推荐

  1. PHP + Memcache 实现多服务器session共享

    很多时候一个完整的系统可能运行在多个服务器上,如果这多个服务器之间需要共享session的话,那么php默认的files保存session的方式就无能为力了.这时我们可以考虑使用memcache 来接 ...

  2. PowerDesigner生成SQL脚本时,对象带有双引号的问题解决

    在pdm查看脚本时,发现表名和字段名带有双引号: 1.create table"cfg_user_card_account"  ( 2.  "user_card_acco ...

  3. IE9中Media queries在iframe无效的解决方法

    在css中有5个media querie @media screen and(min-width:0px)and(max-width:319px){ body {background-color:re ...

  4. thinkphp 调用系统的方法

    在需要调用的脚本 加载 load('filename');//filename为文件名

  5. Octopus系列之关于多选属性如何在OO中表示呢?

    在电子商务系统中 关于产品属性的问题 会设计如下几个表 产品信息        Product 选项信息表     Option        存储 Size  Color.... 选项值信息表  O ...

  6. LAMP整理

    LAMP第一部分 查看编译了哪些软件:是编译时自动生成的 Cat /usr/local/apache2/build/config.nice 网站根目录存放处: /usr/local/apache2/h ...

  7. php像新浪微博一样生成短域名

    <?php function shorturl($url='', $prefix='', $suffix='') { $base32 = array ( 'a', 'b', 'c', 'd', ...

  8. cf卡中,wtmp文件较大,导致磁盘空间满了

    看了一下,有一个wtmp 和wtmp.1的文件非常大.wtmp记录的是机器注销.启动的信息.由此可见,机器长时间的不断重启,造成该日志记录超级大,把cf的空间给占满了. wtmp日志可以用who和la ...

  9. 1800: [Ahoi2009]fly 飞行棋

    #include<cstdio> #include<iostream> using namespace std; ],n,ans; int main() { scanf(&qu ...

  10. 使用 JavaScript 修改浏览器 URL 地址栏

    现在的浏览器里,有一个十分有趣的功能,你可以在不刷新页面的情况下修改浏览器URL;在浏览过程中.你可以将浏览历史储存起来,当你在浏览器点击后退按钮的时候,你可以冲浏览历史上获得回退的信息,这听起来并不 ...