python怎么生成日志_如何创建一个日志文件来记录python中的每个步骤?

本文介绍了一个日志模块的使用方法,包括如何导入模块、配置日志级别和格式,并通过示例展示了不同条件下的日志输出。同时,文章还讨论了如何处理异常事件,通过设置不同的日志级别来记录不同程度的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

你知道,有一个^{}模块!在import logging

import os

logging.basicConfig(filename='tmp.log',

format='%(levelname)s %(asctime)s :: %(message)s',

level=logging.DEBUG)

# format is a formatter string, level shows what level of logs it will record

# in this case it is everything!

# Levels are as follows from most to least critical

# CRITICAL

# ERROR

# WARNING

# INFO

# DEBUG

do = True

yes = True

do_the_work = lambda: None

def main():

logging.debug("Inside of main()")

if do:

logging.debug("Inside of if do:")

do_the_work()

logging.debug("doing work")

if yes:

logging.debug("inside of if yes:")

do_the_work()

logging.debug("doing work")

for list in os.listdir('.'): # there were three files in my folder

logging.debug("inside of for each files/dirs calling list")

print('python')

logging.debug("calling sys.system python")

logging.debug("executing python with give dir path")

将产生以下输出:

^{pr2}$

您可以轻松地捕捉异常并让它们抛出更多关键事件。在try:

really_important_method()

except EndOfTheWorldError:

logging.critical("Duck and cover boys, it's gonna blow.")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值