The correct way to open log based on Python

不言
Release: 2018-05-04 14:41:44
Original
2025 people have browsed it

This article mainly introduces the relevant information and has certain reference value. Now I share it with everyone. Friends in need can refer to it

Save the code to the file: 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())
Copy after login

Usage:

from logger import LOG 
 
if __name__ == "__main__": 
  LOG.info('Checking %s:%s ...' % (str(date), str(data_type)))
Copy after login

Related recommendations:

Correct way to open multi-process shared variables based on python


The above is the detailed content of The correct way to open log based on Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template