python logging 模块

本文介绍了一个Python日志模块的配置与使用方法,包括如何设置日志级别、格式及输出方式等。通过示例代码展示了如何将日志输出到文件和控制台,并实现了日志文件的滚动保存。

使用时

from my_log import  *

init_log(path,logging.DEBUG)

#注意:这样使用后,因为使用了系统默认的logging,你程序调用的其他第三方库如果也是使用logging的也会按照你格式输出到指定文件



############my_log.py###############

import logging

from logging.handlers import RotatingFileHandler


def init_log(file_name, level):
Rthandler = RotatingFileHandler(file_name, maxBytes=10*1024*1024,backupCount=5)
formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
Rthandler.setFormatter(formatter)


logging.getLogger('').addHandler(Rthandler)




#屏幕输出
console = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)


#logging.getLogger('').setLevel(logging.INFO)
logging.getLogger('').setLevel(level)






#测试代码
#logging.debug("debug message")
#logging.info("info message")
#logging.warn("warn message")
#logging.error(("error message",'asd'))
#logging.critical("critical message")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值