Python is more elegant logging program

--- --- restore content begins

Read this article takes about 5 minutes.

In Python, under normal circumstances we may directly own logging module for logging, including before my time as well. In use we need to configure Handler, Formatter performs some processing, such as the log output to a different location or a different set of output formats, or block and set the log backup. But in fact, personal feeling logging with them it's not that easy, in fact, mainly configuration is more complicated.

Common use

First look logging common solution, and I usually configure the output to a file, console and Elasticsearch. Output to the console it is merely convenient and direct view; convenient and direct output to a file is stored, to retain a history of all backup; output to Elasticsearch, directly as a central storage and analysis, using Kibana can easily view and analyze Elasticsearch operation.

So here I would basically do the logging package written as follows:

import logging

Defined over how to use it? Only you need to use a defined way to get logger, and then log the content corresponding to:

logger = get_logger()

Results are as follows:

DEBUG - 2019-10-11 22:27:35,923 - process: 99490 - logger.py - __main__ - 81 - logger - this is a message 

We look at the basic realization of the definition of it. Here are some constants used to first define some basic properties logging module, such as  LOG_ENABLED representatives of the log function is turned on, LOG_TO_ES if on behalf of the logs to elasticsearch, and there are many other logs basic configuration as  LOG_FORMAT the configuration log entry for each output the basic format, in addition to the necessary information about the connection. These variables and command-line runtime environment variables or docking, can easily achieve the replacement of some of the switches and configuration.

Then it defines such a method get_logger receiving one argument name. After the first method to get the name, will go to global variables inside look loggers, loggers variable is a global dictionary, if you have already declared the logger, can be returned directly to obtain, and then do their secondary initialization. If the name is not found inside loggers corresponding logger, it can be created. After you create a logger, you can add variety to its corresponding Handler, such as the output to the console to use StreamHandler, output to a file or to use FileHandler RotatingFileHandler, output to Elasticsearch to use CMRESHandler, respectively, corresponding information can be configured.

Finally, it will save the new logger to global loggers inside and return can be, so if you have the same name logger can look directly loggers return directly.

Here dependence of the additional output to Elasticsearch package called CMRESHandler, it can support output log to Elasticsearch inside, if you want to use, then you can install it:

pip install CMRESHandler

Well, I used above is, before logging configuration, by a configuration as described above, I can achieve logging output to three positions, and the corresponding effect can be achieved. For example, after the output to Elasticsearch, I can very easily use Kibana to view the current operating conditions, the proportion ERROR Log, etc., as shown:

Further statistical analysis can be done on the basis of it.

loguru

The above implementation is already a more feasible to configure the program. However, I still feel some trouble Handler with them, especially in a new project often too lazy to write some configuration. Even without the above configuration, the basic configuration with the logging lines, like the following general configuration:

import logging

I do not bother to write, feeling is not an elegant implementation.

There is a demand force ah, this is not someone realizes such a library, called loguru, you can log in to configure and use more simple and convenient.

Let us look at it in the end is how to use it.

installation

首先,这个库的安装方式很简单,就用基本的 pip 安装即可,Python 3 版本的安装如下:

pip3 install loguru

安装完毕之后,我们就可以在项目里使用这个 loguru 库了。

基本使用

那么这个库怎么来用呢?我们先用一个实例感受下:

from loguru import logger

看到了吧,不需要配置什么东西,直接引入一个 logger,然后调用其 debug 方法即可。

在 loguru 里面有且仅有一个主要对象,那就是 logger,loguru 里面有且仅有一个 logger,而且它已经被提前配置了一些基础信息,比如比较友好的格式化、文本颜色信息等等。

上面的代码运行结果如下:

2019-10-13 22:46:12.367 | DEBUG | __main__:<module>:4 - this is a debug message 

可以看到其默认的输出格式是上面的内容,有时间、级别、模块名、行号以及日志信息,不需要手动创建 logger,直接使用即可,另外其输出还是彩色的,看起来会更加友好。

以上的日志信息是直接输出到控制台的,并没有输出到其他的地方,如果想要输出到其他的位置,比如存为文件,我们只需要使用一行代码声明即可。

例如将结果同时输出到一个 runtime.log 文件里面,可以这么写:

from loguru import logger

很简单吧,我们也不需要再声明一个 FileHandler 了,就一行 add 语句搞定,运行之后会发现目录下 runtime.log 里面同样出现了刚刚控制台输出的 DEBUG 信息。

上面就是一些基本的使用,但这还远远不够,下面我们来详细了解下它的一些功能模块。

详细使用

既然是日志,那么最常见的就是输出到文件了。loguru 对输出到文件的配置有非常强大的支持,比如支持输出到多个文件,分级别分别输出,过大创建新文件,过久自动删除等等。

下面我们分别看看这些怎样来实现,这里基本上就是 add 方法的使用介绍。因为这个 add 方法就相当于给 logger 添加了一个 Handler,它给我们暴露了许多参数来实现 Handler 的配置,下面我们来详细介绍下。

首先看看它的方法定义吧:

def add(

看看它的源代码,它支持这么多的参数,如 level、format、filter、color 等等。

sink

另外我们还注意到它有个非常重要的参数 sink,官方文档可以了解到通过 sink 我们可以传入多种不同的数据结构,汇总如下:

•sink 可以传入一个 file 对象,例如 sys.stderr 或者 open('file.log', 'w') 都可以。•sink 可以直接传入一个 str 字符串或者 pathlib.Path 对象,其实就是代表文件路径的,如果识别到是这种类型,它会自动创建对应路径的日志文件并将日志输出进去。•sink 可以是一个方法,可以自行定义输出实现。•sink 可以是一个 logging 模块的 Handler,比如 FileHandler、StreamHandler 等等,或者上文中我们提到的 CMRESHandler 照样也是可以的,这样就可以实现自定义 Handler 的配置。•sink 还可以是一个自定义的类,具体的实现规范可以参见官方文档。

所以说,刚才我们所演示的输出到文件,仅仅给它传了一个 str 字符串路径,他就给我们创建了一个日志文件,就是这个原理。

format、filter、level

下面我们再了解下它的其他参数,例如 format、filter、level 等等。

其实它们的概念和格式和 logging 模块都是基本一样的了,例如这里使用 format、filter、level 来规定输出的格式:

logger.add('runtime.log', format="{time} {level} {message}", filter="my_module", level="INFO")

删除 sink

另外添加 sink 之后我们也可以对其进行删除,相当于重新刷新并写入新的内容。

删除的时候根据刚刚 add 方法返回的 id 进行删除即可,看下面的例子:

from loguru import logger

看这里,我们首先 add 了一个 sink,然后获取它的返回值,赋值为 trace。随后输出了一条日志,然后将 trace 变量传给 remove 方法,再次输出一条日志,看看结果是怎样的。

控制台输出如下:

2019-10-13 23:18:26.469 | DEBUG | __main__:<module>:4 - this is a debug message 

日志文件 runtime.log 内容如下:

2019-10-13 23:18:26.469 | DEBUG | __main__:<module>:4 - this is a debug message 

可以发现,在调用 remove 方法之后,确实将历史 log 删除了。

这样我们就可以实现日志的刷新重新写入操作。

rotation 配置

用了 loguru 我们还可以非常方便地使用 rotation 配置,比如我们想一天输出一个日志文件,或者文件太大了自动分隔日志文件,我们可以直接使用 add 方法的 rotation 参数进行配置。

我们看看下面的例子:

logger.add('runtime_{time}.log', rotation="500 MB")

通过这样的配置我们就可以实现每 500MB 存储一个文件,每个 log 文件过大就会新创建一个 log 文件。我们在配置 log 名字时加上了一个 time 占位符,这样在生成时可以自动将时间替换进去,生成一个文件名包含时间的 log 文件。

另外我们也可以使用 rotation 参数实现定时创建 log 文件,例如:

logger.add('runtime_{time}.log', rotation='00:00')

这样就可以实现每天 0 点新创建一个 log 文件输出了。

另外我们也可以配置 log 文件的循环时间,比如每隔一周创建一个 log 文件,写法如下:

logger.add('runtime_{time}.log', rotation='1 week')

这样我们就可以实现一周创建一个 log 文件了。

retention 配置

很多情况下,一些非常久远的 log 对我们来说并没有什么用处了,它白白占据了一些存储空间,不清除掉就会非常浪费。retention 这个参数可以配置日志的最长保留时间。

比如我们想要设置日志文件最长保留 10 天,可以这么来配置:

logger.add('runtime.log', retention='10 days')

这样 log 文件里面就会保留最新 10 天的 log,妈妈再也不用担心 log 沉积的问题啦。

compression 配置

loguru 还可以配置文件的压缩格式,比如使用 zip 文件格式保存,示例如下:

logger.add('runtime.log', compression='zip')

这样可以更加节省存储空间。

字符串格式化

loguru 在输出 log 的时候还提供了非常友好的字符串格式化功能,像这样:

logger.info('If you are using Python {}, prefer {feature} of course!', 3.6, feature='f-strings')

这样在添加参数就非常方便了。

Traceback 记录

在很多情况下,如果遇到运行错误,而我们在打印输出 log 的时候万一不小心没有配置好 Traceback 的输出,很有可能我们就没法追踪错误所在了。

但用了 loguru 之后,我们用它提供的装饰器就可以直接进行 Traceback 的记录,类似这样的配置即可:

@logger.catch

We do a test, when we call the three parameters are passed 0, directly caused the error of division by 0, and see what the situation will be:

my_function(0, 0, 0)

After the operation is completed, the log can be found inside appeared Traceback information, and give us the time of the output variable value, really can not be praised! The results are as follows:

> File "run.py", line 15, in <module> 

Therefore, with loguru can very easily implement tracking log, debug efficiency may be higher on a ten-fold?

In addition loguru and many powerful features, there will no longer start one by one to explain, the more content we can see loguru official documentation to learn more about.

After reading it, it is time to replace their own logging module into loguru it!

We can add this group 631 441 315 exchange, the group has a large number of PDF books, tutorials, free of charge! No matter what stage of learning to small partners can get to the appropriate tutorial!

 

--- end --- restore content

Guess you like

Origin www.cnblogs.com/qingdeng123/p/11716596.html