This article brings you a detailed introduction to logging in Python (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Introduction
There are many basic usages of logging on the Internet, so I won’t introduce them here. Before introducing the main text, let's first look at a requirement:
Suppose you need to encapsulate a certain function into a class library for others to use, how to deal with the logs in the class library?
A few years ago in a In projects developed in C#, I used this method: define a logging base class. All classes that need to use logging inherit this base class. In this base class, define a LogHandler event, which is used to implement specific logging actions. At the same time, the LogHandler delegate of class A can be linked to the LogHandler delegate of class B. On LogHandler, the log information of the two classes is added together.
Since looking at the implementation of logging in python, I found that my approach is really weak.
I introduced the log output in the peewee framework in my previous blog Python: The use of logging.NullHandler. Simply put, a Logger named peewee is defined in peewee and a NullHandler is added. The caller You only need to add a specific Handler to output the log, which is very convenient.
Suppose we also have a Logger in the main program. After calling peewee, I want to output the two logs to the same log file. Obviously it is not advisable to point the FileHandler of two logs to the same log file, as there is a risk of concurrent file preemption. Of course, we can also name the Logger in the main program as peewee, but this is not only too low, but also if another library is called, which also encapsulates a Logger, it will be difficult to handle.
Logger of tree stump structure
The Logger object is designed as a tree structure, and it has a parent attribute. A Logger named root is defined in logging as the root node of all Loggers, and the parent attribute of root is None. root is global.
When calling
logging.getLogger(name=None)
to get a Logger object, if name is None, the root node root is returned. If name contains ., such as name = 'a.b', then if there is already a Logger named a, then a.b is the child node of a. If there is no Logger named a, then a.b is the child node of root. node.
After the child logger completes processing the log message, it will pass the log message to its parent logger by default. Therefore, we do not have to define and configure handlers for all Loggers used in an application. It is enough to configure handlers for a top-level Logger and then create child loggers as needed. We can turn off this delivery mechanism by setting the Logger's propagate property to False.
What does it mean? Let’s look at the code:
import logging logA = logging.getLogger('a') logA.setLevel(logging.DEBUG) logA.addHandler(logging.StreamHandler()) logB = logging.getLogger('a.b') logB.addHandler(logging.StreamHandler())
Output result:
Logger A Logger B Logger B
The reason why Logger B is output twice is because logB is a child node of logA , and Handler is also defined in logB, so the Handler of logB is output once, and the Handler of logA is also output once, that is, twice. If you want to output only once, you can delete the Handler in logB. Of course, this can be useful, especially if you don't have a log management tool on hand. For example, you need to output all the logs in the main program to understand the overall running sequence of the program, but you want to output a separate log of a certain module so that you can clearly understand the error reporting or execution sequence in the module.
The previous peewee example is easy to solve. You only need to set the parent attribute of the peewee log to the log of the main program.
Conclusion
In fact, this is a relatively easy problem to explain, and there is no need to write so much. I don’t want to share with you how to use logging in python, but I want to share with you an idea of how logging is implemented, because I have encountered this problem and designed a solution, and then I was completely blown away.
The above is the detailed content of Detailed introduction to logging in Python (with examples). For more information, please follow other related articles on the PHP Chinese website!