几乎所有的论文源码都会用logging
来记录log,但是我总是不太理解它的用法,所以这里做一下记录。
事件分级
logging
将事件分级为:
Level | When it’s used |
---|---|
DEBUG | Detailed information, typically of interest only when diagnosing problems. |
INFO | Confirmation that things are working as expected. |
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. |
ERROR | Due to a more serious problem, the software has not been able to perform some function. |
CRITICAL | A 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()
文档信息
- 本文作者:weownthenight
- 本文链接:https://weownthenight.github.io/2022/06/21/logging%E7%9A%84%E7%94%A8%E6%B3%95/
- 版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)