logging模块

什么时候使用logging

Task you want to perform

The best tool for the task

Display console output for ordinary usage of a command line script or program

print()

Report events that occur during normal operation of a program (e.g. for status monitoring or fault investigation)

logging.info() (or logging.debug() for very detailed output for diagnostic purposes)

Issue a warning regarding a particular runtime event

warnings.warn()arrow-up-right in library code if the issue is avoidable and the client application should be modified to eliminate the warninglogging.warning()arrow-up-right if there is nothing the client application can do about the situation, but the event should still be noted

Report an error regarding a particular runtime event

Raise an exception

Report suppression of an error without raising an exception (e.g. error handler in a long-running server process)

logging.error()arrow-up-right, logging.exception()arrow-up-right or logging.critical()arrow-up-right as appropriate for the specific error and application domain

级别

Level

When it’s used

Numeric value

DEBUG

Detailed information, typically of interest only when diagnosing problems.

10

INFO

Confirmation that things are working as expected.

20

WARNING

An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.

30

ERROR

Due to a more serious problem, the software has not been able to perform some function.

40

CRITICAL

A serious error, indicating that the program itself may be unable to continue running.

50

NOTSET

0

基本用法

代码

输出

注意

  • DEBUGINFO级别日志没有在控制台显示, 是因为默认级别是WARNING

保存到文件

代码

输出

example.log内的内容

最后更新于