基于Python log 的正确打开方式


本文摘自php中文网,作者不言,侵删。

这篇文章主要介绍了关于,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

保存代码到文件:logger.py

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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())

使用方法:

1

2

3

4

from logger import LOG

  

if __name__ == "__main__":

  LOG.info('Checking %s:%s ...' % (str(date), str(data_type)))

相关推荐:

基于python的多进程共享变量正确打开方式


以上就是基于Python log 的正确打开方式的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python怎么发送post

Python的import怎么用

如何保存Python代码

Python集合如何访问

Python获取csv文本指定数据方法

Python如何无视大小写

Python爬虫图片、操作excel

Python基础入门--函数

Python中单行注释以什么开头

Python是一种面向什么的高级语言

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...