基于Python log 的正确打开方式

yipeiwu_com6年前Python基础

保存代码到文件: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 的正确打开方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

简单的连接MySQL与Python的Bottle框架的方法

Python关于mySQL的连接插件众多,Bottle下也有人专门开发的插件:bottle-mysql具体使用方法见官方,总共感觉其用法限制太多,其使用起来不方便,最适合的当然是,myS...

django 发送手机验证码的示例代码

django 发送手机验证码的示例代码

一、流程分析: 1.用户在项目前端,输入手机号,然后点击【获取验证码】,将手机号发到post到后台。 2.后台验证手机号是否合法,是否已被占用,如果通过验证,则生成验证码,并通过运行脚本...

Python实现将HTML转成PDF的方法分析

Python实现将HTML转成PDF的方法分析

本文实例讲述了Python实现将HTML转成PDF的方法。分享给大家供大家参考,具体如下: 主要使用的是wkhtmltopdf的Python封装——pdfkit 安装 1. Instal...

浅谈function(函数)中的动态参数

我们可向函数传递动态参数,*args,**kwargs,首先我们来看*args,示例如下: 1.show(*args) def show(*args): print(args,typ...

python实现快速排序的示例(二分法思想)

python实现快速排序的示例(二分法思想)

本文介绍了python实现快速排序的示例(二分法思想),分享给大家,具体如下: 实现思路 将所需要的数字存入一个列表中 1.首先,设置将最左侧的那个数设置为基准数,在列表中索引为0 2...