• Python logging模块


    1 logging模块简介

    logging模块是Python内置的标准模块,主要用于输出运行日志,可以设置输出日志的等级、日志保存路径、日志文件回滚等;相比print,具备如下优点:

    1. 可以通过设置不同的日志等级,在release版本中只输出重要信息,而不必显示大量的调试信息;
    2. print将所有信息都输出到标准输出中,严重影响开发者从标准输出中查看其它数据;logging则可以由开发者决定将信息输出到什么地方,以及怎么输出;

    2 logging模块使用

    2.1 基本使用

    配置logging基本的设置,然后在控制台输出日志,

    1. import logging
    2. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    3. logger = logging.getLogger(__name__)
    4. logger.info("Start print log")
    5. logger.debug("Do something")
    6. logger.warning("Something maybe fail.")
    7. logger.info("Finish")

    运行时,控制台输出,

    1. 2016-10-09 19:11:19,434 - __main__ - INFO - Start print log
    2. 2016-10-09 19:11:19,434 - __main__ - WARNING - Something maybe fail.
    3. 2016-10-09 19:11:19,434 - __main__ - INFO - Finish

    logging中可以选择很多消息级别,如debug、info、warning、error以及critical。通过赋予logger或者handler不同的级别,开发者就可以只输出错误信息到特定的记录文件,或者在调试时只记录调试信息。

    例如,我们将logger的级别改为DEBUG,再观察一下输出结果,

    logging.basicConfig(level = logging.DEBUG,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    控制台输出,可以发现,输出了debug的信息。

    1. 2016-10-09 19:12:08,289 - __main__ - INFO - Start print log
    2. 2016-10-09 19:12:08,289 - __main__ - DEBUG - Do something
    3. 2016-10-09 19:12:08,289 - __main__ - WARNING - Something maybe fail.
    4. 2016-10-09 19:12:08,289 - __main__ - INFO - Finish

    logging.basicConfig函数各参数:

    filename:指定日志文件名;

    filemode:和file函数意义相同,指定日志文件的打开模式,'w'或者'a';

    format:指定输出的格式和内容,format可以输出很多有用的信息,

    1. 参数:作用
    2. %(levelno)s:打印日志级别的数值
    3. %(levelname)s:打印日志级别的名称
    4. %(pathname)s:打印当前执行程序的路径,其实就是sys.argv[0]
    5. %(filename)s:打印当前执行程序名
    6. %(funcName)s:打印日志的当前函数
    7. %(lineno)d:打印日志的当前行号
    8. %(asctime)s:打印日志的时间
    9. %(thread)d:打印线程ID
    10. %(threadName)s:打印线程名称
    11. %(process)d:打印进程ID
    12. %(message)s:打印日志信息

    datefmt:指定时间格式,同time.strftime();

    level:设置日志级别,默认为logging.WARNNING;

    stream:指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略;

    2.2 将日志写入到文件

    2.2.1 将日志写入到文件

    设置logging,创建一个FileHandler,并对输出消息的格式进行设置,将其添加到logger,然后将日志写入到指定的文件中,

    1. import logging
    2. logger = logging.getLogger(__name__)
    3. logger.setLevel(level = logging.INFO)
    4. handler = logging.FileHandler("log.txt")
    5. handler.setLevel(logging.INFO)
    6. formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    7. handler.setFormatter(formatter)
    8. logger.addHandler(handler)
    9. logger.info("Start print log")
    10. logger.debug("Do something")
    11. logger.warning("Something maybe fail.")
    12. logger.info("Finish")

    log.txt中日志数据为,

    1. 2016-10-09 19:01:13,263 - __main__ - INFO - Start print log
    2. 2016-10-09 19:01:13,263 - __main__ - WARNING - Something maybe fail.
    3. 2016-10-09 19:01:13,263 - __main__ - INFO - Finish

    2.2.2 将日志同时输出到屏幕和日志文件

    logger中添加StreamHandler,可以将日志输出到屏幕上,

    1. import logging
    2. logger = logging.getLogger(__name__)
    3. logger.setLevel(level = logging.INFO)
    4. handler = logging.FileHandler("log.txt")
    5. handler.setLevel(logging.INFO)
    6. formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    7. handler.setFormatter(formatter)
    8. console = logging.StreamHandler()
    9. console.setLevel(logging.INFO)
    10. logger.addHandler(handler)
    11. logger.addHandler(console)
    12. logger.info("Start print log")
    13. logger.debug("Do something")
    14. logger.warning("Something maybe fail.")
    15. logger.info("Finish")

    可以在log.txt文件和控制台中看到,

    1. 2016-10-09 19:20:46,553 - __main__ - INFO - Start print log
    2. 2016-10-09 19:20:46,553 - __main__ - WARNING - Something maybe fail.
    3. 2016-10-09 19:20:46,553 - __main__ - INFO - Finish

    可以发现,logging有一个日志处理的主对象,其他处理方式都是通过addHandler添加进去,logging中包含的handler主要有如下几种,

    1. handler名称:位置;作用
    2. StreamHandler:logging.StreamHandler;日志输出到流,可以是sys.stderr,sys.stdout或者文件
    3. FileHandler:logging.FileHandler;日志输出到文件
    4. BaseRotatingHandler:logging.handlers.BaseRotatingHandler;基本的日志回滚方式
    5. RotatingHandler:logging.handlers.RotatingHandler;日志回滚方式,支持日志文件最大数量和日志文件回滚
    6. TimeRotatingHandler:logging.handlers.TimeRotatingHandler;日志回滚方式,在一定时间区域内回滚日志文件
    7. SocketHandler:logging.handlers.SocketHandler;远程输出日志到TCP/IP sockets
    8. DatagramHandler:logging.handlers.DatagramHandler;远程输出日志到UDP sockets
    9. SMTPHandler:logging.handlers.SMTPHandler;远程输出日志到邮件地址
    10. SysLogHandler:logging.handlers.SysLogHandler;日志输出到syslog
    11. NTEventLogHandler:logging.handlers.NTEventLogHandler;远程输出日志到Windows NT/2000/XP的事件日志
    12. MemoryHandler:logging.handlers.MemoryHandler;日志输出到内存中的指定buffer
    13. HTTPHandler:logging.handlers.HTTPHandler;通过"GET"或者"POST"远程输出到HTTP服务器

    2.2.3 日志回滚

    使用RotatingFileHandler,可以实现日志回滚,

    1. import logging
    2. from logging.handlers import RotatingFileHandler
    3. logger = logging.getLogger(__name__)
    4. logger.setLevel(level = logging.INFO)
    5. #定义一个RotatingFileHandler,最多备份3个日志文件,每个日志文件最大1K
    6. rHandler = RotatingFileHandler("log.txt",maxBytes = 1*1024,backupCount = 3)
    7. rHandler.setLevel(logging.INFO)
    8. formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    9. rHandler.setFormatter(formatter)
    10. console = logging.StreamHandler()
    11. console.setLevel(logging.INFO)
    12. console.setFormatter(formatter)
    13. logger.addHandler(rHandler)
    14. logger.addHandler(console)
    15. logger.info("Start print log")
    16. logger.debug("Do something")
    17. logger.warning("Something maybe fail.")
    18. logger.info("Finish")

    可以在工程目录中看到,备份的日志文件,

    1. 2016/10/09 19:36 732 log.txt
    2. 2016/10/09 19:36 967 log.txt.1
    3. 2016/10/09 19:36 985 log.txt.2
    4. 2016/10/09 19:36 976 log.txt.3

    2.3 设置消息的等级

    可以设置不同的日志等级,用于控制日志的输出,

    1. 日志等级:使用范围
    2. FATAL:致命错误
    3. CRITICAL:特别糟糕的事情,如内存耗尽、磁盘空间为空,一般很少使用
    4. ERROR:发生错误时,如IO操作失败或者连接问题
    5. WARNING:发生很重要的事件,但是并不是错误时,如用户登录密码错误
    6. INFO:处理请求或者状态变化等日常事务
    7. DEBUG:调试过程中使用DEBUG等级,如算法中每个循环的中间状态

    2.4 捕获traceback

    Python中的traceback模块被用于跟踪异常返回信息,可以在logging中记录下traceback,

    代码,

    1. import logging
    2. logger = logging.getLogger(__name__)
    3. logger.setLevel(level = logging.INFO)
    4. handler = logging.FileHandler("log.txt")
    5. handler.setLevel(logging.INFO)
    6. formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    7. handler.setFormatter(formatter)
    8. console = logging.StreamHandler()
    9. console.setLevel(logging.INFO)
    10. logger.addHandler(handler)
    11. logger.addHandler(console)
    12. logger.info("Start print log")
    13. logger.debug("Do something")
    14. logger.warning("Something maybe fail.")
    15. try:
    16. open("sklearn.txt","rb")
    17. except (SystemExit,KeyboardInterrupt):
    18. raise
    19. except Exception:
    20. logger.error("Faild to open sklearn.txt from logger.error",exc_info = True)
    21. logger.info("Finish")

    控制台和日志文件log.txt中输出,

    1. Start print log
    2. Something maybe fail.
    3. Faild to open sklearn.txt from logger.error
    4. Traceback (most recent call last):
    5. File "G:\zhb7627\Code\Eclipse WorkSpace\PythonTest\test.py", line 23, in <module>
    6. open("sklearn.txt","rb")
    7. IOError: [Errno 2] No such file or directory: 'sklearn.txt'
    8. Finish

    也可以使用logger.exception(msg,_args),它等价于logger.error(msg,exc_info = True,_args),

    logger.error("Faild to open sklearn.txt from logger.error",exc_info = True)

    替换为,

    logger.exception("Failed to open sklearn.txt from logger.exception")

    控制台和日志文件log.txt中输出,

    1. Start print log
    2. Something maybe fail.
    3. Failed to open sklearn.txt from logger.exception
    4. Traceback (most recent call last):
    5. File "G:\zhb7627\Code\Eclipse WorkSpace\PythonTest\test.py", line 23, in <module>
    6. open("sklearn.txt","rb")
    7. IOError: [Errno 2] No such file or directory: 'sklearn.txt'
    8. Finish

    2.5 多模块使用logging

    主模块mainModule.py,

    1. import logging
    2. import subModule
    3. logger = logging.getLogger("mainModule")
    4. logger.setLevel(level = logging.INFO)
    5. handler = logging.FileHandler("log.txt")
    6. handler.setLevel(logging.INFO)
    7. formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    8. handler.setFormatter(formatter)
    9. console = logging.StreamHandler()
    10. console.setLevel(logging.INFO)
    11. console.setFormatter(formatter)
    12. logger.addHandler(handler)
    13. logger.addHandler(console)
    14. logger.info("creating an instance of subModule.subModuleClass")
    15. a = subModule.SubModuleClass()
    16. logger.info("calling subModule.subModuleClass.doSomething")
    17. a.doSomething()
    18. logger.info("done with subModule.subModuleClass.doSomething")
    19. logger.info("calling subModule.some_function")
    20. subModule.som_function()
    21. logger.info("done with subModule.some_function")

    子模块subModule.py,

    1. import logging
    2. module_logger = logging.getLogger("mainModule.sub")
    3. class SubModuleClass(object):
    4. def __init__(self):
    5. self.logger = logging.getLogger("mainModule.sub.module")
    6. self.logger.info("creating an instance in SubModuleClass")
    7. def doSomething(self):
    8. self.logger.info("do something in SubModule")
    9. a = []
    10. a.append(1)
    11. self.logger.debug("list a = " + str(a))
    12. self.logger.info("finish something in SubModuleClass")
    13. def som_function():
    14. module_logger.info("call function some_function")

    执行之后,在控制和日志文件log.txt中输出,

    1. 2016-10-09 20:25:42,276 - mainModule - INFO - creating an instance of subModule.subModuleClass
    2. 2016-10-09 20:25:42,279 - mainModule.sub.module - INFO - creating an instance in SubModuleClass
    3. 2016-10-09 20:25:42,279 - mainModule - INFO - calling subModule.subModuleClass.doSomething
    4. 2016-10-09 20:25:42,279 - mainModule.sub.module - INFO - do something in SubModule
    5. 2016-10-09 20:25:42,279 - mainModule.sub.module - INFO - finish something in SubModuleClass
    6. 2016-10-09 20:25:42,279 - mainModule - INFO - done with subModule.subModuleClass.doSomething
    7. 2016-10-09 20:25:42,279 - mainModule - INFO - calling subModule.some_function
    8. 2016-10-09 20:25:42,279 - mainModule.sub - INFO - call function some_function
    9. 2016-10-09 20:25:42,279 - mainModule - INFO - done with subModule.some_function

    首先在主模块定义了logger'mainModule',并对它进行了配置,就可以在解释器进程里面的其他地方通过getLogger('mainModule')得到的对象都是一样的,不需要重新配置,可以直接使用。定义的该logger的子logger,都可以共享父logger的定义和配置,所谓的父子logger是通过命名来识别,任意以'mainModule'开头的logger都是它的子logger,例如'mainModule.sub'。

    实际开发一个application,首先可以通过logging配置文件编写好这个application所对应的配置,可以生成一个根logger,如'PythonAPP',然后在主函数中通过fileConfig加载logging配置,接着在application的其他地方、不同的模块中,可以使用根logger的子logger,如'PythonAPP.Core','PythonAPP.Web'来进行log,而不需要反复的定义和配置各个模块的logger。

    3 通过JSON或者YAML文件配置logging模块

    尽管可以在Python代码中配置logging,但是这样并不够灵活,最好的方法是使用一个配置文件来配置。在Python 2.7及以后的版本中,可以从字典中加载logging配置,也就意味着可以通过JSON或者YAML文件加载日志的配置。

    3.1 通过JSON文件配置

    JSON配置文件,

    1. {
    2. "version":1,
    3. "disable_existing_loggers":false,
    4. "formatters":{
    5. "simple":{
    6. "format":"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    7. }
    8. },
    9. "handlers":{
    10. "console":{
    11. "class":"logging.StreamHandler",
    12. "level":"DEBUG",
    13. "formatter":"simple",
    14. "stream":"ext://sys.stdout"
    15. },
    16. "info_file_handler":{
    17. "class":"logging.handlers.RotatingFileHandler",
    18. "level":"INFO",
    19. "formatter":"simple",
    20. "filename":"info.log",
    21. "maxBytes":"10485760",
    22. "backupCount":20,
    23. "encoding":"utf8"
    24. },
    25. "error_file_handler":{
    26. "class":"logging.handlers.RotatingFileHandler",
    27. "level":"ERROR",
    28. "formatter":"simple",
    29. "filename":"errors.log",
    30. "maxBytes":10485760,
    31. "backupCount":20,
    32. "encoding":"utf8"
    33. }
    34. },
    35. "loggers":{
    36. "my_module":{
    37. "level":"ERROR",
    38. "handlers":["info_file_handler"],
    39. "propagate":"no"
    40. }
    41. },
    42. "root":{
    43. "level":"INFO",
    44. "handlers":["console","info_file_handler","error_file_handler"]
    45. }
    46. }

    通过JSON加载配置文件,然后通过logging.dictConfig配置logging,

    1. import json
    2. import logging.config
    3. import os
    4. def setup_logging(default_path = "logging.json",default_level = logging.INFO,env_key = "LOG_CFG"):
    5. path = default_path
    6. value = os.getenv(env_key,None)
    7. if value:
    8. path = value
    9. if os.path.exists(path):
    10. with open(path,"r") as f:
    11. config = json.load(f)
    12. logging.config.dictConfig(config)
    13. else:
    14. logging.basicConfig(level = default_level)
    15. def func():
    16. logging.info("start func")
    17. logging.info("exec func")
    18. logging.info("end func")
    19. if __name__ == "__main__":
    20. setup_logging(default_path = "logging.json")
    21. func()

    3.2 通过YAML文件配置

    通过YAML文件进行配置,比JSON看起来更加简介明了,

    1. version: 1
    2. disable_existing_loggers: False
    3. formatters:
    4. simple:
    5. format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    6. handlers:
    7. console:
    8. class: logging.StreamHandler
    9. level: DEBUG
    10. formatter: simple
    11. stream: ext://sys.stdout
    12. info_file_handler:
    13. class: logging.handlers.RotatingFileHandler
    14. level: INFO
    15. formatter: simple
    16. filename: info.log
    17. maxBytes: 10485760
    18. backupCount: 20
    19. encoding: utf8
    20. error_file_handler:
    21. class: logging.handlers.RotatingFileHandler
    22. level: ERROR
    23. formatter: simple
    24. filename: errors.log
    25. maxBytes: 10485760
    26. backupCount: 20
    27. encoding: utf8
    28. loggers:
    29. my_module:
    30. level: ERROR
    31. handlers: [info_file_handler]
    32. propagate: no
    33. root:
    34. level: INFO
    35. handlers: [console,info_file_handler,error_file_handler]

    通过YAML加载配置文件,然后通过logging.dictConfig配置logging,

    1. import yaml
    2. import logging.config
    3. import os
    4. def setup_logging(default_path = "logging.yaml",default_level = logging.INFO,env_key = "LOG_CFG"):
    5. path = default_path
    6. value = os.getenv(env_key,None)
    7. if value:
    8. path = value
    9. if os.path.exists(path):
    10. with open(path,"r") as f:
    11. config = yaml.load(f)
    12. logging.config.dictConfig(config)
    13. else:
    14. logging.basicConfig(level = default_level)
    15. def func():
    16. logging.info("start func")
    17. logging.info("exec func")
    18. logging.info("end func")
    19. if __name__ == "__main__":
    20. setup_logging(default_path = "logging.yaml")
    21. func()

  • 相关阅读:
    【vue3】:Excel导入导出解决方案
    linux如何运行c程序命令
    MATLAB矩阵
    【缓存分类以及常见的缓存淘汰策略】
    【C++】list的使用(上)
    从一个webpack loader中学习
    Business Objects单一报表升级为全套商业智能BI产品,探索更多平台与行业
    机器学习之逻辑回归以及梯度下降法求解
    python写一个文本处理器
    【历史上的今天】9 月 17 日:世界上的第一张火车票;GamerDNA 创始人出生;中国开设第一个网上多媒体讲座
  • 原文地址:https://blog.csdn.net/m0_72557783/article/details/128162492