1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| import os import json import logging.config
mail_from = os.environ['MAIL_FROM'] rcpt_to = os.environ['RCPT_TO'] smtp_server = 'smtp.gmail.com' smtp_port = 587 smtp_user = os.environ['SMTP_USER'] smtp_password = os.environ['SMTP_PASSWORD']
config = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'simple': { 'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s' } }, 'handlers': { 'consoleHandler': { 'level': 'DEBUG', 'formatter': 'simple', 'class': 'logging.StreamHandler', }, 'smtpHandler': { 'level': 'ERROR', 'formatter': 'simple', 'class': 'logging.handlers.SMTPHandler', 'mailhost': (smtp_server, smtp_port), 'fromaddr': mail_from, 'toaddrs': [rcpt_to], 'subject': "error log", 'credentials': (smtp_user, smtp_password), 'secure': (), } }, 'loggers': { '': { 'handlers': ['consoleHandler', 'smtpHandler'], 'level': "INFO", } } }
logger = logging.getLogger(__name__) logging.config.dictConfig(config) logger.debug('Debug log message') logger.info('Info log message') logger.warning('Warning log message') logger.error('Error log message')
|