4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Python】ロギングでGmail送信する

Last updated at Posted at 2021-05-22

ロギングの全体像としては下の記事をご覧いただきまして、今回はメール送信部分のみ扱います。

https://qiita.com/ryoheiszk/items/362ae8ce344966b5516c

はじめに

Gmail経由でエラーログを送信したいと思ったわけですが、これといった記事が見つからなかっため困っていました。

何とか動作したため、ここに共有しておきます。

全体の流れ

logging.handlers.SMTPHandler()はTLSに対応していないため、オーバーライドしてTlsSMTPHandler()を作ります。

そこから生成したインスタンスにおいて、Gmail用の設定を流し込んで完了です。

実際のコード

module.py
import logging
import logging.handlers


class TlsSMTPHandler(logging.handlers.SMTPHandler):
    """
    loggingをGmailで送信させるために、SMTPHandlerをオーバーライドしてTLSに対応させる。
    参考: https://gist.github.com/Agasper/8ef727892f7d8d63e0ac
    """
    def emit(self, record):
        """
        Emit a record.

        Format the record and send it to the specified addressees.
        """
        try:
            import smtplib
            try:
                from email.utils import formatdate
            except ImportError:
                formatdate = self.date_time
            port = self.mailport
            if not port:
                port = smtplib.SMTP_PORT
            smtp = smtplib.SMTP(self.mailhost, port)
            msg = self.format(record)
            msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\n\r\n%s" % (
                self.fromaddr,
                ",".join(self.toaddrs),
                self.getSubject(record),
                formatdate(), msg)
            if self.username:
                smtp.ehlo()  # for tls add this line
                smtp.starttls()  # for tls add this line
                smtp.ehlo()  # for tls add this line
                smtp.login(self.username, self.password)
            smtp.sendmail(self.fromaddr, self.toaddrs, msg)
            smtp.quit()
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            self.handleError(record)


def set_logger(module_name):
    """共通のロガーを定義する

    Args:
        module_name (str)

    Returns:
        logger
    """
    logger = logging.getLogger(module_name)

    emailHandler = TlsSMTPHandler(
        mailhost=("smtp.gmail.com", 587),
        fromaddr="from_gmail",
        toaddrs=["to_email"],
        subject="{Urgent} CHECK LOGS",
        credentials=("from_gmail", "google_app_pw")
    )

    formatter = logging.Formatter(
        "%(asctime)s [%(levelname)s] (%(filename)s | %(funcName)s | %(lineno)s) %(message)s")

    emailHandler.setFormatter(formatter)

    logger.setLevel(logging.DEBUG)
    emailHandler.setLevel(logging.WARNING)

    logger.addHandler(emailHandler)

    return logger
main.py
import module


logger = source.module.set_logger(__name__)

logger.debug("debug")
logger.info("info")
logger.warning("warning")
4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?