Using Logging with

New in version 0.6.0.

The following example shows how to set up logging for It is off by default in the library and will not pollute your logs.

import logging

import github3

# Set up a file to have all the logs written to
file_handler = logging.FileHandler("github_script.log")

# Send the logs to stderr as well
stream_handler = logging.StreamHandler()

# Format the log output and include the log level's name and the time it was
# generated
formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s")

# Use that Formatter on both handlers

# Get the logger used by internally by referencing its name
# directly
logger = logging.getLogger("github3")
# Add the handlers to it
# Set the level which determines what you see

# Make a library call and see the information posted
r = github3.repository("sigmavirus24", "")
print("{0} - {0.html_url}".format(r))

One thing to note is that if you want more detailed information about what is happening while the requests are sent, you can do the following:

import logging
urllib3 = logging.getLogger('requests.packages.urllib3')

And configure the logger for urllib3. Unfortunately, requests itself doesn’t provide any logging, so the best you can actually get is by configuring urllib3.

You will see messages about the following in the logs:

  • Construction of URLs used in requests, usually in the form: ('', 'repos', 'sigmavirus24', '')

  • What request is being sent, e.g., POST kwargs={}

  • If JSON is trying to be extracted from the response, what the response’s status code was, what the expected status code was and whether any JSON was actually returned.