基于Python log 的正确打开方式

yipeiwu_com7年前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设计】。

相关文章

pytorch程序异常后删除占用的显存操作

1-删除模型变量 del model_define 2-清空CUDA cache torch.cuda.empty_cache() 3-步骤2(异步)需要一定时间,设置时延...

Python搭建Spark分布式集群环境

Python搭建Spark分布式集群环境

前言 Apache Spark 是一个新兴的大数据处理通用引擎,提供了分布式的内存抽象。Spark 最大的特点就是快,可比 Hadoop MapReduce 的处理速度快 100 倍。本...

python 遍历pd.Series的index和value

遍历pd.Series的index和value的方法如下,python built-in list的enumerate方法不管用 for i, v in s.items(): p...

Python http接口自动化测试框架实现方法示例

Python http接口自动化测试框架实现方法示例

本文实例讲述了Python http接口自动化测试框架实现方法。分享给大家供大家参考,具体如下: 一、测试需求描述 对服务后台一系列的http接口功能测试。 输入:根据接口描述构造不同的...

Python closure闭包解释及其注意点详解

Python closure闭包解释及其注意点详解

一、闭包 1.定义:当一个函数在内部定义函数,并且内部的函数应用外部函数的参数或者局部变量,当内部函数被当做返回值的时候,相关参数和变量保存在返回的函数之中,这种结果,叫做闭包。 2.例...