基于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设计】。

相关文章

python 字符串追加实例

通过一个for循环,将一个一个字符追加到字符串中: 方法一: string = '' str=u"追加字符" for i in range(len(str)): string+=...

Python生成随机验证码的两种方法

使用python生成随机验证码的方法有很多种,今天小编给大家分享两种方法,大家可以灵活运用这两种方法,设计出适合自己的验证码方法。 方法一: 利用range方法,对于range方法不清楚...

Python 调用DLL操作抄表机

# -*- coding: GBK -*- from ctypes import * dll = windll.LoadLibrary('JBA188.dll') a = dll.tes...

Python面向对象程序设计之继承与多继承用法分析

本文实例讲述了Python面向对象程序设计之继承与多继承。分享给大家供大家参考,具体如下: 1. 继承 在C++和Java中,使用继承时,子类的构造函数会自动调用父类的构造函数,但在Py...

python迭代器常见用法实例分析

本文实例讲述了python迭代器常见用法。分享给大家供大家参考,具体如下: 迭代器 迭代是访问集合元素的一种方式。迭代器是一个可以记住遍历的位置的对象。迭代器对象从集合的第一个元素开始访...