diff --git a/singer_sdk/logger.py b/singer_sdk/logger.py new file mode 100644 index 0000000000..31a733f5c4 --- /dev/null +++ b/singer_sdk/logger.py @@ -0,0 +1,52 @@ +"""Logging configuration for the Singer SDK.""" + +from __future__ import annotations + +import logging +import os +import typing as t +from pathlib import Path + +import yaml + +from singer_sdk.metrics import METRICS_LOG_LEVEL_SETTING, METRICS_LOGGER_NAME + +if t.TYPE_CHECKING: + from singer_sdk.helpers._compat import Traversable + +__all__ = ["setup_logging"] + + +def load_yaml_logging_config(path: Traversable | Path) -> t.Any: # noqa: ANN401 + """Load the logging config from the YAML file. + + Args: + path: A path to the YAML file. + + Returns: + The logging config. + """ + with path.open() as f: + return yaml.safe_load(f) + + +def setup_logging( + config: t.Mapping[str, t.Any], + default_logging_config: dict[str, t.Any], +) -> None: + """Setup logging. + + Args: + default_logging_config: A default + :py:std:label:`Python logging configuration dictionary`. + config: A plugin configuration dictionary. + """ + logging.config.dictConfig(default_logging_config) + + config = config or {} + metrics_log_level = config.get(METRICS_LOG_LEVEL_SETTING, "INFO").upper() + logging.getLogger(METRICS_LOGGER_NAME).setLevel(metrics_log_level) + + if "SINGER_SDK_LOG_CONFIG" in os.environ: # pragma: no cover + log_config_path = Path(os.environ["SINGER_SDK_LOG_CONFIG"]) + logging.config.dictConfig(load_yaml_logging_config(log_config_path)) diff --git a/singer_sdk/metrics.py b/singer_sdk/metrics.py index 990285ae04..2484054c46 100644 --- a/singer_sdk/metrics.py +++ b/singer_sdk/metrics.py @@ -7,21 +7,13 @@ import json import logging import logging.config -import os import typing as t from dataclasses import dataclass, field -from pathlib import Path from time import time -import yaml - -from singer_sdk.helpers._resources import get_package_files - if t.TYPE_CHECKING: from types import TracebackType - from singer_sdk.helpers._compat import Traversable - DEFAULT_LOG_INTERVAL = 60.0 METRICS_LOGGER_NAME = __name__ METRICS_LOG_LEVEL_SETTING = "metrics_log_level" @@ -377,43 +369,3 @@ def sync_timer(stream: str, **tags: t.Any) -> Timer: """ tags[Tag.STREAM] = stream return Timer(Metric.SYNC_DURATION, tags) - - -def _load_yaml_logging_config(path: Traversable | Path) -> t.Any: # noqa: ANN401 - """Load the logging config from the YAML file. - - Args: - path: A path to the YAML file. - - Returns: - The logging config. - """ - with path.open() as f: - return yaml.safe_load(f) - - -def _get_default_config() -> t.Any: # noqa: ANN401 - """Get a logging configuration. - - Returns: - A logging configuration. - """ - log_config_path = get_package_files("singer_sdk").joinpath("default_logging.yml") - return _load_yaml_logging_config(log_config_path) - - -def _setup_logging(config: t.Mapping[str, t.Any]) -> None: - """Setup logging. - - Args: - config: A plugin configuration dictionary. - """ - logging.config.dictConfig(_get_default_config()) - - config = config or {} - metrics_log_level = config.get(METRICS_LOG_LEVEL_SETTING, "INFO").upper() - logging.getLogger(METRICS_LOGGER_NAME).setLevel(metrics_log_level) - - if "SINGER_SDK_LOG_CONFIG" in os.environ: - log_config_path = Path(os.environ["SINGER_SDK_LOG_CONFIG"]) - logging.config.dictConfig(_load_yaml_logging_config(log_config_path)) diff --git a/singer_sdk/plugin_base.py b/singer_sdk/plugin_base.py index 1564558cfe..39add257bc 100644 --- a/singer_sdk/plugin_base.py +++ b/singer_sdk/plugin_base.py @@ -15,6 +15,7 @@ import click from jsonschema import Draft7Validator +import singer_sdk.logger as singer_logger from singer_sdk import about, metrics from singer_sdk.cli import plugin_cli from singer_sdk.configuration._dict_config import ( @@ -23,6 +24,7 @@ ) from singer_sdk.exceptions import ConfigValidationError from singer_sdk.helpers._classproperty import classproperty +from singer_sdk.helpers._resources import get_package_files from singer_sdk.helpers._secrets import SecretString, is_common_secret_key from singer_sdk.helpers._util import read_json_file from singer_sdk.helpers.capabilities import ( @@ -162,7 +164,7 @@ def __init__( if self._is_secret_config(k): config_dict[k] = SecretString(v) self._config = config_dict - metrics._setup_logging(self.config) # noqa: SLF001 + singer_logger.setup_logging(self.config, self.get_default_logging_config()) self.metrics_logger = metrics.get_metrics_logger() self._validate_config(raise_errors=validate_config) @@ -178,6 +180,15 @@ def setup_mapper(self) -> None: logger=self.logger, ) + def get_default_logging_config(self) -> t.Any: # noqa: ANN401, PLR6301 + """Get a default logging configuration for the plugin. + + Returns: + A logging configuration. + """ + log_config_path = get_package_files("singer_sdk") / "default_logging.yml" + return singer_logger.load_yaml_logging_config(log_config_path) + @property def mapper(self) -> PluginMapper: """Plugin mapper for this tap.