此python代码示例中的日志记录如何发生

时间:2018-09-07 13:26:58

标签: python python-3.x logging

我试图通过摆弄this basic example来理解python日志记录。我在该代码中添加了几行,并在下面进行了复制。查找#added by me注释以查找我添加的行:

spam_application.py

import logging
import auxiliary_module

# create logger with 'spam_application'
logger = logging.getLogger('spam_application')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('spam.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)
logger.addHandler(ch)

logger.error("Testing logger err")   #added by me
logger.debug("desting logger debug") #added by me

logger.info('creating an instance of auxiliary_module.Auxiliary')
a = auxiliary_module.Auxiliary()
logger.info('created an instance of auxiliary_module.Auxiliary')
logger.info('calling auxiliary_module.Auxiliary.do_something')
a.do_something()
logger.info('finished auxiliary_module.Auxiliary.do_something')
logger.info('calling auxiliary_module.some_function()')
auxiliary_module.some_function()
logger.info('done with auxiliary_module.some_function()')

auxiliary_module.py

import logging

# create logger
module_logger = logging.getLogger('spam_application.auxiliary')

class Auxiliary:
    def __init__(self):
        self.logger = logging.getLogger('spam_application.auxiliary.Auxiliary')
        self.logger.info('creating an instance of Auxiliary')

    def do_something(self):
        self.logger.info('doing something')
        a = 1 + 1
        self.logger.debug("a =%s", a)   #added by me
        self.logger.info('done doing something')
        self.logger.error("Testing logger err")     #added by me
        self.logger.debug("testing logger debug")   #added by me
        logging.debug("-->testing logger debug")    #added by me
        logging.error("-->testing logger err")      #added by me

def some_function():
    module_logger.info('received a call to "some_function"')

控制台输出

2018-09-07 18:43:57,041 - spam_application - ERROR - Testing logger err
2018-09-07 18:43:57,041 - spam_application.auxiliary.Auxiliary - ERROR - Testing logger err
ERROR:root:-->testing logger err
INFO:spam_application:finished auxiliary_module.Auxiliary.do_something
INFO:spam_application:calling auxiliary_module.some_function()
INFO:spam_application.auxiliary:received a call to "some_function"
INFO:spam_application:done with auxiliary_module.some_function()

spam.log

2018-09-07 18:43:57,041 - spam_application - ERROR - Testing logger err
2018-09-07 18:43:57,041 - spam_application - DEBUG - desting logger debug
2018-09-07 18:43:57,041 - spam_application - INFO - creating an instance of auxiliary_module.Auxiliary
2018-09-07 18:43:57,041 - spam_application.auxiliary.Auxiliary - INFO - creating an instance of Auxiliary
2018-09-07 18:43:57,041 - spam_application - INFO - created an instance of auxiliary_module.Auxiliary
2018-09-07 18:43:57,041 - spam_application - INFO - calling auxiliary_module.Auxiliary.do_something
2018-09-07 18:43:57,041 - spam_application.auxiliary.Auxiliary - INFO - doing something
2018-09-07 18:43:57,041 - spam_application.auxiliary.Auxiliary - DEBUG - a =2
2018-09-07 18:43:57,041 - spam_application.auxiliary.Auxiliary - INFO - done doing something
2018-09-07 18:43:57,041 - spam_application.auxiliary.Auxiliary - ERROR - Testing logger err
2018-09-07 18:43:57,041 - spam_application.auxiliary.Auxiliary - DEBUG - testing logger debug
2018-09-07 18:43:57,042 - spam_application - INFO - finished auxiliary_module.Auxiliary.do_something
2018-09-07 18:43:57,042 - spam_application - INFO - calling auxiliary_module.some_function()
2018-09-07 18:43:57,042 - spam_application.auxiliary - INFO - received a call to "some_function"
2018-09-07 18:43:57,042 - spam_application - INFO - done with auxiliary_module.some_function()

疑问

  1. 为什么在控制台中出现以下几行?:

    INFO:spam_application:finished auxiliary_module.Auxiliary.do_something
    INFO:spam_application:calling auxiliary_module.some_function()
    INFO:spam_application.auxiliary:received a call to "some_function"
    INFO:spam_application:done with auxiliary_module.some_function()
    
  2. 为什么控制台中的最后五行不遵循其他格式?

  3. 为什么没有

    DEBUG:root:-->testing logger debug
    
  4. 为什么这些行不在文件中:

    DEBUG:root:-->testing logger debug
    ERROR:root:-->testing logger err
    
  5. 当我在子模块(此处为logging.xyz())中调用logging.getLogger('abc').xyz()而不是auxiliary_module时,子模块记录的行为基本如何?

1 个答案:

答案 0 :(得分:0)

1、2、3、4。 问题的本质是您使用两个记录器。您使用代码logger = logging.getLogger('spam_application')创建的第一个。第二个是根记录器。您以代码logging.debug("-->testing logger debug")开始。通常,您需要多个记录器以区分记录消息的来源。跟踪记录器,不要混用。

要轻松登录python,您可以仅使用root logger。您可以使用logging.basicConfig(...)进行配置,并使用logging.info(...)创建日志消息。

5。 它的行为就很好。您无需执行任何其他操作即可使日志正常运行。尽管您可能会在跨多个线程和进程进行日志记录时遇到困难。