What's Changed
New Contributors
* KrinsKumar made their first contribution in https://github.com/u-ways/logging-http-client/pull/21
* spyingcyclops made their first contribution in https://github.com/u-ways/logging-http-client/pull/22
Summary
* Feat: Support for default logging by KrinsKumar in https://github.com/u-ways/logging-http-client/pull/21
* Docs: Integrate with ReadTheDocs Service by spyingcyclops in https://github.com/u-ways/logging-http-client/pull/23
* Refactor: 12 14 24 - Improve The Logging Client Native Requests Feature Support And Enable Multiple Hooks & Obscurers by u-ways in https://github.com/u-ways/logging-http-client/pull/25
Details
1. Read The Docs Endpoint
This is one of the bigger releases for this library, for starters, we now have ReadTheDocs endpoint for those who prefer reading the documentation there: https://logging-http-client.readthedocs.io/en/latest/
2. Ability to Customize The Default Hook Log Level
On top of that, we now support customizing the default logging hooks log level, by default, they will log at the `INFO` level, but from now on, you can specify the desired level as an integer per Python log level setting conventions:
python
import logging
import logging_http_client
"""
CRITICAL = 50
ERROR = 40
WARNING = 30
INFO = 20
DEBUG = 10
NOTSET = 0
"""
logging_http_client.set_default_hooks_logging_level(logging.DEBUG)
logging_http_client.create().get('https://www.python.org')
=> Logs will be recorded at the DEBUG level now.
3. Support For Accumulative Obscurers And Multiple Logging Hooks
Previously, you could only provide a single logging hook for requests or response logging. From now on, the configurations allows you to provide multiple obscurers and multiple looks:
python
import logging
from requests import Response
import logging_http_client
def custom_response_logging_hook(logger: logging.Logger, response: Response):
logger.debug("Custom response logging for %s", response.url)
def custom_response_logging_hook_2(logger: logging.Logger, response: Response):
logger.debug("Another custom response logging for %s", response.url)
logging_http_client.set_response_logging_hooks(
[custom_response_logging_hook, custom_response_logging_hook_2]
)
logging_http_client.create().get('https://www.python.org')
=> Log records will include:
{ message { "Custom response logging for https://www.python.org" } }
{ message { "Another custom response logging for https://www.python.org" } }
Here is how you can also utilize both in your application:
python
import logging
import logging_http_client
from requests import PreparedRequest
from logging_http_client import HttpLogRecord
def custom_request_logging_hook(logger: logging.Logger, request: PreparedRequest):
logger.debug(
"Custom request logging for %s",
request.url,
IMPORTANT: Usage of this static method will automatically apply the obscurers for you.
extra=HttpLogRecord.from_request(request)
)
def first_request_obscurer(record: HttpLogRecord) -> HttpLogRecord:
if record.request_headers.get("Authorization"):
record.request_headers["Authorization"] = "Bearer ****"
return record
def second_request_obscurer(record: HttpLogRecord) -> HttpLogRecord:
if record.request_body:
record.request_body = "OBSCURED_BODY"
return record
logging_http_client.enable_request_body_logging()
logging_http_client.set_request_logging_hooks([custom_request_logging_hook])
logging_http_client.set_request_log_record_obscurers([first_request_obscurer, second_request_obscurer])
root_logger = logging.getLogger()
root_logger.setLevel(level=logging.DEBUG)
client = logging_http_client.create(logger=root_logger)
client.post(
url="https://www.python.org",
headers={"accept": "application/json", "Authorization": "Bearer secret"},
json={"sensitive": "data"},
)
=> Log record will include:
{ http { 'request_headers': { 'Authorization': 'Bearer ****', ... }, 'request_body': 'OBSCURED_BODY', ... }
`
The older methods are still there for backwards compatibility but they're marked as deprecated and will be removed in a future (next) `logging-http-client` version.
For further details, read: https://github.com/u-ways/logging-http-client?tab=readme-ov-file#iii-activating-the-log-record-obscurer-in-your-own-custom-logging-hooks
4. The Logging Session Implementation Details Has Been Greatly Improved.
Finally, the logging session implementation was a bit rough, didn't factor in prepared statements (prepared requests), and was unnecessarily complex. I have revised the logic over this weekend and now it's much easier to understand and maintain for future contributors. 🙂
**Full Changelog**: https://github.com/u-ways/logging-http-client/compare/2.32.3.6...2.32.3.7