Python内置库-logging
Posted 九幽帝君
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python内置库-logging相关的知识,希望对你有一定的参考价值。
-
调试
-
辅助定位问题
-
数据分析
二、日志级别
何时使用 | |
---|---|
debug | 细节信息,仅当诊断问题时适用 |
info | 确认程序按预期运行 |
warning | 表明已经或即将发生意外(例如:磁盘空间不足)。程序仍然按预期进行 |
error | 由于严重的问题,程序的某些功能已经不能正常执行。 |
critical | 严重的错误,表明程序已不能继续执行 |
1、官方文档说明:http://docs.python.org/zh-cn/3/howto/logging.html
说明 | |
---|---|
logging.debug(msg,*args,**kwargs) | 创建一条严重级别为debug的日志记录 |
logging.info(msg,*args,**kwargs) | 创建一条严重级别为info的日志记录 |
logging.warning(msg,*args,**kwargs) | 创建一条严重级别为warning的日志记录 |
logging.error(msg,*args,**kwargs) | 创建一条严重级别为error的日志记录 |
logging.critical(msg,*args,**kwargs) | 创建一条严重级别为critical的日志记录 |
logging.log(level,*args,**kwargs) | 创建一条严重级别为level的日志记录 |
logging.basicConfig(**kwargs) |
import logging # logging 默认设置级别是warning级别 logging.warning(\'Watch out!\') # will print a message to the console logging.error(\'hhhh\') # 打印warning以下级别的 logging.info(\'I told you so\') # will not print anything
logging.basicConfig(level=logging.INFO)
"""设置日志的级别""" # logging 默认设置级别是warning级别,设置日志的级别INFO logging.basicConfig(level=logging.INFO) logging.warning(\'Watch out!\') # will print a message to the console logging.error(\'hhhh\') # 打印warning以下级别的 logging.info(\'I told you so\') # will not print anything
logging.basicConfig(filename=\'example.log\', encoding=\'utf-8\', level=logging.DEBUG)
"""保存日志到文件""" logging.basicConfig(filename=\'app.log\', encoding=\'utf-8\', level=logging.DEBUG) logging.debug(\'This message should go to the log file\') logging.info(\'我是谁\') logging.warning(\'And this, too\') logging.error(\'And non-ASCII stuff, too, like Øresund and Malmö\')
logging.basicConfig(filename=\'example.log\', encoding=\'utf-8\', level=logging.DEBUG, format=\'%(asctime)s [%(levelname)s] %(message)s (%(filename)s:% (lineno)s)\',datefmt=\'%m/%d/%Y %I:%M:%S %p\') logging.debug(\'This message should go to the log file\') logging.info(\'我是谁\') logging.warning(\'And this, too\') logging.error(\'And non-ASCII stuff, too, like Øresund and Malmö\')
logging.basicConfig(format=\'%(asctime)s %(message)s\')
logging.basicConfig(format=\'%(asctime)s %(message)s\') logging.warning(\'is when this event was logged.\')
5、显示日期/时间(自定义)
logging.basicConfig(format=\'%(asctime)s %(message)s\', datefmt=\'%m/%d/%Y %I:%M:%S %p\') logging.warning(\'is when this event was logged.\')
说明 | |
---|---|
loggers | 提供应用程序代码直接使用的接口 |
handlers | 用于日志记录发送到指定的目的位置 |
filters | 提供更细粒度的日志过滤功能,用于决定哪些日志记录将会被输出(其他的日志记录会被忽略) |
formatters |
3、python日志的定义
import logging # create logger 创建一个logger 记录器 logger = logging.getLogger(\'simple_example\') logger.setLevel(logging.DEBUG) # 设置级别 # create console handler and set level to debug 创建一个处理器 ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) # 设置级别 # create formatter formatter = logging.Formatter(\'%(asctime)s - %(name)s - %(levelname)s - %(message)s\') # add formatter to ch 定义格式器 ch.setFormatter(formatter) # add ch to logger logger.addHandler(ch) # \'application\' code logger.debug(\'debug message\') logger.info(\'info message\') logger.warning(\'warn message\') logger.error(\'error message\') logger.critical(\'critical message\')
- 保存至文件
import logging # create logger 创建一个logger 记录器 logger = logging.getLogger(\'simple_example\') logger.setLevel(logging.DEBUG) # 设置级别 # create console handler and set level to debug 创建一个处理器 # ch = logging.StreamHandler() # 流处理器 ch = logging.FileHandler(\'MyLog.log\', encoding=\'utf-8\') # 文件处理器 ch.setLevel(logging.DEBUG) # 设置级别 # create formatter formatter = logging.Formatter(\'%(asctime)s - %(name)s - %(levelname)s - %(message)s\') # add formatter to ch 定义格式器 ch.setFormatter(formatter) # add ch to logger logger.addHandler(ch) # \'application\' code logger.debug(\'debug message\') logger.info(\'info message\') logger.warning(\'warn message\') logger.error(\'error message\') logger.critical(\'critical message\')
- 保存至文件以及在终端上显示
import logging # create logger 创建一个logger 记录器 logger = logging.getLogger(\'simple_example\') logger.setLevel(logging.DEBUG) # 设置级别 # create console handler and set level to debug 创建一个处理器 ch = logging.StreamHandler() # 流处理器 ch.setLevel(logging.DEBUG) # 设置级别 # create formatter formatter = logging.Formatter(\'%(asctime)s - %(name)s - %(levelname)s - %(message)s\') # add formatter to ch 定义格式器 ch.setFormatter(formatter) # add ch to logger logger.addHandler(ch) # 定义一个文件处理器 # 文件处理器 ch_file = logging.FileHandler(\'appLog.log\', encoding=\'utf-8\') # 文件处理器 ch_file.setLevel(logging.DEBUG) # 设置级别 # create formatter_file formatter_file = logging.Formatter(\'%(asctime)s - %(name)s - %(levelname)s - %(message)s\') # add formatter_file to ch_file 定义格式器 ch_file.setFormatter(formatter_file) # add ch_file to logger logger.addHandler(ch_file) # \'application\' code logger.debug(\'debug message\') logger.info(\'info message\') logger.warning(\'warn message\') logger.error(\'error message\') logger.critical(\'critical message\')
- 封装
import logging import os def get_logger(): # create logger # os.path.basename(__file__) 当前文件名 print(os.path.basename(__file__)) logger = logging.getLogger(os.path.basename(__file__)) logger.setLevel(logging.DEBUG) # create console handler and set level to debug ch = logging.FileHandler(filename=\'mylog.log\', encoding="utf-8") ch.setLevel(logging.DEBUG) # create formatter formatter = logging.Formatter(\'%(asctime)s - %(name)s - %(levelname)s - %(message)s\') # add formatter to ch ch.setFormatter(formatter) # add ch to logger logger.addHandler(ch) return logger logger = get_logger() def log_info(message): logger.info(message) log_info("log info 方法") logger.debug(\'debug message\') logger.info(\'info message\') logger.warning(\'warn message\') logger.error(\'error message\') logger.critical(\'critical message\')
- 配置文件logging.conf 文件(win需要把中午注释删了)
[loggers] # loggers 对象列表 keys=root,main [handlers] # handlers 对象列表 keys=consoleHandlers,fileHandlers [formatters] # formatters 列表 keys=fmt [logger_root] level=DEBUG handlers=consoleHandlers,fileHandlers [logger_main] # main logger level = DEBUG handlers = fileHandlers qualname=main propagate=0 [handler_consoleHandlers]# consoleHandlers 指定控制器的输出方向、级别、输出格式、参数 class = StreamHandler level = DEBUG formatter = fmt args = (sys.stdout,) [handler_fileHandlers]# 循环日志文件 以文件大小来 分割# 每隔 1000 Byte 划分一个日志文件,备份文件为 3 个 class = logging.handlers.RotatingFileHandler level = DEBUG formatter = fmt args = (\'./logs/test.log\', \'a\', 10000, 3, \'UTF-8\') [formatter_fmt] # fmt 格式 format=%(asctime)s [%(levelname)s] %(message)s (%(filename)s:%(lineno)s) datefmt=
本文来自博客园,作者:jiuyou-emperor,转载请注明原文链接:https://www.cnblogs.com/jiuyou-emperor/
以上是关于Python内置库-logging的主要内容,如果未能解决你的问题,请参考以下文章