基于Python log 的正确打开方式
更新时间:2018年04月28日 15:25:20 作者:life4711
下面小编就为大家分享一篇基于Python log 的正确打开方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
保存代码到文件:logger.py
import os
import logbook
from logbook.more import ColorizedStderrHandler
import smtplib
LOG_DIR = os.path.join('log')
if not os.path.exists(LOG_DIR):
os.makedirs(LOG_DIR)
def get_logger(name='test', file_log=False):
logbook.set_datetime_format('local')
ColorizedStderrHandler(bubble=False).push_application()
if file_log:
logbook.TimedRotatingFileHandler(os.path.join(LOG_DIR, '%s.log' % name), date_format='%Y%m%d', bubble=True).push_application()
return logbook.Logger(name)
LOG = get_logger(file_log=True)
def send_email(email_conf, message):
smtp = smtplib.SMTP()
smtp.connect(email_conf['host'], email_conf['port'])
smtp.login(email_conf['user'], email_conf['password'])
smtp.sendmail(email_conf['fromaddr'], email_conf['recipients'], message.as_string())
使用方法:
from logger import LOG
if __name__ == "__main__":
LOG.info('Checking %s:%s ...' % (str(date), str(data_type)))
以上这篇基于Python log 的正确打开方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Scrapy基于scrapy_redis实现分布式爬虫部署的示例
这篇文章主要介绍了Scrapy基于scrapy_redis实现分布式爬虫部署的示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-09-09
Python中列表list以及list与数组array的相互转换实现方法
这篇文章主要介绍了Python中list以及list与array的相互转换实现方法,简单分析了Python中list的功能、使用方法及list与array相互转换实现技巧,需要的朋友可以参考下2017-09-09


最新评论