Skip to content

set up logging #119

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions src/streamlit_analytics2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from .main import counts, start_tracking, stop_tracking, track # noqa: F401
import logging

__version__ = "0.8.7"

logging.getLogger(__name__).addHandler(logging.NullHandler())
22 changes: 12 additions & 10 deletions src/streamlit_analytics2/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
# as modules are only imported once by a streamlit app.
counts = {"loaded_from_firestore": False}

logging.info("SA2: Streamlit-analytics2 successfully imported")
logger = logging.getLogger(__name__)

logger.info("SA2: Streamlit-analytics2 successfully imported")


def reset_counts():
Expand Down Expand Up @@ -332,18 +334,18 @@ def start_tracking(
counts.update({k: json_counts[k] for k in json_counts if k in counts})

if verbose:
logging.info(f"{log_msg_prefix}{load_from_json}")
logging.info("SA2: Success! Loaded counts:")
logging.info(counts)
logger.info(f"{log_msg_prefix}{load_from_json}")
logger.info("SA2: Success! Loaded counts:")
logger.info(counts)

except FileNotFoundError:
if verbose:
logging.warning(
logger.warning(
f"SA2: File {load_from_json} not found, proceeding with empty counts."
)
except Exception as e:
# Catch-all for any other exceptions, log the error
logging.error(f"SA2: Error loading counts from {load_from_json}: {e}")
logger.error(f"SA2: Error loading counts from {load_from_json}: {e}")

# Reset session state.
if "user_tracked" not in st.session_state:
Expand Down Expand Up @@ -416,7 +418,7 @@ def start_tracking(
# }

if verbose:
logging.info("\nSA2: Tracking script execution with streamlit-analytics...")
logger.info("\nSA2: Tracking script execution with streamlit-analytics...")


def stop_tracking(
Expand All @@ -437,11 +439,11 @@ def stop_tracking(
"""

if verbose:
logging.info("SA2: Finished script execution. New counts:")
logging.info(
logger.info("SA2: Finished script execution. New counts:")
logger.info(
"%s", counts
) # Use %s and pass counts to logging to handle complex objects
logging.info("%s", "-" * 80) # For separators or multi-line messages
logger.info("%s", "-" * 80) # For separators or multi-line messages

# Reset streamlit functions.
st.button = _orig_button
Expand Down