时间:2021-07-01 10:21:17 帮助过:71人阅读
保存代码到文件: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的多进程共享变量正确打开方式
以上就是基于Python log 的正确打开方式的详细内容,更多请关注Gxl网其它相关文章!