Skip to content

Added logger with basic debugging info #86

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

Merged
merged 1 commit into from
Nov 30, 2022
Merged

Conversation

fnesveda
Copy link
Member

Logging was missing for a long time from the client, and it made it harder to debug the client.

This adds some basic logging to the HTTP calls, using the standard Python logger, enriched by contextual information from the client methods which are doing the HTTP calls by some funky metaclass programming.

There is a _DebugLogFormatter class which is great for testing, used like this:

import logging
from apify_client._logging import _DebugLogFormatter

client_logger = logging.getLogger('apify_client')
client_logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
handler.setFormatter(_DebugLogFormatter())
client_logger.addHandler(handler)

@fnesveda fnesveda added the adhoc Ad-hoc unplanned task added during the sprint. label Nov 29, 2022
@fnesveda fnesveda added this to the 52nd sprint - Platform team milestone Nov 29, 2022
@fnesveda fnesveda self-assigned this Nov 29, 2022
@github-actions github-actions bot added the t-core-services Issues with this label are in the ownership of the core services team. label Nov 29, 2022
@fnesveda fnesveda merged commit 7d30b6c into master Nov 30, 2022
@fnesveda fnesveda deleted the feature/add-logger branch November 30, 2022 09:02
Copy link
Contributor

@mhamas mhamas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the biggest python magic I've every seen :D. I've never worked with context variables and meta classes, but after reading up on them the usage here looks good to me. A thanks for teaching me something new!

Comment on lines +202 to +206
The log records have useful properties added with the `extra` argument,
like `attempt`, `status_code`, `url`, `client_method` and `resource_id`.
To print those out, you'll need to use a custom log formatter.
To learn more about log formatters and how to use them,
please refer to the official Python [documentation on logging](https://docs.python.org/3/howto/logging.html#formatters).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be beneficial here to show a simple example with custom log formatter? So the users don't need to go dig in the official docs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to write something, but then I realized there's so many ways how users can have their formatters configured, and I kinda gave up. Usually, if you need logging in your app, you're already using some log formatters, and you won't be learning about them from this library.

Anyway, the logging here is most useful to us while developing the client, not so much to the users (especially the extra log properties), maybe I'll add a mention of the _DebugLogFormatter somewhere to README or something like that.

@fnesveda
Copy link
Member Author

@mhamas You're welcome! Python can be quite powerful, I'm glad we're now doing the Python SDK because I'm getting to dive deep into it. Currently about to send my first PR to CPython 😄

@fnesveda fnesveda added the validated Issues that are resolved and their solutions fulfill the acceptance criteria. label May 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adhoc Ad-hoc unplanned task added during the sprint. t-core-services Issues with this label are in the ownership of the core services team. validated Issues that are resolved and their solutions fulfill the acceptance criteria.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants