python日志模块_使用Python打印日志-日志记录模块

python日志模块

In this tutorial, we will learn what is logging? (basics of logging), how we can print logs in python and the different log levels that should be used while printing logs.

在本教程中,我们将学习什么是日志记录? (日志记录的基础知识),我们如何在python中打印日志以及在打印日志时应使用的不同日志级别

First of all, let us discuss what is logging.

首先,让我们讨论什么是日志记录。

什么是日志记录? (What is Logging?)

Logging is basically a way to track events(what is happening) whenever any program/script runs. Logging calls are added by software developers in their software to print useful steps during software code execution along with some information which can be later used to track down the code execution.

日志基本上是一种在任何程序/脚本运行时跟踪事件(正在发生的事情)的方法 。 记录调用由软件开发人员在其软件中添加,以在软件代码执行期间打印有用的步骤以及一些信息,这些信息以后可用于跟踪代码执行。

  • Proper logging can help you develop a better understanding of the flow of the program and it is very helpful in discovering those scenarios which you may have not thought at about during development.

    正确的日志记录可以帮助您更好地理解程序的流程,并且对于发现开发过程中可能没有想到的方案非常有帮助

  • Logging is very in debugging issues.

    日志记录非常涉及调试问题。

  • With the help of Logs, developers get an extra set of eyes to look at the flow of the application. Logs can store information, like which user ID or IP which is accessing the application.

    在Logs的帮助下,开发人员可以有更多的眼光来查看应用程序的流程。 日志可以存储信息,例如正在访问应用程序的用户ID或IP

  • By logging useful data and metrics in the software application, you can not only debug errors easily but you can also use the data to analyze the performance of the application to plan for scaling.

    通过在软件应用程序中记录有用的数据指标 ,您不仅可以轻松调试错误,还可以使用数据来分析应用程序的性能以计划扩展。

  • An event is described as a descriptive message which can optionally contain variable data (i.e. data is mainly different for each occurrence of the event).

    事件被描述为描述性消息 ,可以可选地包含可变数据 (即,每次发生事件时数据主要不同)。

  • Events or Logs printed, also have an importance also known as Log level, which we will cover in the later section of this tutorial.

    事件或日志的打印也很重要,也称为Log level ,我们将在本教程的后面部分中介绍。

Many beginners use the print() method in their python code to print statements and variables, to debug their code.

许多初学者在其python代码中使用print()方法来打印语句和变量,以调试其代码。

Using logs to do this is the right approach.

使用日志执行此操作是正确的方法。

Python记录模块 (Python Logging Module)

The Logging module is an inbuilt module in Python which is powerful and ready to use. This module allows writing logs either to a file or console or to any other output stream.

日志模块是Python中的内置模块功能强大且可以立即使用 。 该模块允许将日志写入 文件或控制台或任何其他输出流。

This module is used by many third-party Python libraries. So if your python project uses many third party libraries, then you can use the logging module, so that log handling can be same in your project and the third party libraries you are using.

许多第三方Python库都使用此模块。 因此,如果您的python项目使用许多第三方库,则可以使用日志记录模块,以便日志处理在您的项目和所使用的第三方库中可以相同。

To use this module you just need to write the following:

要使用此模块,您只需要编写以下内容:

import logging

After importing the logging module, you can use various methods provided by the logging module to print the logs.

导入日志记录模块后,可以使用日志记录模块提供的各种方法来打印日志。

不同级别的日志消息 (Different Levels of Log Message)

In the Logging Module, there are 5 standard levels by default that mainly indicate the severity of events. Also, the logging functions are named after the level.

在“日志记录模块”中,默认情况下有5个标准级别,主要指示事件严重性 。 此外,日志记录功能以级别命名。

The levels, in order of increasing severity, are given below:

这些级别按严重性从高到低的顺序如下:

Let us discuss the meaning of each level one by one in the table given below:

让我们在下表中逐一讨论每个级别的含义:

Level Time to use
DEBUG This level is mainly used to provide detailed information, typically of interest only when debugging problems.
INFO This level is used to confirm that things are working as expected. Just the usual information.
WARNING This level tells that something unexpected happened but not too severe that it may affect the normal functioning of the program/software.
ERROR This should be used to log more serious problems like errors or exceptions leading to functionality getting broken.
CRITICAL This level indicates a super serious error like the application not getting started or the database being unavailable to setup connection, etc.
水平 使用时间
调试 该级别主要用于提供详细信息 ,通常仅在调试问题时才有意义。
信息 此级别用于确认事情正在按预期进行。 只是通常的信息。
警告 此级别表明发生某些意外事件,但不太严重,可能会影响程序/软件的正常运行。
错误 这应该用于记录更严重的问题,例如错误或导致功能崩溃的异常。
危急 此级别表示一个非常严重的错误,例如应用程序无法启动数据库无法建立连接等。

You can all the types of log levels in your python code to log different information.

您可以在python代码中使用所有类型的日志级别来记录不同的信息。

But what logs will be printed, depends on the Logging configuration.

但是将打印哪些日志,取决于“日志记录”配置。

It is important to note that the default level is WARNING, which means that only events of this level and above this level will be tracked, which are, WARNING, ERROR and CRITICAL.

重要的是要注意, 默认级别WARNING ,这意味着仅跟踪此级别及该级别以上的事件,即WARNINGERRORCRITICAL

Python记录方法 (Python Logging Methods)

There are some convenient functions for simple logging usage like debug(), info(), warning(), error() , and critical(). Let us discuss them, one by one:

对于简单的日志记录用法,有一些方便的功能,例如debug()info()warning()error()critical() 。 让我们一一讨论:

How to Log? Task to perform
print() method If you want to display normal messages on console for the user information.
warning.warn() and logging.warn()

To log a warning regarding a particular runtime event.

The warning.warn() method is used in python code if the issue is avoidable and the changes must be made to eliminate the warning.

The logging.warning() method is used in the case there is some small issue/error which doesn't have any direct impact on the functioning of the code, but must be tracked as it may cause problem later. For example, module version mismatch, etc.

Raise an Error/Exception In order to report an error regarding a particular runtime event.
logging.error() or logging.critical()

If you want to report the an error without raising an exception. If you have done proper exception handling in python, but you must still log the exception which is handled so that it can later be found and fixed.

logging.critical() can be used for critical errors like program startup failed or database connection failed, etc.

logging.info() or logging.debug()

The logging.info() method can be used in order to report events that occur during normal operation of a program (e.g. for status monitoring or fault investigation).

logging.debug() is specifically used for very detailed output for diagnostic purposes.

如何登录? 要执行的任务
print()方法 如果要在控制台上显示普通消息以获取用户信息。
warning.warn()logging.warn()

记录有关特定运行时事件的警告。

如果可以避免问题,则在python代码中使用warning.warn()方法,必须进行更改以消除该警告。

logging.warning()方法用于出现一些小问题/错误的情况,该问题不会对代码的功能产生任何直接影响 ,但必须对其进行跟踪,因为以后可能会引起问题。 例如,模块版本不匹配等。

引发错误/异常 为了报告有关特定运行时事件的错误。
logging.error()logging.critical()

如果要报告错误而不引发异常。 如果您已经在python中完成了适当的异常处理 ,但是您仍然必须记录已处理的异常,以便以后可以找到并修复它。

logging.critical()可用于严重错误,例如程序启动失败或数据库连接失败等。

logging.info()logging.debug()

可以使用logging.info()方法来报告程序正常运行期间发生的事件(例如,用于状态监视或故障调查)。

logging.debug()特别用于非常详细的输出,以用于诊断目的。

Python记录基本示例 (Python Logging Basic Example)

Let us take a basic example where we will print log messages corresponding to different log levels:

让我们以一个基本示例为例,在该示例中,我们将打印与不同日志级别相对应的日志消息:

import logging

logging.debug('It is a debug message')	# it will not be printed
logging.info('It is an info message')	# not printed
logging.warning('OOPs!!! It is a warning')	# it will be print because it is default level
logging.error('Oops !! an error message')	# will be printed 
logging.critical('Oh!!!! it is a critical message')	# will be printed

WARNING:root: OOPs!!! It is a warning ERROR:root: Oops !! an error message CRITICAL:root: Oh!!!! it is a critical message

警告:root:糟糕!!! 这是一个警告错误:root:糟糕! 错误消息严重:root:哦!!!! 这是一个关键信息

The above output shows the severity level just before each message along with root , which is the name that the logging module gives to its default logger.

上面的输出显示了每条消息之前的严重性级别以及root ,这是日志记录模块赋予其默认记录器的名称

The above output format shows the level, name, and message and all are separated by a colon (:) and it is the default format for logs(we can change the format).

上述输出格式示出了电平,姓名和消息 ,并且所有由冒号分开( : ),它是用于日志的默认格式 (我们可以改变格式)。

It is important to note that debug() and info() messages didn't get logged. It is because, by default, the logging module logs the messages with a severity level of WARNING or above as we have already mentioned.

重要的是要注意, debug()info()消息没有被记录 。 这是因为默认情况下,正如我们已经提到的,日志记录模块默认记录严重级别WARNING或更高的消息。

You can also change that by just configuring the logging module to log events of all levels if you want to do so.

您也可以通过配置日志记录模块以记录所有级别的事件(如果需要)来更改此设置。

翻译自: https://www.studytonight.com/python/python-logging

python日志模块

你可能感兴趣的:(python日志模块_使用Python打印日志-日志记录模块)