0

I have python code that is making mysql calls. It logs all mysql errors (and sends me a notifiocation on google chat). However warnings such as this dont get reported which makes sense since they are not warnings. I would however like the mysql statement logged when there is a warning so I can fix the underlying issue. What is the best way to find those warnings and get them to the log(with the bad mysql statment

/usr/local/lib/python3.4/dist-packages/pymysql/cursors.py:170: Warning: (1366, "Incorrect string value: '\\xF3n Cha...' for column 'recipient-name' at row 1")

.

try:
     cursor.execute(query_string, field_split)
         db.commit()
     except pymysql.err.InternalError as e:
        logger.warning('Mysql Error : %s', e)
        logger.warning('Statement : %s', cursor._last_executed)
        string_google= str(e.args[1] + ' - ' + cursor._last_executed)
        googlechat(string_google)
        return #exit rather then marking report run good
1
  • "What is the best way to find those warnings and get them to the log(with the bad mysql statment " SHOW WARNINGS Commented Aug 26, 2019 at 14:48

1 Answer 1

1

Use catch_warnings from the warnings module. It's a context manager that provides you with a list of the warnings. The code would look something like this:

with warnings.catch_warnings(record=True) as w:
    function_that_triggers_warning()
    if w:
         logging_function(w[-1])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.