From 4f7cabe55786a4c7d21f7154e31e5cb8da338b41 Mon Sep 17 00:00:00 2001 From: Stanislav Kozlovski Date: Fri, 29 Nov 2024 16:46:27 +0100 Subject: [PATCH] MINOR: Set a prefix in the logger The `posthog` logger would log without a prefix, which would cause confusion with users of the library. This patch adds a simple prefix to the log message - the new format is `[posthog] ` --- posthog/client.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/posthog/client.py b/posthog/client.py index b12764c8..0c501cdb 100644 --- a/posthog/client.py +++ b/posthog/client.py @@ -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) @@ -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) def require(name, field, data_type): """Require that the named `field` has the right `data_type`"""