Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

python logging setting debug level

When I run the below piece of code, the logging is working fine. But when I comment #1) and #2) under setup_logger the log is not displayed.

What does #1) and #2) do here?

import logging
import sys
def get_logger(name):
    print('get_logger -- ', name)
    log = logging.getLogger("hello.{}".format(name))    
    return log

def setup_logger():
    print('setup_logger')
    root = logging.getLogger("")
    root.setLevel(logging.ERROR)

    handler = logging.StreamHandler(sys.stdout)
    handler.setFormatter(logging.Formatter(
        fmt="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    ))
    root.addHandler(handler)

    logger = logging.getLogger("hello")    #1
    logger.setLevel(logging.DEBUG)         #2

LOG = get_logger(__name__)
setup_logger()
print(LOG)

def main():
    LOG.debug('hello')

if __name__ == '__main__':
    main()
question from:https://stackoverflow.com/questions/66045859/python-logging-setting-debug-level

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The first line you marked gets a logger with given name. If there currently is no logger with the name you provided, a new one will be created (Also known as Singleton). You can read more about logging.getLogger in the Python documentation :

Return a logger with the specified name or, if name is None, return a logger which is the root logger of the hierarchy. If specified, the name is typically a dot-separated hierarchical name like ‘a’, ‘a.b’ or ‘a.b.c.d’. Choice of these names is entirely up to the developer who is using logging.

All calls to this function with a given name return the same logger instance. This means that logger instances never need to be passed between different parts of an application.

The second line you marked sets the loglevel on the logger instance. If you set the loglevel to DEBUG, all messages you log will be printed to the console / file. If you set it to INFO, all messages except for debug messages will be logged. And so on. Quote from the docs again:

Sets the threshold for this logger to level. Logging messages which are less severe than level will be ignored; logging messages which have severity level or higher will be emitted by whichever handler or handlers service this logger, unless a handler’s level has been set to a higher severity level than level.

When a logger is created, the level is set to NOTSET (which causes all messages to be processed when the logger is the root logger, or delegation to the parent when the logger is a non-root logger). Note that the root logger is created with level WARNING.

If you still have questions, I can edit my post to answer them.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...