Python中logging模块用于做什么,怎么用
Admin 2022-07-25 群英技术资讯 1176 次浏览
这篇文章给大家分享的是“Python中logging模块用于做什么,怎么用”,对大家学习和理解有一定的参考价值和帮助,有这方面学习需要的朋友,接下来就跟随小编一起学习一下吧。logging模块是Python内置的标准模块,主要用于输出脚本运行日志,可以设置输出日志的等级、日志保存路径等。
级别排序:CRITICAL > ERROR > WARNING > INFO > DEBUG
import logging # 引入logging模块
# 将信息打印到控制台上
logging.debug("debug")
logging.info("info")
logging.warning("warning")
logging.error("error")
logging.critical("critical")
[root@zijie ~]# python log.py
WARNING:root:warning
ERROR:root:error
CRITICAL:root:critical
默认生成的root logger的level是logging.WARNING,低于该级别不输出,如果要展示低于WARNING级别的内容,可以引入logging.basicConfig指定日志级别logging.basicConfig(level=logging.DEBUG)
| 格式 | 描述 |
| filename | 指定使用指定的文件名而不是 StreamHandler 创建 FileHandler。 |
| filemode | 如果指定 filename,则以此模式打开文件(‘r'、‘w'、‘a')。默认为“a”。 |
| format | 为处理程序使用指定的格式字符串。 |
| datefmt | 使用 time.strftime() 所接受的指定日期/时间格式。 |
| style | 如果指定了格式,则对格式字符串使用此样式。'%' 用于 printf 样式、'{' 用于 str.format()、'$' 用于 string。默认为“%”。 |
| level | 将根记录器级别设置为指定的级别。默认生成的 root logger 的 level 是 logging.WARNING,低于该级别的就不输出了。级别排序:CRITICAL > ERROR > WARNING > INFO > DEBUG。(如果需要显示所有级别的内容,可将 level=logging.NOTSET) |
| stream | 使用指定的流初始化 StreamHandler。注意,此参数与 filename 不兼容――如果两者都存在,则会抛出 ValueError。 |
| handlers | 如果指定,这应该是已经创建的处理程序的迭代,以便添加到根日志程序中。任何没有格式化程序集的处理程序都将被分配给在此函数中创建的默认格式化程序。注意,此参数与 filename 或 stream 不兼容――如果两者都存在,则会抛出 ValueError。 |
import logging
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(filename)s %(levelname)s %(message)s',
datefmt='%a %d %b %Y %H:%M:%S',
filename='xuehui.log',
filemode='w')
logging.info('This is a info.')
logging.debug('This is a debug message.')
logging.warning('This is a warning.')
import logging
import os.path
import time
#创建logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# 创建handler,用于写入日志文件
logdate = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
log_path = 'logs/'
log_name = log_path + logdate + '.log'
logfile = log_name
fh = logging.FileHandler(logfile, mode='w')
fh.setLevel(logging.DEBUG)
# 定义输出格式
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
fh.setFormatter(formatter)
# 将logger添加到handler
logger.addHandler(fh)
# 日志
logger.debug('this is a logger debug message')
logger.info('this is a logger info message')
logger.warning('this is a logger warning message')
logger.error('this is a logger error message')
logger.critical('this is a logger critical message')
import logging
import os.path
import time
#创建logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# 创建handler,用于写入日志文件
logdate = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
log_path = 'logs/'
log_name = log_path + logdate + '.log'
logfile = log_name
fh = logging.FileHandler(logfile, mode='w')
fh.setLevel(logging.DEBUG)
# 定义输出格式
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
fh.setFormatter(formatter)
# 将logger添加到handler
logger.addHandler(fh)
# 日志
try:
open('/data/exist', 'rb')
except BaseException as e:
logger.error('Failed to open file', exc_info=True)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
Python内置函数,chr() 用一个整数作参数,返回一个对应的字符。
这篇文章主要为大家详细介绍了Python中集合的创建、使用和遍历,集合常见的操作函数,集合与列表,元组,字典的嵌套,感兴趣的小伙伴可以了解一下
这篇文章主要介绍了解决python spyder 突然打不开的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
这里给大家分享的是实现二终端网络可靠度的方法以及使用Python实现的代码,有需要的小伙伴可以参考下。
大家好,本篇文章主要讲的是Python自动爬取图片并保存实例代码,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008