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!

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool