Skip to content

Instantly share code, notes, and snippets.

@dtaivpp
Last active September 9, 2020 15:41
Show Gist options
  • Save dtaivpp/6e557ba08a1bc33dcfca7e2c093ebf87 to your computer and use it in GitHub Desktop.
Save dtaivpp/6e557ba08a1bc33dcfca7e2c093ebf87 to your computer and use it in GitHub Desktop.
import logging
import requests
logger = logging.getLogger(__name__)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
fileHandle = logging.FileHandler('failed_requests.log')
fileHandle.setLevel(logging.WARNING)
fileHandle.setFormatter(formatter)
logger.addHandler(fileHandle)
# Credit to Spartas for this solution https://github.com/spartas
def successful_request(response):
status_prefix = response.status_code // 100
if status_prefix == 1:
logger.debug(status_formatter(response.status_code, reponse.url))
if status_prefix == 3:
logger.notice(status_formatter(response.status_code, reponse.url))
if status_prefix == 4:
logger.warning(status_formatter(response.status_code, reponse.url))
if status_prefix == 5:
logger.error(status_formatter(response.status_code, reponse.url))
return (status_prefix <= 3)
def status_formatter(code, url):
return f'Response Code: {code} URL: {url}'
@dtaivpp
Copy link
Author

dtaivpp commented Jul 12, 2020

@spartas that is a good call. This is why I shouldn't write code past midnight. I'll update and throw an edit on my post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment