logging的用法

2022/06/21 Python 共 2126 字,约 7 分钟

几乎所有的论文源码都会用logging来记录log,但是我总是不太理解它的用法,所以这里做一下记录。

参考:logging advanced tutorials

Logging in Python

Python之日志处理(logging模块)

事件分级

logging将事件分级为:

LevelWhen it’s used
DEBUGDetailed information, typically of interest only when diagnosing problems.
INFOConfirmation that things are working as expected.
WARNINGAn 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.
ERRORDue to a more serious problem, the software has not been able to perform some function.
CRITICALA serious error, indicating that the program itself may be unable to continue running.

默认的level是WARNING,也就是说只有事件严重程度大于等于WARNING才会记录。这个级别我们可以在logging.basicConfig()中通过level关键字自己调整。

logger

我们不止需要记录某一个文件的调试信息,通常我们想要让logging的信息遵循包的结构,让我们知道错误信息究竟是出自具体哪个文件:

For example, a logger named ‘scan’ is the parent of loggers ‘scan.text’, ‘scan.html’ and ‘scan.pdf’. Logger names can be anything you want, and indicate the area of an application in which a logged message originates.

最普遍的做法就是:

logger = logging.getLogger(__name__)

我看的开源代码是:

logger = logging.getLogger(__file__)

其中:

  • __name__:
    • 当当前文件被直接执行:__name__==__main__
    • 当当前文件被导入:__name__将会是module的名字
  • __file__:
    • 当文件路径在sys.path中时, __file__是相对路径
    • 当文件路径不在sys.path中时,__file__是绝对路径

Basic Configuration

我们用basicConfig()配置最基本的logging选项,需要注意的是这个方法只能被调用1次。

比较常用的配置包括:

  • level: The root logger will be set to the specified security level.
  • filename: This specifies the file.
  • filemode: if filename is given, the file is opened in this mode. The default is a, which means append.
  • format: This is the format of the log message.

举例来说:

import logging

logging.basicConfig(filename='app.log', filemode='w', format='%(name)s - %(levelname)s - %(message)s')
logging.warning('This will get logged to a file')

更多的basicConfig()的用法可以查看:logging.basicConfig

一个例子

这里以我碰到的一个论文源码举例,其中logging相关代码如下:

# train.py
import logging

logger = logging.getLogger(__file__)

def train():
    # logging is set to INFO (resp. WARN) for main (resp. auxiliary) process. logger.info => log main process only, logger.warning => log all processes
    logging.basicConfig(level=logging.INFO if args.local_rank in [-1, 0] else logging.WARN)
    logger.warning("Running process %d", args.local_rank)  # This is a logger.warning: it will be printed by all distributed processes
    logger.info("Arguments: %s", pformat(args))
    
if __name__ == "__main__":
    train()

文档信息

Search

    Table of Contents