Skip to content

Instantly share code, notes, and snippets.

@JFK
Last active December 11, 2015 14:58
Show Gist options
  • Save JFK/4617714 to your computer and use it in GitHub Desktop.
Save JFK/4617714 to your computer and use it in GitHub Desktop.
logger sample code
#!/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