The logging in Python

Source: Internet
Author: User

python logging modules can be confusing places find some interesting phenomena by seeing the code of the Python logging module:

1. The logging object is actually a tree structure, and each created logging object is the child node of the root logging object. When constructing logging objects using the GetLogger (name=none) function of the logging module, if name is None, this results in a root logging object. If name contains., such as name = ' A.B.C ', in this way will produce 3 logging objects, respectively, C, B, a,c->b->a->root,root tree root node, a root child node, B is a child node, c is a child node, and so on.

2. The root node is global, although multiple modules are involved in the process, but they share a root node.

3. Each logging object hit log, will also pass the log information to the upper logging object, for C->b->a->root this case, this log will actually play 4 times, with C, B, a, root sequentially hit a log.

One might ask, like I used to initialize a logging object in the same way as a module or a B module, so that the initialized object would be a child of the root logging object, and the root logging object would normally hit the log on the screen. Under normal circumstances, the log will be two copies, one will hit the file, one will hit the screen. So why is the fact that only the log file has the corresponding log, but there is no display of objects on the screen?

In fact, if the process is a little curious, the way directly accustomed to some doubts, and with such curiosity to explore, I believe there will be more gains.

So, the more confusing is, why I call a module generated by the slogger.info out of the log, only the log file has, and root logging why not hit log to hit the screen. Why root logging does not work. This time, you can look at the logging __init__.py code, you will find that the root logging info code is as follows:

def info (msg, *args, **kwargs):

"""

Log a message with severity ' INFO ' on the root logger.

"""

If Len (root.handlers) = = 0:

Basicconfig ()

Root.info (msg, *args, **kwargs)

The above code relates to Root.handlers, which is suspected to be related to the way the root.handlers is played with log. Therefore, print Len (root.handlers) finds the result to be 0. That is, the default root logging corresponding to the handlers is [], so that the result is Slogger log, root logging will not play any log.



The logging module in Python

Log exception information

When you use the logging module to record exception information, you do not need to pass in the exception object, as long as you call logger.error ()   or  logger.exception () to record the current exception.

# Log exception information try:1/0except: # is equivalent to the error level, but will additionally record the currently thrown exception stack information logger.exception (' This was an exception message ') # 2016- 10-08 21:59:19,493 Error:this is a exception message# Traceback (most recent call last): # File "D:/git/py_labs/demo /use_logging.py ", line, in <module># 1/0# Zerodivisionerror:integer division or modulo by zero


The logging in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.