diff --git a/google/api_core/client_logging.py b/google/api_core/client_logging.py new file mode 100644 index 00000000..1aa5160c --- /dev/null +++ b/google/api_core/client_logging.py @@ -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 diff --git a/tests/unit/test_client_logging.py b/tests/unit/test_client_logging.py new file mode 100644 index 00000000..5e03b64b --- /dev/null +++ b/tests/unit/test_client_logging.py @@ -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."