Last active
December 11, 2015 14:58
-
-
Save JFK/4617714 to your computer and use it in GitHub Desktop.
logger sample code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import settings | |
import sys | |
import traceback | |
import logging | |
import smtplib | |
from email.MIMEText import MIMEText | |
from email.Header import Header | |
from email.Utils import formatdate | |
if settings.application["debug"] == True: | |
logging.getLogger().setLevel(logging.DEBUG) | |
def info(string): | |
logging.info(string) | |
def warn(string): | |
logging.warning(string) | |
def mail(): | |
try: | |
exc_type, exc_value, exc_traceback = sys.exc_info() | |
body += "\n\n" | |
body += "-"*60 | |
body += "\n".join(traceback.format_exception(exc_type, exc_value, exc_traceback)) | |
body += "-"*60 | |
body += "\n\n" | |
from_addr = settings.application["mail"]["noreply"] | |
encoding = settings.application["mail"]["encoding"] | |
to_addr = 'error@mailhostname' | |
subject = 'API Warning' | |
to_addr = to_addr.rstrip(' ') | |
msg = MIMEText(body, 'plain', encoding) | |
msg['Subject'] = Header(subject, encoding) | |
msg['From'] = from_addr | |
msg['To'] = to_addr | |
msg['Date'] = formatdate() | |
s = smtplib.SMTP() | |
s.connect() | |
s.sendmail(from_addr, [to_addr], msg.as_string()) | |
s.close() | |
except Exception, e: | |
warn(str(e)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment