python怎么打印日志
Python中有一个模块logging,可以直接记录日志
python中有一个模块logging,可以直接记录日志
#日志级别
#CRITICAL50
#ERROR40
#WARNING30
#INFO20
#DEBUG10
logging.basicConfig()函数中的具体参数:
filename:指定的文件名创建FiledHandler,这样日志会被存储在指定的文件中;
filemode:文件打开方式,在指定了filename时使用这个参数,默认值为“w”还可指定为“a”;
format:指定handler使用的日志显示格式;
datefmt:指定日期时间格式。,格式参考strftime时间格式化(下文)
level:设置rootlogger的日志级别
stream:用指定的stream创建StreamHandler。可以指定输出到sys.stderr,sys.stdout或者文件,默认为sys.stderr。
若同时列出了filename和stream两个参数,则stream参数会被忽略。
logging模块测试
打印日志到标准输出中
importlogging
logging.debug('debugmessage')
logging.info('infomessage')
logging.warning('warningmessage')
输出结果
C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe
D:/pyworkpeace/tupian.py'https://www.tianyancha.com/login'
WARNING:root:warningmessage
Processfinishedwithexitcode0
可以看出默认情况下Python的logging模块将日志打印到了标准输出中,且只显示了大于等于WARNING级别的日志。默认的日志的格式为:
日志级别:Logger名称:用户输出消息
以上内容为大家介绍了python培训之怎么打印日志,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python函数标注是什么
python函数标注是什么在参数的使用方法中,还有一种解包的情况是需要我们掌握的。比如,将列表或者字典的值转换为函数的参数,就需要用到参数解...详情>>
2023-11-14 14:13:35
python正负索引的使用
python正负索引的使用本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、说明使用正索引(从左至右)、负索引(从右至左)访问元素,时...详情>>
2023-11-14 13:45:04
python垃圾回收的机制过程
python垃圾回收的机制过程本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、步骤(1)收集所有容器对象(list,dict,tuple,customClas...详情>>
2023-11-14 13:05:15
pythontry语句的执行原理
pythontry语句的执行原理1、原理分析(1)执行try子句(try和except关键字之间的多行语句)。(2)如果没有异常发生,则跳过except子句并完成try语句...详情>>
2023-11-14 09:37:35热门推荐
技术干货






