I'm using SMTPHandler to log my caught exceptions in my python script which requests new data to my backend.
Here's how I initialized my SMTPHandler:
# write errors to email
error_mail_subject = "ERROR: Script error in %s" % sys.argv[0]
error_mail_handler = logging.handlers.SMTPHandler(mailhost=("smtp.gmail.com", 587),
fromaddr="test#gmail.com",
toaddrs="test#gmail.com",
subject=error_mail_subject)
error_mail_handler.setLevel(logging.ERROR)
error_mail_handler.setFormatter(debug_format)
# Build the logger
logger = logging.getLogger()
logger.addHandler(error_mail_handler)
But unfortunately, I get this following error during handling of a script exception
smtplib.SMTPSenderRefused: (530, b'5.7.0 Must issue a STARTTLS command first. w15sm3562054qta.16 - gsmtp', 'test#gmail.com')
When the script calls:
logger.exception(e)
This successfully sent an email from my gmail, tested just now:
import logging.handlers
error_mail_subject = "hello, it's me"
error_mail_handler = logging.handlers.SMTPHandler(mailhost=("smtp.gmail.com", 587),
fromaddr="email",
toaddrs="email",
subject=error_mail_subject,
credentials=('email', 'password'),
secure=())
error_mail_handler.setLevel(logging.ERROR)
logger = logging.getLogger()
logger.addHandler(error_mail_handler)
logger.exception(Exception("hey"))
I also had to sign into gmail and then go here to allow access to less secure apps: https://www.google.com/settings/security/lesssecureapps
Related
This code snippet, linked here, should be sending a log message to Papertrail, unfortunately, there's nothing happening at all (no error message either). I did replace the "N" and the "XXXXX" with my own values. The environment is Python 3.10.6 and I'm inside of a virtual environment created by Poetry.
I would expect calling an undefined function to log an exception to Papertrail.
Telnet works fine and my other applications work fine as well.
import logging
import socket
from logging.handlers import SysLogHandler
syslog = SysLogHandler(address=('logsN.papertrailapp.com', XXXXX))
format = '%(asctime)s YOUR_APP: %(message)s'
formatter = logging.Formatter(format, datefmt='%b %d %H:%M:%S')
syslog.setFormatter(formatter)
logger = logging.getLogger()
logger.addHandler(syslog)
logger.setLevel(logging.INFO)
def my_handler(type, value, tb):
logger.exception('Uncaught exception: {0}'.format(str(value)))
# Install exception handler
sys.excepthook = my_handler
logger.info('This is a message')
nofunction() #log an uncaught exception
I am trying to setup AI similar to how it is being done here
I am using Python 3.9.13 and following packages: opencensus==0.11.0, opencensus-ext-azure==1.1.7, opencensus-context==0.1.3
My code looks something like this:
import logging
import time
from opencensus.ext.azure.log_exporter import AzureLogHandler
# create the logger
app_insights_logger = logging.getLogger(__name__)
# set the handler
app_insights_logger.addHandler(AzureLogHandler(
connection_string='InstrumentationKey=00000000-0000-0000-0000-000000000000')
)
# set the logging level
app_insights_logger.setLevel(logging.INFO)
# this prints 'logging level = 20'
print('logging level = ',app_insights_logger.getEffectiveLevel())
# try to log an exception
try:
result = 1 / 0
except Exception:
app_insights_logger.exception('Captured a math exception.')
app_insights_logger.handlers[0].flush()
time.sleep(5)
However the exception does not get logged, I tried adding the explicit flush as mentioned in this post
Additionally, I tried adding the instrumentation key as mentioned in the docs, when that didn't work I tried with the entire connection string(the one with the ingestion key)
So,
How can I debug if my app is indeed sending requests to Azure ?
How can I check on the Azure portal if it is a permission issue ?
You can set the severity level before logging any kind of telemetry information to Application Insights.
Note: By default, root logger can be configured with warning severity. if you want to add other severity information you have to set like (logger.setLevel(logging.INFO)).
I am using below code to log the telemetry information in Application Insights
import logging
from logging import Logger
from opencensus.ext.azure.log_exporter import AzureLogHandler
AI_conn_string= '<Your AI Connection string>'
handler = AzureLogHandler(connection_string=AI_conn_string)
logger = logging.getLogger()
logger.addHandler(handler)
#by default root logger can be configured with warning severity.
logger.warning('python console app warning log in AI ')
# setting severity for information level logging.
logger.setLevel(logging.INFO)
logger.info('Test Information log')
logger.info('python console app information log in AI')
try:
logger.warning('python console app Try block warning log in AI')
result = 1 / 0
except Exception:
logger.setLevel(logging.ERROR)
logger.exception('python console app error log in AI')
Results
Warning and Information log
Error log in AI
How can I debug if my app is indeed sending requests to Azure ?
We cannot debug the information whether the telemetry data send to Application Insights or not. But we can see the process like below
How can I check on the Azure portal if it is a permission issue ?
Instrumentation key and connection string has the permission to access the Application Insights resource.
When sending any message via Flask-Mail, such as below, the last line with mail.send(msg) will also result in the mail headers and content being logged.
Message("Hello", sender="from#example.com", recipients=["to#example.com"])
msg.body = 'anything'
mail.send(msg)
As my mail may contain sensitive information, I want to disable this logging entirely. Yet, playing around with the logging module, I could not find a logger configured for Flask-Mail.
How do I disable logging in Flask-Mail?
Figured this one out:
Flask-Mail uses Python's smtplib to send mail. smtplib does not use the logging module, but it prints information for debugging to stderr.
smtplib includes following method:
def set_debuglevel(self, debuglevel):
"""Set the debug output level.
A non-false value results in debug messages for connection and for all
messages sent to and received from the server.
"""
self.debuglevel = debuglevel
If we use Flask-Mail, we can set this variable when we initialize our app like this:
app = Flask(__name__)
mail = Mail(app)
app.extensions['mail'].debug = 0
Any output is now suppressed.
I read some documentation and some examples about Logging in Flask. Then I write this piece of code:
ADMINS = [config["sys_admin_email"]]
if not app.debug:
from logging.handlers import SMTPHandler
mail_handler = SMTPHandler(mailhost=('smtp.email.com', 587),
credentials=('helpdesk#email.com', 'fsociety'),
fromaddr='helpdesk#email.com',
toaddrs=ADMINS, subject='Service Failed')
mail_handler.setLevel(logging.ERROR)
# config the format
mail_handler.setFormatter(logging.Formatter('''
Message type: %(levelname)s
Location: %(pathname)s:%(lineno)d
Module: %(module)s
Function: %(funcName)s
Time: %(asctime)s
Message:
%(message)s
'''))
app.logger.addHandler(mail_handler)
But I can't understand how the mail_handler will handle with a exception ?
I ask that because I want to notify my sysadmin when an Internal Error (500) raises.
Note: Maybe I need a error handler so I created this method to catch all Internal Errors. But doesn't work.
#app.errorhandler(500)
def reportToSysAdmin(e):
app.logger.error(e)
print "email sent"
Please have a look at the following link:
http://flask.pocoo.org/docs/0.10/errorhandling/
Details:
Above code created a new SMTPHandler that will send mails with the mail server listening on 127.0.0.1 to all the ADMINS from the address server-error#example.com with the subject “Service Failed”. If your mail server requires credentials, these can also be provided. For that check out the documentation for the SMTPHandler.
I am attempting to get an email sent to me any time an error occurs in my Flask application. The email is not being sent despite the handler being registered. I used smtplib to verify that my SMTP login details are correct. The error is displayed in Werkzeug's debugger, but no emails are sent. How do I log exceptions that occur in my app?
import logging
from logging.handlers import SMTPHandler
from flask import Flask
app = Flask(__name__)
app.debug = True
app.config['PROPAGATE_EXCEPTIONS'] = True
if app.debug:
logging.basicConfig(level=logging.INFO)
# all of the $ names have actual values
handler = SMTPHandler(
mailhost = 'smtp.mailgun.org',
fromaddr = 'Application Bug Reporter <$mailgun_email_here>',
toaddrs = ['$personal_email_here'],
subject = 'Test Application Logging Email',
credentials = ('$mailgun_email_here', '$mailgun_password_here')
)
handler.setLevel(logging.ERROR)
app.logger.addHandler(handler)
#app.route('/')
def index():
raise Exception('Hello, World!') # should trigger an email
app.run()
The issue was which logger the handler was added to. Flask uses the werkzeug logger to log exceptions during view functions, not the base app.logger. I had to register my handler with the werkzeug logger:
logging.getLogger('werkzeug').addHandler(handler)
In addition, I had to include the port in mailhost:
handler = SMTPHandler(
mailhost=('smtp.mailgun.org', 587),
fromaddr='Application Bug Reporter <$mailgun_email_here>',
toaddrs=['$personal_email_here'],
subject='Test Application Logging Email',
credentials=('$mailgun_email_here', '$mailgun_password_here')
)