简体   繁体   English

logging.basicConfig有什么作用?

[英]what does logging.basicConfig does?

I was reading loggers in python and got confused with logging.basicConfig() method. 我正在阅读python中的记录器,并与logging.basicConfig()方法混淆。 As mentioned in the python docs that it set many config for root logger, does that mean that it set the configs only for root logger or does it do it even for user created loggers also ? 如python docs所述,它为root logger设置了许多配置,这是否意味着它仅为root logger设置了配置,或者甚至为用户创建的logger设置了配置?

Another doubt is that whenever we create a user defined logger, does it become child logger of root logger ? 另一个疑问是,每当我们创建用户定义的记录器时,它是否会成为root记录器的子记录器?

To answer your second part: 要回答您的第二部分:

    # Pass no arguments to get the root logger
    root_logger = logging.getLogger()
    # This logger is a child of the root logger
    logger_a = logging.getLogger('foo')
    # Configure logger_a here e.g. change threshold level to logging.DEBUG
    # This is a child of logger_a
    logger_b = logging.getLogger('foo.bar')

Both logger_a and logger_b are user-defined, but only logger_a will inherit the default configuration of root_logger . 无论logger_alogger_b是用户定义的,但只有logger_a将继承的默认配置root_logger If, between lines 4 & 6 of the above, you were to configure logger_a so that it had different settings to root_logger , logger_b would default to logger_a 's settings rather than root_logger 's, since it is a direct descendant of logger_a and not root_logger . 如果在上述第4行和第6行之间配置logger_a ,使其与root_logger设置不同,则logger_b将默认为logger_a的设置,而不是root_logger的设置,因为它是logger_a的直接后代,而不是root_logger

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM