Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

requests logging is not documented, incomplete and works unexpectedly #1297

Closed
ssbarnea opened this issue Apr 9, 2013 · 10 comments
Closed
Assignees

Comments

@ssbarnea
Copy link
Contributor

ssbarnea commented Apr 9, 2013

Currently logging in requests library is not properly documented.

The API Changes section of the documentation reminds about the removal of the config parameter but contains no code. See http://docs.python-requests.org/en/latest/api/?highlight=logging

import requests
import logging

# these two lines enable debugging at httplib level (requests->urllib3->httplib)
# you will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.
# the only thing missing will be the response.body which is not logged.
import httplib
httplib.HTTPConnection.debuglevel = 1

logging.basicConfig() # you need to initialize logging, otherwise you will not see anything from requests
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

requests.get('http://httpbin.org/headers')

So this means there are a few things to do:
#1 add the above piece of code to the documentation (including the API changes section)
#2 provide proper response on few stackoverflow questions related to requests logging
#3 find a way to allow extensive logging (logging BODY too)

@sigmavirus24
Copy link
Contributor

requests does no logging of its own. All logging actually done is done by urllib3 and can be set up similar to your example:

import requests
import logging

logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

Even if we did do logging, we would not log the data sent in a post. You can, however, do the following:

response = requests.get(url)
logging.debug("Response: Status code: %d", response.status_code)
logging.debug("Request: %s %s %s", response.request.method, response.request.url, response.request.body)

I'm only leaving this ticket open as a reminder for myself during the documentation refresh I have planned.

@ghost ghost assigned sigmavirus24 Apr 9, 2013
@ssbarnea
Copy link
Contributor Author

ssbarnea commented Apr 9, 2013

Have you tried to run the first piece of code? I will not log anything unless you call logging.debug("...") first. I tried with requests 1.2.0 and it will not log anything until you log something.

@sigmavirus24
Copy link
Contributor

No I didn't run it because I know for a fact that requests proper does no logging of its own. All logging is done by urllib3 and I strongly suspect that the logging module is only working because urllib3 is technically part of the requests namespace. I'm also writing all of this from a phone so I can't exactly run anything at the moment.

@ssbarnea
Copy link
Contributor Author

ssbarnea commented Apr 9, 2013

Thanks Ian,
I do think that having a way to log what requests is sending and receiving would be extremely useful and should be part of requests itself.
Why? For example I am trying to debug python-jira package and if I want to see what is sent/received, the only way would be use Wireshark, because it would be a mess to apply your model after all calls made to requests (there are plenty!).
We could even use a custom logging level, one that would be lower than logging.DEBUG, so it would not change the current behavior unless you choose to switch to the more verbose mode.
I could implement this, but first I do want to find out that you do not have anything against this, I don't want to end-up using my custom requests library, and patch it after each requests release.

@Lukasa
Copy link
Member

Lukasa commented Apr 9, 2013

Adding extra detail was proposed in #988. Here, as there, any extra logging belongs in urllib3 and should be added there. This is being discussed at urllib3/urllib3#107.

@sigmavirus24
Copy link
Contributor

Requests had logging before 1.0 and it was torn out during the refactor. Take from that what you will. I'm entirely indifferent on the proposal. It's up to @kennethreitz no matter what.

@jvtm
Copy link

jvtm commented Apr 10, 2013

AFAIK, the behavior in the example is because of python logging module itself.

If "root logger" is not initialized, nothing will be outputted. Calling logging.debug() (or .info or any other) initializes it with default settings. I think this is the same thing as calling logging.basicConfig() first.

My guess is that the same thing happens with any python library that uses logging.

If you want output (to console. file, whatever), then you need to initialize the logging to do so.

@ssbarnea
Copy link
Contributor Author

@jvtm Thanks! Now I can update the sample code and so we can include it in the documentation.

ssbarnea added a commit to ssbarnea/requests that referenced this issue Apr 10, 2013
* Added build directory and *.egg to .gitignore
* Added sphinx as setup requirement in order to be able to build documentation with `pyhton setup.py build_sphinx`

modified:   .gitignore
modified:   docs/api.rst
modified:   setup.py
kennethreitz pushed a commit that referenced this issue Apr 14, 2013
Documents the actual logging methods #1297
@sigmavirus24
Copy link
Contributor

@ssbarnea's pull request was merged closing this issue.

@Justsoos
Copy link

Justsoos commented Mar 22, 2020

yeah, as @jvtm said, only initialize the "root logging" could set the log active for import module, and logging module can not match "requests.packages.urllib3", but "urllib3", like:

import requests
import logging

logger = logging.getLogger("urllib3")
logger.setLevel(level=logging.DEBUG)
logging.debug("")

working on python 3.6

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants