在整个过程中自动使用一个类实例

时间:2015-06-01 02:37:09

标签: python class python-2.7 logging

一旦导入并实例化了python日志记录模块,整个过程就会在所有模块和线程中进行。他们是如何实现这种效果的?

示例:

myapp.py

1. Received: by oiax69 with SMTP id x69so5491756oia.2 for <myaddr1@mydomain1.biz>; Sun, 31 May 2015 15:39:45 -0700 (PDT)
2. Received: by oiav63 with SMTP id v63so5487624oia.3 for <myaddr2@mydomain2.com>; Sun, 31 May 2015 15:39:46 -0700 (PDT)    
3. Received: by oiav63 with SMTP id v63so5493687oia.0 for <myaddr3@mydomain3.com>; Sun, 31 May 2015 15:39:45 -0700 (PDT)

mylib.py

import logging
import mylib

def main():
    logging.basicConfig(filename='myapp.log', level=logging.INFO)
    logging.info('Started')
    mylib.do_something()
    logging.info('Finished')

if __name__ == '__main__':
    main()

myapp.log

import logging

def do_something():
    logging.info('Doing something')

1 个答案:

答案 0 :(得分:1)

首次导入模块时,已加载的模块对象将放入{{1}}。稍后导入将找到模块对象,而不是重新加载模块。

日志记录模块有一堆模块属性,用于在第一次导入后保存日志记录配置状态。