Skip to content

Instantly share code, notes, and snippets.

@MBRCTV
Created June 24, 2019 07:30
Show Gist options
  • Save MBRCTV/7c9e6fda4ab6b18ff4ba3a00e26c6217 to your computer and use it in GitHub Desktop.
Save MBRCTV/7c9e6fda4ab6b18ff4ba3a00e26c6217 to your computer and use it in GitHub Desktop.
logger
import logging
import logging.handlers
MAILHOST = 'imap.gmail.com'
FROM = '***@gmail.com'
TO = ['***@gmail.com' ,'**** ']
# SUBJECT = 'Test Logging email from Python logging module (buffering)'
PASSWORD = '****'
# logging_example.py
# Create a custom logger
# logger = logging.getLogger(__name__)
class Logger(object):
def __init__(self, name='logger',level=logging.INFO, c_level=logging.DEBUG, e_level=logging.WARNING,f_level=logging.DEBUG,subject='[Logger]'):
self.logger = logging.getLogger(name)
self.logger.setLevel(level)
# Create handlers
c_handler = logging.StreamHandler()
f_handler = logging.FileHandler(f'{__name__}.log')
e_handler = logging.handlers.SMTPHandler(mailhost=(MAILHOST ,587),
fromaddr=FROM,
toaddrs=TO,
subject=subject,
credentials=(FROM ,PASSWORD),
secure=())
c_handler.setLevel(c_level)
e_handler.setLevel(e_level)
f_handler.setLevel(f_level)
# Create formatters and add it to handlers
c_format = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
f_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
e_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
c_handler.setFormatter(c_format)
f_handler.setFormatter(f_format)
e_handler.setFormatter(e_format)
# Add handlers to the logger
self.logger.addHandler(c_handler)
self.logger.addHandler(f_handler)
self.logger.addHandler(e_handler)
def debug(self, msg):
self.logger.debug(msg)
def info(self, msg):
self.logger.info(msg)
def warning(self, msg):
self.logger.warning(msg)
def error(self, msg):
self.logger.error(msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment