简体   繁体   中英

what does logging.basicConfig does?

I was reading loggers in python and got confused with logging.basicConfig() method. 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 ?

Another doubt is that whenever we create a user defined logger, does it become child logger of root logger ?

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 . 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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