Skip to content

Commit

Permalink
feat: add base logger to enable debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ohmayr committed Nov 4, 2024
1 parent 46b3d3a commit 10bb6ea
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
16 changes: 16 additions & 0 deletions google/api_core/client_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import logging


class BaseLogger:

def __init__(self, log_level=logging.CRITICAL):
self._logger = logging.getLogger("google")
self._logger.setLevel(log_level)
console_handler = logging.StreamHandler()
console_handler.setLevel(log_level)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
console_handler.setFormatter(formatter)
self._logger.addHandler(console_handler)

def get_logger(self):
return self._logger
17 changes: 17 additions & 0 deletions tests/unit/test_client_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import logging
import pytest

from google.api_core.client_logging import BaseLogger


def test_base_logger(caplog):

logger = BaseLogger().get_logger()

with caplog.at_level(logging.INFO, logger="google"):
logger.info("This is a test message.")

assert "This is a test message." in caplog.text
assert caplog.records[0].name == "google"
assert caplog.records[0].levelname == "INFO"
assert caplog.records[0].message == "This is a test message."

0 comments on commit 10bb6ea

Please sign in to comment.