Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions posthog/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,7 @@ def __init__(

# personal_api_key: This should be a generated Personal API Key, private
self.personal_api_key = personal_api_key
if debug:
# Ensures that debug level messages are logged when debug mode is on.
# Otherwise, defaults to WARNING level. See https://docs.python.org/3/howto/logging.html#what-happens-if-no-configuration-is-provided
logging.basicConfig()
self.log.setLevel(logging.DEBUG)
else:
self.log.setLevel(logging.WARNING)
self._set_up_logger(debug)

if self.enable_exception_autocapture:
self.exception_capture = ExceptionCapture(self, integrations=self.exception_autocapture_integrations)
Expand Down Expand Up @@ -925,6 +919,19 @@ def _add_local_person_and_group_properties(self, distinct_id, groups, person_pro

return all_person_properties, all_group_properties

def _set_up_logger(self, debug):
if debug:
# Ensures that debug level messages are logged when debug mode is on.
# Otherwise, defaults to WARNING level. See https://docs.python.org/3/howto/logging.html#what-happens-if-no-configuration-is-provided
logging.basicConfig()
self.log.setLevel(logging.DEBUG)
else:
self.log.setLevel(logging.WARNING)

handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('[%(name)s] %(message)s'))
log.addHandler(handler)
log.propagate = False # Prevents double logging (otherwise we'd have an extra log that doesn't use the handler)
Comment on lines +931 to +934
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! This is a much cleaner log statement.

There are a couple of blocking issues here:

  • log isn't qualified correctly - running this this would raise an exception
  • Because log is defined at the class level, multiple instances of Client will accumulate handlers and duplicate log messages.

Finally, I think it's worth including the log level in the formatter.

Suggested change
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('[%(name)s] %(message)s'))
log.addHandler(handler)
log.propagate = False # Prevents double logging (otherwise we'd have an extra log that doesn't use the handler)
if not self.log.handlers:
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter("[%(name)s] %(levelname)s - %(message)s"))
self.log.addHandler(handler)
self.log.propagate = False


def require(name, field, data_type):
"""Require that the named `field` has the right `data_type`"""
Expand Down